Repository navigation
fix(anthropic): sanitize tool_use ids on native /v1/messages path - #31094
mateo-berri merged 3 commits into
Conversation
Cross-provider replay (e.g. kimi -> Anthropic) can carry tool ids like functions.Bash:0 that violate Anthropic's ^[a-zA-Z0-9_-]+$ pattern and 400. Co-authored-by: Cursor <cursoragent@cursor.com>
Codecov Report❌ Patch coverage is
📢 Thoughts on this report? Let us know! |
Greptile SummaryThis PR adds ID sanitization for
Confidence Score: 5/5Safe to merge — the sanitization is idempotent, copy-on-write, and guarded against double-application. The change is narrowly scoped to ID normalization on an existing path, all new functions are idempotent (valid IDs pass through unchanged), the double-sanitization shortcut via No files require special attention.
|
| Filename | Overview |
|---|---|
| litellm/llms/anthropic/common_utils.py | Adds normalize_anthropic_tool_use_id, _sanitize_tool_use_id_content_block, and sanitize_tool_use_ids_in_anthropic_messages — all idempotent and copy-on-write; clean implementation. |
| litellm/llms/anthropic/experimental_pass_through/adapters/transformation.py | Refactors both the non-streaming and streaming tool-use ID paths to use the shared normalize_anthropic_tool_use_id helper; thought_sig is still correctly extracted for provider_specific_fields.signature. |
| litellm/llms/anthropic/experimental_pass_through/messages/handler.py | Adds sanitize_tool_use_ids_in_anthropic_messages call in both anthropic_messages and anthropic_messages_handler; the _litellm_messages_presanitized flag correctly prevents double-sanitization on the async path. |
| tests/test_litellm/llms/anthropic/test_anthropic_common_utils.py | Adds unit tests for sanitize_tool_use_ids_in_anthropic_messages and normalize_anthropic_tool_use_id; verifies both tool_use.id and tool_result.tool_use_id normalization and non-mutation of original messages. |
| tests/test_litellm/llms/anthropic/experimental_pass_through/adapters/test_anthropic_experimental_pass_through_adapters_transformation.py | Adds test for colon/dot ID sanitization in the OpenAI→Anthropic adapter transformation; all mock-only, no network calls. |
| tests/test_litellm/llms/anthropic/experimental_pass_through/messages/test_anthropic_experimental_pass_through_messages_handler.py | Adds async test verifying that anthropic_messages sanitizes tool IDs before dispatch and does not mutate the caller's original message list. |
Reviews (3): Last reviewed commit: "Merge remote-tracking branch 'origin/lit..." | Re-trigger Greptile
…itellm_sanitize-anthropic-tool-use-ids
Summary
tool_use/tool_resultids on the native Anthropic/v1/messagespath so replayed cross-provider history (e.g. kimi → Anthropic) no longer 400s on ids likefunctions.Bash:0tool_useblocks in the pass-through adapterFixes LIT-3881
Test plan
pytest tests/test_litellm/llms/anthropic/test_anthropic_common_utils.py -k "sanitize_tool_use or normalize_anthropic_tool_use"pytest tests/test_litellm/llms/anthropic/experimental_pass_through/adapters/test_anthropic_experimental_pass_through_adapters_transformation.py::test_translate_openai_content_to_anthropic_sanitizes_colon_dot_tool_call_idspytest tests/test_litellm/llms/anthropic/experimental_pass_through/messages/test_anthropic_experimental_pass_through_messages_handler.py::test_anthropic_messages_sanitizes_tool_use_ids_before_dispatch