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, 06:00 UTC
fix(python): preserve Anthropic tool result correlation (#5230)
Linear: [DEVREL-306](https://linear.app/composio/issue/DEVREL-306/tool-execution-response-shapes-are-inconsistent) ## Problem Gauge recommendation `cmvfzize507i30it0t33stivf` identifies missing Anthropic call/result correlation and confusion between session, modifier, and provider-helper contracts. The regression reproduced against published `composio==0.31.0` / `composio-anthropic==0.31.0` and the worktree's origin/next baseline: both session-backed and direct helper batches raised `KeyError: tool_use_id` when consumers tried to correlate two calls. Source evidence: runs `cmvtk2xiq000401fydrgbbdd1` seq 113/122, `cmvtk2xiq000601fy0gevtted` seq 92/95/101, `cmvfzawkc00av01nuxmosc2vx` seq 161/165/171, and `cmvfzamhp00ad01nus00np06n` seq 90/96/101/107/112/113. Live inspection of https://docs.composio.dev/docs/providers/anthropic.md confirmed that its Python example already pairs results with original tool-use IDs; that existing adaptation is correct, not the package fix. Related recommendation `cmvhl2427013q0ippyvciudqo` supplies advisory outcome-checking evidence, not an observed successful or failed provider-side send. **Root cause.** Owned by ComposioHQ/composio. Python `AnthropicProvider.execute_tool_call` returned the execution dictionary unchanged, discarding the originating `ToolUseBlock.id`; batch handling inherited that loss. `BaseProvider.execute_tool_for_target` also projected session results down to data/error/successful, dropping log ID, explicit session status, and returned Instant charges. The separate session-tool wrapper performed another conversion. Session and modifier contracts intentionally differ: Python uses `tool_slug` and a Pydantic model with `result_type`, direct execution uses `slug` and dictionaries with `successful`, and TypeScript uses positional tool slugs plus `resultType` or `successful` at different boundaries. TypeScript Anthropic helpers already correlate native message blocks but serialize a payload projection rather than a full execution response. ## Change Committed as `eac0839b8` on the assigned branch. Exported `AnthropicToolExecutionResponse` from `composio_anthropic`; single-call and batch helpers now return the existing dictionary fields plus the originating `tool_use_id`, without mutating the executor's dictionary. Consolidated Python session-result adaptation to retain `log_id`, `result_type`, and any returned `instant_charge`, while continuing to derive successful only from completed status. Migrated affected provider contract assertions and valid response fixtures, and added repeated-tool-name correlation, explicit success/failure, empty-error failure, serialization, public typing, and raised-auth-error coverage. Updated Python session/modifier docstrings, both SDK package READMEs, both Anthropic package READMEs, and TypeScript helper return documentation. Python still explicitly constructs Anthropic content blocks, including the correct tool-use ID mapping; the existing web guide remains unchanged and compatible. Routing, account selection, auth provisioning, dependency ranges, discovery layout, retry/idempotency behavior, generated outputs, and Gauge criteria are unchanged. TypeScript changes are documentation only; no TypeScript changeset is needed. ## Verification exercised - Published-package and baseline-source reproduction: both direct and session helper batches failed with missing tool_use_id; the independent authentication-error case passed before the fix. - Final rerunnable execution-contract suite: 327 passed, 11 skipped, with 59 deprecation warnings; covers provider helpers, session execution, modifiers, explicit null/empty-error failures, schema alias restoration, Google consumers, and Anthropic regressions. - Scoped Ruff check passed for all changed Python implementation and test files. - Mypy with the repository-locked pydantic 2.12.5 and json-schema-to-pydantic 0.4.11 passed all seven selected source files, including public single-call and batch response inference for both execution targets. - Built core and Anthropic wheels, installed non-editable artifacts outside the source tree, and ran all nine Anthropic provider tests against the installed distributions: nine passed. - Rebuilt and reinstalled final Python distributions without the package cache. A throwaway consumer used a real local HTTP socket with a synthetic service: two calls to the same tool retained distinct IDs, completed/failed statuses, log IDs, and returned Instant charge; empty-error failure remained unsuccessful; both Pydantic serializers worked; authentication failure raised. Anthropic client request serialization accepted the explicit Python tool_result adaptation, and its IDs matched the unchanged web-guide adaptation. - Installed @composio/core 0.28.0, @composio/anthropic 0.11.7, and @anthropic-ai/sdk 0.135.0 in an isolated TypeScript consumer. A local synthetic HTTP service verified resultType/logId serialization, multiple correlated native result blocks, JSON payload projection, and the documented absence of is_error on those helper blocks. - Extracted the new TypeScript core README example and typechecked it against installed public package types using tsc with strict checking: passed. - Rendered all four changed package READMEs from Markdown to HTML and checked the execution contract sections. Inspected installed Python package metadata/docstrings for the documented correlation, outcome, and serialization contracts. - Committed only the 17 intended files as eac0839b8; git status --porcelain was empty. Removed build scaffolding and throwaway consumers; local smoke servers stopped. No push or external product write was performed. Rerun by the Agent Experience Factory on the pushed commit before opening this PR: - `NODE_OPTIONS=--max-old-space-size=4096 UV_CONCURRENT_BUILDS=1 uv run --no-project --with ./python --with ./python/providers/anthropic --with ./python/providers/google --with pytest==9.2.0 --with pytest-mock==3.17.0 --with pytest-timeout==2.4.0 python -m pytest python/tests/test_provider.py python/tests/test_tool_router.py python/tests/test_tool_execution.py python/tests/test_google_provider.py python/tests/test_tool_schema_aliasing.py python/providers/anthropic/tests -q` in `.` — exit 0 - `NODE_OPTIONS=--max-old-space-size=4096 UV_CONCURRENT_BUILDS=1 uv run --no-project --with ruff==0.17.3 ruff check --config python/config/ruff.toml python/composio/core/models/tools.py python/composio/core/models/tool_router_session.py python/composio/core/models/_modifiers.py python/composio/core/provider/base.py python/providers/anthropic/composio_anthropic python/providers/anthropic/tests python/tests/test_type_inference_anthropic.py python/tests/test_provider.py python/tests/test_tool_router.py python/tests/test_google_provider.py python/tests/test_tool_schema_aliasing.py` in `.` — exit 0 - `NODE_OPTIONS=--max-old-space-size=4096 UV_CONCURRENT_BUILDS=1 uv run --no-project --with . --with ./providers/anthropic --with mypy==2.3.1 --with types-requests==2.33.0.20261030 --with types-jsonschema==4.26.0.20260518 --with pydantic==2.12.5 --with json-schema-to-pydantic==0.4.11 mypy --config-file config/mypy.ini composio/core/models/tools.py composio/core/models/tool_router_session.py composio/core/models/_modifiers.py composio/core/provider/base.py providers/anthropic/composio_anthropic tests/test_type_inference_anthropic.py` in `python` — exit 0 ## Remaining validation - SDK/provider publication is pending; the candidate contract is not claimed to be shipped on PyPI or deployed to production. - An artifact-exposed matched Gauge rerun of the cited source sessions remains pending after publication/deployment, with unchanged criteria. No paid runs, evaluations, optimizations, deployments, or criterion changes were started; original-finding removal and absence of new Gauge findings are not claimed. - All execution smoke responses were synthetic and local. No credentialed Composio/Anthropic action, provider-side send, auth recovery, exactly-once execution, or live side effect was attempted or established. - The full monorepo and every provider suite were not run; the selected regression run included 11 existing skipped tests. - An unpinned mypy environment using pydantic 2.14.2 / json-schema-to-pydantic 0.5.0 reported a transitive TypeAdapter typing error in unchanged python/composio/utils/schema_converter.py:813. The recorded rerunnable typecheck uses the repository's locked versions and passes; dependency ranges and that unrelated source were left unchanged. - The TypeScript Anthropic helper's existing payload-only serialization and lack of is_error remain unchanged and are now explicitly documented; application outcome decisions must use the session/direct execution contract rather than infer success from helper content. Not merged or deployed. A matched Gauge retest can only run after deployment. <sub>Opened by the Agent Experience Factory to improve agent preference across docs and landing pages (queue `2026-11-04-next`).</sub> ## Python CI follow-up The TypeSafe session test expected the old three-field execution response. It now supplies an explicit log ID and checks that the adapter preserves `log_id` and `result_type`. Validation at `518459ae2`: - `uv run --no-project --with ./python --with ./python/providers/typesafe --with hypothesis --with pytest --with pytest-mock pytest python/tests/test_typesafe_provider.py -q --no-header`: 141 passed. - Ruff lint and format checks for `python/tests/test_typesafe_provider.py`: passed. - `git diff --check`: passed. The TypeScript E2E jobs also reported OpenAI 401 `invalid_organization` errors. Their CI credential issue remains unresolved by this test-only change.