@devries-cameron20 / composio Mirror · read-only / commits / 8bb1d2995
Fork 0

Composio powers 1000+ toolkits, tool search, context management, authentication, and a sandboxed workbench to help you build AI agents that turn intent into action.

Mirrored from https://github.com/Camo67/composio.git

Synced Nov 21, 12:15 UTC

fix(core): raise tool not found only on 404/400

getRawComposioToolBySlug relabelled every client error, including an invalid API key (401), as ComposioToolNotFoundError. Map only 404/400 to not-found and wrap the rest in a new ComposioToolFetchError that keeps the client error as cause. Toolkits.getToolkitBySlug compared against the OpenAI APIError class, so its not-found branch never fired; import the Composio client class instead. Python mirrors the mapping: an unknown slug raises ToolNotFoundError (now a NotFoundError), anything else propagates the composio_client error unchanged. PRDE-1613 Claude-Session: https://claude.ai/code/session_017HtbhwMAKcfebo8HyXWa5s

jkomyno committed Sep 11, 2026 at 22:54 UTC 8bb1d299500c20331eb8b2cc59ddc4c5e36336e6
9 files changed +232 -20
.changeset/tool-not-found-only-on-404.md new
+5
@@ -0,0 +1,5 @@
1 +---
2 +'@composio/core': patch
3 +---
4 +
5 +Stop reporting every failed tool lookup as `ComposioToolNotFoundError`. `tools.getRawComposioToolBySlug`, and the `tools.get` / `tools.execute` paths that call it, now raise `ComposioToolNotFoundError` only when the API answers 404 or 400. Any other failure, such as an invalid API key (401), a server error, or a network fault, raises the new `ComposioToolFetchError` with the client error preserved as `cause`. `toolkits.get(slug)` now applies its 404/400 check against the Composio client's `APIError` instead of the OpenAI one, so an unknown toolkit raises `ComposioToolkitNotFoundError` as documented.
python/composio/core/models/tools.py
+18 -6
@@ -6,7 +6,7 @@ from collections.abc import Mapping
6 from pathlib import Path
7
8 import typing_extensions as te
9 -from composio_client import omit
9 +from composio_client import APIStatusError, omit
10 from pydantic import BaseModel as PydanticBaseModel
11
12 from composio.client import HttpClient
@@ -28,7 +28,11 @@ from composio.core.provider.agentic import AgenticProvider, AgenticProviderExecu
28 from composio.core.provider.base import BaseProvider, ExecuteToolFn
29 from composio.core.provider.none_agentic import NonAgenticProvider
30 from composio.core.types import ToolkitVersionParam
31 -from composio.exceptions import InvalidParams, ToolVersionRequiredError
31 +from composio.exceptions import (
32 + InvalidParams,
33 + ToolNotFoundError,
34 + ToolVersionRequiredError,
35 +)
36 from composio.utils.pydantic import none_to_omit
37 from composio.utils.toolkit_version import get_toolkit_version
38 from composio.utils.upload_dir_allowlist import resolve_effective_upload_allowlist
@@ -199,13 +203,21 @@ class Tools(Resource, t.Generic[TTool, TToolCollection]):
203 def get_raw_composio_tool_by_slug(self, slug: str) -> Tool:
204 """
205 Returns schema for the given tool slug.
206 +
207 + :raises ToolNotFoundError: when the backend reports the slug as unknown
208 + (404, or 400 for a malformed slug). Any other client error, such as
209 + an invalid API key, is re-raised unchanged.
210 """
203 - return _normalize_tool(
204 - self._client.tools.retrieve(
211 + try:
212 + response = self._client.tools.retrieve(
213 tool_slug=slug,
214 toolkit_versions=none_to_omit(self._toolkit_versions),
207 - ),
208 - )
215 + )
216 + except APIStatusError as error:
217 + if error.status_code in (400, 404):
218 + raise ToolNotFoundError(f"Tool with slug {slug} not found") from error
219 + raise
220 + return _normalize_tool(response)
221
222 def get_raw_composio_tools(
223 self,
python/composio/exceptions.py
+7 -2
@@ -449,8 +449,13 @@ class InvalidExecuteFunctionError(ComposioError):
449 pass
450
451
452 -class ToolNotFoundError(ComposioError):
453 - pass
452 +class ToolNotFoundError(NotFoundError):
453 + """Raised when a tool slug does not exist.
454 +
455 + Mirrors the TypeScript SDK's ``ComposioToolNotFoundError``. Other failures
456 + while fetching a tool (invalid API key, server or network errors) are not
457 + translated and surface as the underlying ``composio_client`` error.
458 + """
459
460
461 class InvalidModifier(ComposioError):
python/tests/test_tool_retrieval_errors.py new
+73
@@ -0,0 +1,73 @@
1 +"""Error mapping for ``Tools.get_raw_composio_tool_by_slug``.
2 +
3 +Only an unknown slug becomes ``ToolNotFoundError``. Other client failures,
4 +such as an invalid API key, must surface unchanged so callers can tell a
5 +missing tool from a rejected request (parity with the TypeScript SDK).
6 +"""
7 +
8 +from unittest.mock import Mock
9 +
10 +import httpx
11 +import pytest
12 +from composio_client import AuthenticationError, BadRequestError, NotFoundError
13 +
14 +from composio import exceptions
15 +from composio.core.models.tools import Tools
16 +from tests.conftest import mock_http_client
17 +
18 +
19 +def _status_error(cls, status: int):
20 + request = httpx.Request("GET", "https://backend.composio.dev/api/v3/tools/X")
21 + response = httpx.Response(status, request=request)
22 + return cls("error", response=response, body=None)
23 +
24 +
25 +@pytest.fixture
26 +def mock_client() -> Mock:
27 + return mock_http_client()
28 +
29 +
30 +@pytest.fixture
31 +def tools(mock_client: Mock) -> Tools:
32 + return Tools(client=mock_client, provider=Mock())
33 +
34 +
35 +def test_unknown_slug_raises_tool_not_found(tools: Tools, mock_client: Mock) -> None:
36 + not_found = _status_error(NotFoundError, 404)
37 + mock_client.tools.retrieve.side_effect = not_found
38 +
39 + with pytest.raises(exceptions.ToolNotFoundError) as exc_info:
40 + tools.get_raw_composio_tool_by_slug("NONEXISTENT_TOOL")
41 +
42 + assert exc_info.value.__cause__ is not_found
43 + assert isinstance(exc_info.value, exceptions.NotFoundError)
44 + assert "NONEXISTENT_TOOL" in str(exc_info.value)
45 +
46 +
47 +def test_malformed_slug_raises_tool_not_found(tools: Tools, mock_client: Mock) -> None:
48 + mock_client.tools.retrieve.side_effect = _status_error(BadRequestError, 400)
49 +
50 + with pytest.raises(exceptions.ToolNotFoundError):
51 + tools.get_raw_composio_tool_by_slug("malformed slug")
52 +
53 +
54 +def test_invalid_api_key_is_not_tool_not_found(tools: Tools, mock_client: Mock) -> None:
55 + unauthorized = _status_error(AuthenticationError, 401)
56 + mock_client.tools.retrieve.side_effect = unauthorized
57 +
58 + with pytest.raises(AuthenticationError) as exc_info:
59 + tools.get_raw_composio_tool_by_slug("SLACK_FETCH_CONVERSATION_HISTORY")
60 +
61 + assert exc_info.value is unauthorized
62 + assert exc_info.value.status_code == 401
63 +
64 +
65 +def test_execute_surfaces_auth_error_from_schema_fetch(
66 + tools: Tools, mock_client: Mock
67 +) -> None:
68 + mock_client.tools.retrieve.side_effect = _status_error(AuthenticationError, 401)
69 +
70 + with pytest.raises(AuthenticationError):
71 + tools.execute("SLACK_FETCH_CONVERSATION_HISTORY", {}, user_id="user")
72 +
73 + mock_client.tools.execute.assert_not_called()
ts/packages/core/src/errors/ToolErrors.ts
+24
@@ -5,6 +5,7 @@ import { ComposioConnectedAccountNotFoundError } from './ConnectedAccountsErrors
5 export const ToolErrorCodes = {
6 TOOLSET_NOT_DEFINED: 'TOOLSET_NOT_DEFINED',
7 TOOL_NOT_FOUND: 'TOOL_NOT_FOUND',
8 + TOOL_FETCH_ERROR: 'TOOL_FETCH_ERROR',
9 INVALID_MODIFIER: 'INVALID_MODIFIER',
10 TOOL_EXECUTION_ERROR: 'TOOL_EXECUTION_ERROR',
11 INVALID_TOOL_ARGUMENTS: 'INVALID_TOOL_ARGUMENTS',
@@ -45,6 +46,29 @@ export class ComposioToolNotFoundError extends ComposioError {
46 }
47 }
48
49 +/**
50 + * Error thrown when a tool schema could not be fetched for a reason other than
51 + * the tool not existing (for example an invalid API key, a server error, or a
52 + * network failure). The underlying client error is preserved as `cause`.
53 + */
54 +export class ComposioToolFetchError extends ComposioError {
55 + constructor(
56 + message: string = 'Failed to fetch tool',
57 + options: Omit<ComposioErrorOptions, 'code' | 'statusCode'> = {}
58 + ) {
59 + super(message, {
60 + ...options,
61 + code: ToolErrorCodes.TOOL_FETCH_ERROR,
62 + possibleFixes: options.possibleFixes || [
63 + 'Ensure the tool slug is valid',
64 + 'Ensure you are using the correct API key',
65 + 'Ensure you are using the correct API endpoint / Base URL and it is working',
66 + ],
67 + });
68 + this.name = 'ComposioToolFetchError';
69 + }
70 +}
71 +
72 export class ComposioInvalidModifierError extends ComposioError {
73 constructor(
74 message: string = 'Invalid modifier',
ts/packages/core/src/models/Toolkits.ts
+1 -2
@@ -1,4 +1,4 @@
1 -import ComposioClient from '@composio/client';
1 +import ComposioClient, { APIError } from '@composio/client';
2 import {
3 ToolkitListParams,
4 ToolKitListResponse,
@@ -16,7 +16,6 @@ import { ConnectionRequest } from '../types/connectionRequest.types';
16 import { telemetry } from '../telemetry/Telemetry';
17 import { AuthSchemeType } from '../types/authConfigs.types';
18 import logger from '../utils/logger';
19 -import { APIError } from 'openai';
19 import {
20 transformToolkitListResponse,
21 transformToolkitRetrieveCategoriesResponse,
ts/packages/core/src/models/Tools.ts
+13 -2
@@ -1,4 +1,4 @@
1 -import ComposioClient from '@composio/client';
1 +import ComposioClient, { APIError } from '@composio/client';
2 import { FileToolModifier } from '#file_tool_modifier';
3 import {
4 Tool,
@@ -42,6 +42,7 @@ import logger from '../utils/logger';
42 import { ExecuteToolFn, GlobalExecuteToolFn } from '../types/provider.types';
43 import {
44 ComposioInvalidModifierError,
45 + ComposioToolFetchError,
46 ComposioToolNotFoundError,
47 ComposioProviderNotDefinedError,
48 ComposioToolVersionRequiredError,
@@ -710,7 +711,17 @@ export class Tools<
711 if (error instanceof ComposioRequestCancelledError) {
712 throw error;
713 }
713 - throw new ComposioToolNotFoundError(`Unable to retrieve tool with slug ${slug}`, {
714 + // The tools endpoint reports an unknown slug as 404 (or 400 for a
715 + // malformed one). Anything else (401, 5xx, network) is not "not found",
716 + // so keep the client error reachable as `cause` under a generic error.
717 + if (error instanceof APIError && (error.status === 404 || error.status === 400)) {
718 + throw new ComposioToolNotFoundError(`Tool with slug ${slug} not found`, {
719 + meta: { slug },
720 + cause: error,
721 + });
722 + }
723 + throw new ComposioToolFetchError(`Unable to retrieve tool with slug ${slug}`, {
724 + meta: { slug },
725 cause: error,
726 });
727 }
ts/packages/core/test/models/toolkits.test.ts
+32 -4
@@ -3,6 +3,10 @@ import { Toolkits } from '../../src/models/Toolkits';
3 import ComposioClient from '@composio/client';
4 import { telemetry } from '../../src/telemetry/Telemetry';
5 import { ComposioAuthConfigNotFoundError } from '../../src/errors/AuthConfigErrors';
6 +import {
7 + ComposioToolkitFetchError,
8 + ComposioToolkitNotFoundError,
9 +} from '../../src/errors/ToolkitErrors';
10 import { AuthSchemeTypes } from '../../src/types/authConfigs.types';
11 import { APIError } from '@composio/client';
12 import type { ToolkitListParams } from '../../src/types/toolkit.types';
@@ -233,10 +237,33 @@ describe('Toolkits', () => {
237 await expect(promise).rejects.toThrowError('Failed to fetch toolkits');
238 });
239
236 - it('should throw ComposioToolkitNotFoundError when toolkit not found', async () => {
237 - mockClient.toolkits.retrieve.mockRejectedValue(
238 - new Error('Toolkit with slug non-existent not found')
240 + it('should throw ComposioToolkitNotFoundError when the API returns 404', async () => {
241 + const notFound = new ComposioClient.NotFoundError(404, undefined, undefined, new Headers());
242 + mockClient.toolkits.retrieve.mockRejectedValueOnce(notFound);
243 +
244 + const error = await toolkits.get('non-existent').catch(e => e);
245 +
246 + expect(error).toBeInstanceOf(ComposioToolkitNotFoundError);
247 + expect(error.cause).toBe(notFound);
248 + });
249 +
250 + it('should not report an invalid API key (401) as toolkit not found', async () => {
251 + const unauthorized = new ComposioClient.AuthenticationError(
252 + 401,
253 + undefined,
254 + undefined,
255 + new Headers()
256 );
257 + mockClient.toolkits.retrieve.mockRejectedValueOnce(unauthorized);
258 +
259 + const error = await toolkits.get('github').catch(e => e);
260 +
261 + expect(error).toBeInstanceOf(ComposioToolkitFetchError);
262 + expect(error.cause).toBe(unauthorized);
263 + });
264 +
265 + it('should throw ComposioToolkitFetchError for non-API failures', async () => {
266 + mockClient.toolkits.retrieve.mockRejectedValueOnce(new Error('socket hang up'));
267
268 const promise = toolkits.get('non-existent');
269 await expect(promise).rejects.toThrowError("Couldn't fetch Toolkit with slug: non-existent");
@@ -530,7 +557,8 @@ describe('Toolkits', () => {
557
558 const promise = toolkits.authorize('user-123', 'non-existent');
559
533 - await expect(promise).rejects.toThrow("Couldn't fetch Toolkit with slug: non-existent");
560 + await expect(promise).rejects.toThrow(ComposioToolkitNotFoundError);
561 + await expect(promise).rejects.toThrow('Toolkit with slug non-existent not found');
562 expect(mockClient.authConfigs.list).not.toHaveBeenCalled();
563 expect(mockClient.authConfigs.create).not.toHaveBeenCalled();
564 expect(mockClient.connectedAccounts.create).not.toHaveBeenCalled();
ts/packages/core/test/tools/tools.test.ts
+59 -4
@@ -1,4 +1,5 @@
1 import { describe, it, expect, vi, beforeEach, afterEach } from 'vitest';
2 +import ComposioClient from '@composio/client';
3 import { mockClient } from '../utils/mocks/client.mock';
4 import { toolMocks } from '../utils/mocks/data.mock';
5 import { Tool, ToolListParams, ToolExecuteParams } from '../../src/types/tool.types';
@@ -12,6 +13,7 @@ import {
13 } from '../utils/toolExecuteUtils';
14 import { MockProvider } from '../utils/mocks/provider.mock';
15 import { ValidationError } from '../../src/errors/ValidationErrors';
16 +import { ComposioToolFetchError, ComposioToolNotFoundError } from '../../src/errors/ToolErrors';
17
18 // Minimal structural shape for a ComposioError-like value (possibly wrapping
19 // another error as its `cause`), used to narrow `catch (error: unknown)`
@@ -455,14 +457,67 @@ describe('Tools', () => {
457 expect(result.slug).toEqual(toolMocks.transformedTool.slug);
458 });
459
458 - it('should throw an error if tool is not found', async () => {
460 + it('should throw ComposioToolNotFoundError when the API returns 404', async () => {
461 const slug = 'NONEXISTENT_TOOL';
462 + const notFound = new ComposioClient.NotFoundError(404, undefined, undefined, new Headers());
463
461 - mockClient.tools.retrieve.mockRejectedValue(null);
464 + mockClient.tools.retrieve.mockRejectedValueOnce(notFound);
465
463 - await expect(context.tools.getRawComposioToolBySlug(slug)).rejects.toThrow(
464 - `Unable to retrieve tool with slug ${slug}`
466 + const error = await context.tools.getRawComposioToolBySlug(slug).catch(e => e);
467 +
468 + expect(error).toBeInstanceOf(ComposioToolNotFoundError);
469 + expect(error.message).toBe(`Tool with slug ${slug} not found`);
470 + expect(error.cause).toBe(notFound);
471 + });
472 +
473 + it('should throw ComposioToolNotFoundError when the API returns 400', async () => {
474 + const slug = 'malformed slug';
475 + const badRequest = new ComposioClient.BadRequestError(
476 + 400,
477 + undefined,
478 + undefined,
479 + new Headers()
480 + );
481 +
482 + mockClient.tools.retrieve.mockRejectedValueOnce(badRequest);
483 +
484 + const error = await context.tools.getRawComposioToolBySlug(slug).catch(e => e);
485 +
486 + expect(error).toBeInstanceOf(ComposioToolNotFoundError);
487 + expect(error.cause).toBe(badRequest);
488 + });
489 +
490 + it('should not report an invalid API key (401) as tool not found', async () => {
491 + const slug = 'SLACK_FETCH_CONVERSATION_HISTORY';
492 + const unauthorized = new ComposioClient.AuthenticationError(
493 + 401,
494 + { error: { message: 'Invalid API key', code: 801, status: 401 } },
495 + undefined,
496 + new Headers()
497 );
498 +
499 + mockClient.tools.retrieve.mockRejectedValueOnce(unauthorized);
500 +
501 + const error = await context.tools.getRawComposioToolBySlug(slug).catch(e => e);
502 +
503 + expect(error).toBeInstanceOf(ComposioToolFetchError);
504 + expect(error).not.toBeInstanceOf(ComposioToolNotFoundError);
505 + expect(error.name).toBe('ComposioToolFetchError');
506 + expect(error.message).toBe(`Unable to retrieve tool with slug ${slug}`);
507 + expect(error.cause).toBe(unauthorized);
508 + expect(error.cause.status).toBe(401);
509 + });
510 +
511 + it('should wrap non-API failures in ComposioToolFetchError', async () => {
512 + const slug = 'TOOL_SLUG';
513 + const networkError = new Error('socket hang up');
514 +
515 + mockClient.tools.retrieve.mockRejectedValueOnce(networkError);
516 +
517 + const error = await context.tools.getRawComposioToolBySlug(slug).catch(e => e);
518 +
519 + expect(error).toBeInstanceOf(ComposioToolFetchError);
520 + expect(error.cause).toBe(networkError);
521 });
522
523 it('should apply schema modifiers when provided', async () => {