Skip to content

Commit 22f996e

Browse files
committed
fix(anthropic): preserve sibling keys beside function_response result
When a tool response dict has result plus other keys, serialize the full dict for Claude tool_result content (parity with Gemini). Keep single-key {result: x} unwrapping. Fixes google#7073. Rebased onto latest main.
1 parent 460715b commit 22f996e

2 files changed

Lines changed: 29 additions & 8 deletions

File tree

‎src/google/adk/models/anthropic_llm.py‎

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -514,15 +514,17 @@ def _part_to_message_block(
514514
# SDK ref: anthropic.types.tool_result_block_param
515515
# https://github.com/anthropics/anthropic-sdk-python/blob/main/src/anthropic/types/tool_result_block_param.py
516516
# Exactly {"result": value} is ADK's wrapper for a non-dict tool return.
517-
elif (
518-
response_data.keys() == {"result"}
519-
and response_data["result"] is not None
520-
):
521-
result = response_data["result"]
522-
if isinstance(result, (dict, list)):
523-
content = json.dumps(result)
517+
# If sibling keys exist beside result, serialize the whole dict so Claude
518+
# matches Gemini (issue #7073).
519+
elif "result" in response_data and response_data["result"] is not None:
520+
if response_data.keys() == {"result"}:
521+
result = response_data["result"]
522+
if isinstance(result, (dict, list)):
523+
content = json.dumps(result)
524+
else:
525+
content = str(result)
524526
else:
525-
content = str(result)
527+
content = json.dumps(response_data, default=str)
526528
elif response_data:
527529
# Fallback: serialize the entire response dict as JSON so that tools
528530
# returning arbitrary key structures (e.g. load_skill returning

‎tests/unittests/models/test_anthropic_llm.py‎

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -928,6 +928,25 @@ def test_part_to_message_block_with_traditional_result():
928928
assert "This is the result from the tool" in result["content"]
929929

930930

931+
def test_part_to_message_block_preserves_sibling_keys_with_result():
932+
"""Issue #7073: sibling keys next to result must reach Claude."""
933+
response_part = types.Part.from_function_response(
934+
name="run_python_analysis",
935+
response={
936+
"result": {"matched": 33, "total": 49},
937+
"files": [{"filename": "matching.xlsx", "bytes": 8684}],
938+
"stdout": "Saved to /tmp/outputs/matching.xlsx\n",
939+
},
940+
)
941+
response_part.function_response.id = "toolu_01"
942+
943+
result = part_to_message_block(response_part)
944+
parsed = json.loads(result["content"])
945+
assert parsed["result"] == {"matched": 33, "total": 49}
946+
assert parsed["files"] == [{"filename": "matching.xlsx", "bytes": 8684}]
947+
assert parsed["stdout"] == "Saved to /tmp/outputs/matching.xlsx\n"
948+
949+
931950
def test_part_to_message_block_with_multiple_content_items():
932951
"""Test content with multiple items."""
933952
from google.adk.models.anthropic_llm import part_to_message_block

0 commit comments

Comments
 (0)