AnthropicLlm._ToolUseIdSanitizer cannot pair an id-less tool_result with a call whose id was invalid-but-present (Anthropic 400)
Maintainer antworten meist innerhalb von 3 Tagen
@llalitkumarrr arbeitet bereits daran.
Seit 23.11.2026.
Bewertung
- Schwierigkeit
- 3/5
- Geschätzter Aufwand
- Ein halber Tag
- Anfängerfreundlichkeit
- 25/100
Rechercherichtung
The bug lives in _ToolUseIdSanitizer in src/google/adk/models/anthropic_llm.py; compare how sanitize fills _mapping versus _unpaired_calls_by_name and _unpaired_calls_list for invalid-but-present ids. Run the standalone repro in the issue, then tests/unittests/models/test_anthropic_llm.py. Done means the repro's result_id == call_id assertion passes and the existing tests stay green. A linked pull request (#8298) already exists, so check it before starting.
Vom Indexierungsmodell aus dem Issue-Text verfasst.
Beschreibung
Describe the bug
_ToolUseIdSanitizer (src/google/adk/models/anthropic_llm.py) is documented to "Reuse one instance per conversation so a tool_use and its paired tool_result get matching unique outputs without collision across turns." That contract breaks for a mixed form: a tool call whose id is present but invalid, followed by a tool result that arrives without any id.
Anthropic tool ids must match [a-zA-Z0-9_-]+. The sanitizer correctly rewrites a call id containing ., :, /, @ … to a toolu_fallback_N id — but it only registers the fallback in the id→fallback mapping, never in the name-based pending registries (_unpaired_calls_by_name / _unpaired_calls_list), which are only filled when tool_id is falsy.
So when the paired result comes back with tool_id="" (the Response with empty/None tool_id: pair with oldest pending call path), the by-name queue is empty — the call never registered there — and the result gets a brand-new fallback id. The converted request then contains a tool_result whose tool_use_id (toolu_fallback_1) matches no tool_use block (the call is toolu_fallback_0), which the Anthropic API rejects with 400 ... tool_result block references unknown tool_use_id.
The same conversation works fine when the call's id is absent entirely (both registries get filled), so only the invalid-but-present form is broken.
To reproduce
Standalone script, no network, no API keys — only google.adk.models.anthropic_llm and google.genai.types:
import re
from google.adk.models.anthropic_llm import _ToolUseIdSanitizer
s = _ToolUseIdSanitizer()
# 1) call with an id that is present but invalid -> rewritten to fallback
call_id = s.sanitize(is_call=True, tool_id="call.hivemind:1", tool_name="get_weather")
# 2) paired result that lost its id entirely -> should pair with the call
result_id = s.sanitize(is_call=False, tool_id="", tool_name="get_weather")
print(call_id, result_id) # toolu_fallback_0 toolu_fallback_1 <-- mismatch
assert re.fullmatch(r"[a-zA-Z0-9_-]+", call_id)
assert result_id == call_id, f"result {result_id} does not match call {call_id}"
Observed on main (2b90353c): result_id is a fresh toolu_fallback_1, so the request the Anthropic backend receives pairs a result with a nonexistent tool_use id and fails with HTTP 400. With is_call=True, tool_id=None (id truly absent) the same pair matches, which isolates the bug to the invalid-but-present form.
Expected behavior
An id-less tool result should pair with the oldest pending call of the same tool regardless of whether that call's own id was absent or merely invalid — the sanitizer is the component that makes both forms valid for Anthropic, and tool_result.tool_use_id must reference the emitted tool_use id.
Proposed fix
Two small changes inside _ToolUseIdSanitizer (~40 lines, no API change):
- Register every fallbacked call for name-based pairing, whether its id was absent or invalid (invalid-id calls currently register only in
_mapping). - Withdraw claimed fallbacks from the pending registries (
_claimed_fallbacks), so a call already answered through its own id cannot also be claimed by an id-less result of the same tool.
Additional context
- Verified: 259 existing tests in
tests/unittests/models/test_anthropic_llm.pypass both on currentmainand with the fix; a 4-case contract suite (2 id-less baselines + 2 invalid-id cases) fails onmainand passes with the fix. - I have the fix prepared and will open a PR referencing this issue.
- Vorherrschende Sprache
- Python
- Sterne
- 22.3k
- Forks
- 4.4k
- Ø Merge
- 1 T. 2 Std.
- Gemergte PRs (30 T.)
- 6
Entwicklungsumgebung
- Kein Dockerfile und keine Docker-Compose-Datei
- Hat eine Pull-Request-Vorlage
- Beitragsleitfaden lesen
Erste Schritte
- Lesen Sie das ganze Issue und danach den Beitragsleitfaden des Projekts.
- Schreiben Sie ins Issue, dass Sie es übernehmen — das erspart doppelte Arbeit.
- Forken Sie das Repository und arbeiten Sie in einem Branch.
- Öffnen Sie einen Pull Request, der die Issue-Nummer nennt.
Mehr aus google/adk-python
-
Schwierigkeit 2/5 1-3 Stunden Anfängerfreundlichkeit 72/100
google/adk-python#8360 ·
Maintainer antworten meist innerhalb von 3 Tagen
-
Schwierigkeit 2/5 1-3 Stunden Anfängerfreundlichkeit 82/100
google/adk-python#8359 ·
Maintainer antworten meist innerhalb von 3 Tagen
-
Can pin to litellm version be changed?Evtl. vergeben @llalitkumarrr hat das vor 10 Tagen übernommen. Offenmodels
Schwierigkeit 1/5 Unter einer Stunde Anfängerfreundlichkeit 65/100
google/adk-python#8129 · 2 Kommentare · 1 zugewiesene Person ·
Maintainer antworten meist innerhalb von 3 Tagen
-
OpenAPI tools ignore operation- and path-level server overridesEvtl. vergeben @sanketpatil06 hat das vor 14 Tagen übernommen. Offentools
Schwierigkeit 2/5 1-3 Stunden Anfängerfreundlichkeit 75/100
google/adk-python#8064 · 4 Kommentare · 1 zugewiesene Person ·
Maintainer antworten meist innerhalb von 3 Tagen
-
Two docstrings in openapi_tool document parameters the signatures do not acceptEvtl. vergeben @surajksharma07 hat das vor 17 Tagen übernommen. Offen
Schwierigkeit 1/5 Unter einer Stunde Anfängerfreundlichkeit 88/100
google/adk-python#8024 · 1 Kommentar · 1 zugewiesene Person ·
Maintainer antworten meist innerhalb von 3 Tagen
Alle Issues in google/adk-python
Ähnliche Issues
-
🧫10장-투향
Schwierigkeit 2/5 1-3 Stunden Anfängerfreundlichkeit 65/100
hyunjimoon/golden-cage-book#188 ·
-
agent/scanner bug stage/010-actionable
Schwierigkeit 1/5 Unter einer Stunde Anfängerfreundlichkeit 88/100
torch-spyre/spyre-inference#1812 ·
Maintainer antworten meist innerhalb von 1 Tag
-
Bug: fenced drafts inside <think> tags break parsing or are returned instead of the answerEvtl. vergeben @aad1tyaaaaa hat das heute übernommen. Offen
Schwierigkeit 2/5 1-3 Stunden Anfängerfreundlichkeit 76/100
google/langextract#639 ·
Maintainer antworten meist innerhalb von 1 Tag
-
[oblt-aw][autodoc] Update autodoc routing docs for relative in-repo lock usesEvtl. vergeben @github-actions hat das heute übernommen. Offen
Schwierigkeit 1/5 1-3 Stunden Anfängerfreundlichkeit 87/100
Maintainer antworten meist innerhalb von 1 Tag
-
Schwierigkeit 2/5 1-3 Stunden Anfängerfreundlichkeit 88/100
Maintainer antworten meist innerhalb von 1 Tag