Skip to content

Commit 8d622cc

Browse files
committed
fix(anthropic): handle arbitrary dictionary tool outputs
1 parent 79fcddb commit 8d622cc

File tree

1 file changed

+14
-10
lines changed

1 file changed

+14
-10
lines changed

src/google/adk/models/anthropic_llm.py

Lines changed: 14 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -100,26 +100,30 @@ def part_to_message_block(
100100
content = ""
101101
response_data = part.function_response.response
102102

103-
# Handle response with content array
104-
if "content" in response_data and response_data["content"]:
103+
# Handle content array format
104+
if "content" in response_data and isinstance(
105+
response_data["content"], list
106+
):
105107
content_items = []
106108
for item in response_data["content"]:
107109
if isinstance(item, dict):
108-
# Handle text content blocks
109110
if item.get("type") == "text" and "text" in item:
110111
content_items.append(item["text"])
111112
else:
112-
# Handle other structured content
113113
content_items.append(str(item))
114114
else:
115115
content_items.append(str(item))
116-
content = "\n".join(content_items) if content_items else ""
117-
# Handle traditional result format
118-
elif "result" in response_data and response_data["result"]:
119-
# Transformation is required because the content is a list of dict.
120-
# ToolResultBlockParam content doesn't support list of dict. Converting
121-
# to str to prevent anthropic.BadRequestError from being thrown.
116+
content = "\n".join(content_items)
117+
# Handle direct result key format
118+
elif (
119+
isinstance(response_data, dict)
120+
and "result" in response_data
121+
and len(response_data) == 1
122+
):
122123
content = str(response_data["result"])
124+
# Fallback: handle arbitrary dictionary or object by stringifying it
125+
else:
126+
content = str(response_data)
123127

124128
return anthropic_types.ToolResultBlockParam(
125129
tool_use_id=part.function_response.id or "",

0 commit comments

Comments
 (0)