Skip to content

Commit cc063d9

Browse files
google-genai-botcopybara-github
authored andcommitted
fix: Add support for code execution result and skip inline data in anthropic llm
``` blaze run assistant/lamda/bard/scrape/vertex/tools:use_vertex_agent -- --prompt_collection_id=cab_v0p7@3 --agent_name=vertex_1p_agent --kernel_id=vertex_agent:claude-sonnet-4@20250514 --dynamically_inject_tools=True --add_final_answer_tag=True --example_ids=plot-line-004_0.5_new_1 ``` TODO: https://evalhub.corp.google.com/runs/Uf5en14gqmK-R/links PiperOrigin-RevId: 768098002
1 parent a021222 commit cc063d9

File tree

1 file changed

+42
-3
lines changed

1 file changed

+42
-3
lines changed

src/google/adk/models/anthropic_llm.py

Lines changed: 42 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616

1717
from __future__ import annotations
1818

19+
import base64
1920
from functools import cached_property
2021
import logging
2122
import os
@@ -45,7 +46,7 @@
4546

4647
logger = logging.getLogger("google_adk." + __name__)
4748

48-
MAX_TOKEN = 1024
49+
MAX_TOKEN = 8192
4950

5051

5152
class ClaudeRequest(BaseModel):
@@ -105,15 +106,53 @@ def part_to_message_block(
105106
content=content,
106107
is_error=False,
107108
)
108-
raise NotImplementedError("Not supported yet.")
109+
if (
110+
part.inline_data
111+
and part.inline_data.mime_type
112+
and part.inline_data.mime_type.startswith("image")
113+
):
114+
data = base64.b64encode(part.inline_data.data).decode()
115+
return anthropic_types.ImageBlockParam(
116+
type="image",
117+
source=dict(
118+
type="base64", media_type=part.inline_data.mime_type, data=data
119+
),
120+
)
121+
if part.executable_code:
122+
return anthropic_types.TextBlockParam(
123+
type="text",
124+
text="Code:```python\n" + part.executable_code.code + "\n```",
125+
# language=part.executable_code.language,
126+
)
127+
if part.code_execution_result:
128+
return anthropic_types.TextBlockParam(
129+
text="Execution Result:```code_output\n"
130+
+ part.code_execution_result.output
131+
+ "\n```",
132+
type="text",
133+
)
134+
135+
raise NotImplementedError(f"Not supported yet: {part}")
109136

110137

111138
def content_to_message_param(
112139
content: types.Content,
113140
) -> anthropic_types.MessageParam:
141+
message_block = []
142+
for part in content.parts or []:
143+
# Image data is not supported in Claude for model turns.
144+
if (
145+
part.inline_data
146+
and part.inline_data.mime_type
147+
and part.inline_data.mime_type.startswith("image")
148+
):
149+
continue
150+
151+
message_block.append(part_to_message_block(part))
152+
114153
return {
115154
"role": to_claude_role(content.role),
116-
"content": [part_to_message_block(part) for part in content.parts or []],
155+
"content": message_block,
117156
}
118157

119158

0 commit comments

Comments
 (0)