Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 10 additions & 8 deletions src/google/adk/models/anthropic_llm.py
Original file line number Diff line number Diff line change
Expand Up @@ -514,15 +514,17 @@ def _part_to_message_block(
# SDK ref: anthropic.types.tool_result_block_param
# https://github.com/anthropics/anthropic-sdk-python/blob/main/src/anthropic/types/tool_result_block_param.py
# Exactly {"result": value} is ADK's wrapper for a non-dict tool return.
elif (
response_data.keys() == {"result"}
and response_data["result"] is not None
):
result = response_data["result"]
if isinstance(result, (dict, list)):
content = json.dumps(result)
# If sibling keys exist beside result, serialize the whole dict so Claude
# matches Gemini (issue #7073).
elif "result" in response_data and response_data["result"] is not None:
if response_data.keys() == {"result"}:
result = response_data["result"]
if isinstance(result, (dict, list)):
content = json.dumps(result)
else:
content = str(result)
else:
content = str(result)
content = json.dumps(response_data, default=str)
elif response_data:
# Fallback: serialize the entire response dict as JSON so that tools
# returning arbitrary key structures (e.g. load_skill returning
Expand Down
19 changes: 19 additions & 0 deletions tests/unittests/models/test_anthropic_llm.py
Original file line number Diff line number Diff line change
Expand Up @@ -928,6 +928,25 @@ def test_part_to_message_block_with_traditional_result():
assert "This is the result from the tool" in result["content"]


def test_part_to_message_block_preserves_sibling_keys_with_result():
"""Issue #7073: sibling keys next to result must reach Claude."""
response_part = types.Part.from_function_response(
name="run_python_analysis",
response={
"result": {"matched": 33, "total": 49},
"files": [{"filename": "matching.xlsx", "bytes": 8684}],
"stdout": "Saved to /tmp/outputs/matching.xlsx\n",
},
)
response_part.function_response.id = "toolu_01"

result = part_to_message_block(response_part)
parsed = json.loads(result["content"])
assert parsed["result"] == {"matched": 33, "total": 49}
assert parsed["files"] == [{"filename": "matching.xlsx", "bytes": 8684}]
assert parsed["stdout"] == "Saved to /tmp/outputs/matching.xlsx\n"


def test_part_to_message_block_with_multiple_content_items():
"""Test content with multiple items."""
from google.adk.models.anthropic_llm import part_to_message_block
Expand Down