Skip to content

Commit ffb5363

Browse files
GWealecopybara-github
authored andcommitted
fix(models): keep the keys beside "result" in a Claude tool result
Close #7073 Co-authored-by: George Weale <[email protected]> PiperOrigin-RevId: 978745007
1 parent 3e1ae9f commit ffb5363

2 files changed

Lines changed: 27 additions & 1 deletion

File tree

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

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -506,7 +506,11 @@ def _part_to_message_block(
506506
# We serialize to str here
507507
# SDK ref: anthropic.types.tool_result_block_param
508508
# https://github.com/anthropics/anthropic-sdk-python/blob/main/src/anthropic/types/tool_result_block_param.py
509-
elif "result" in response_data and response_data["result"] is not None:
509+
# Exactly {"result": value} is ADK's wrapper for a non-dict tool return.
510+
elif (
511+
response_data.keys() == {"result"}
512+
and response_data["result"] is not None
513+
):
510514
result = response_data["result"]
511515
if isinstance(result, (dict, list)):
512516
content = json.dumps(result)

‎tests/unittests/models/test_anthropic_llm.py‎

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1288,6 +1288,28 @@ def test_part_to_message_block_nested_dict_result():
12881288
assert parsed["results"][0]["tags"] == ["a", "b"]
12891289

12901290

1291+
def test_part_to_message_block_keys_beside_result_are_kept():
1292+
"""A tool's own dict travels whole even when one of its keys is 'result'."""
1293+
response_part = types.Part.from_function_response(
1294+
name="run_code",
1295+
response={
1296+
"result": "ok",
1297+
"files": ["report.csv"],
1298+
"stdout": "wrote report.csv",
1299+
},
1300+
)
1301+
response_part.function_response.id = "test_id"
1302+
1303+
result = part_to_message_block(response_part)
1304+
1305+
parsed = json.loads(result["content"])
1306+
assert parsed == {
1307+
"result": "ok",
1308+
"files": ["report.csv"],
1309+
"stdout": "wrote report.csv",
1310+
}
1311+
1312+
12911313
# --- Tests for arbitrary dict fallback (e.g. SkillToolset load_skill) ---
12921314

12931315

0 commit comments

Comments
 (0)