16
16
17
17
from __future__ import annotations
18
18
19
+ import base64
19
20
from functools import cached_property
20
21
import logging
21
22
import os
45
46
46
47
logger = logging .getLogger ("google_adk." + __name__ )
47
48
48
- MAX_TOKEN = 1024
49
+ MAX_TOKEN = 8192
49
50
50
51
51
52
class ClaudeRequest (BaseModel ):
@@ -70,6 +71,14 @@ def to_google_genai_finish_reason(
70
71
return "FINISH_REASON_UNSPECIFIED"
71
72
72
73
74
+ def _is_image_part (part : types .Part ) -> bool :
75
+ return (
76
+ part .inline_data
77
+ and part .inline_data .mime_type
78
+ and part .inline_data .mime_type .startswith ("image" )
79
+ )
80
+
81
+
73
82
def part_to_message_block (
74
83
part : types .Part ,
75
84
) -> Union [
@@ -80,7 +89,7 @@ def part_to_message_block(
80
89
]:
81
90
if part .text :
82
91
return anthropic_types .TextBlockParam (text = part .text , type = "text" )
83
- if part .function_call :
92
+ elif part .function_call :
84
93
assert part .function_call .name
85
94
86
95
return anthropic_types .ToolUseBlockParam (
@@ -89,7 +98,7 @@ def part_to_message_block(
89
98
input = part .function_call .args ,
90
99
type = "tool_use" ,
91
100
)
92
- if part .function_response :
101
+ elif part .function_response :
93
102
content = ""
94
103
if (
95
104
"result" in part .function_response .response
@@ -105,15 +114,45 @@ def part_to_message_block(
105
114
content = content ,
106
115
is_error = False ,
107
116
)
108
- raise NotImplementedError ("Not supported yet." )
117
+ elif _is_image_part (part ):
118
+ data = base64 .b64encode (part .inline_data .data ).decode ()
119
+ return anthropic_types .ImageBlockParam (
120
+ type = "image" ,
121
+ source = dict (
122
+ type = "base64" , media_type = part .inline_data .mime_type , data = data
123
+ ),
124
+ )
125
+ elif part .executable_code :
126
+ return anthropic_types .TextBlockParam (
127
+ type = "text" ,
128
+ text = "Code:```python\n " + part .executable_code .code + "\n ```" ,
129
+ )
130
+ elif part .code_execution_result :
131
+ return anthropic_types .TextBlockParam (
132
+ text = "Execution Result:```code_output\n "
133
+ + part .code_execution_result .output
134
+ + "\n ```" ,
135
+ type = "text" ,
136
+ )
137
+
138
+ raise NotImplementedError (f"Not supported yet: { part } " )
109
139
110
140
111
141
def content_to_message_param (
112
142
content : types .Content ,
113
143
) -> anthropic_types .MessageParam :
144
+ message_block = []
145
+ for part in content .parts or []:
146
+ # Image data is not supported in Claude for model turns.
147
+ if _is_image_part (part ):
148
+ logger .warning ("Image data is not supported in Claude for model turns." )
149
+ continue
150
+
151
+ message_block .append (part_to_message_block (part ))
152
+
114
153
return {
115
154
"role" : to_claude_role (content .role ),
116
- "content" : [ part_to_message_block ( part ) for part in content . parts or []] ,
155
+ "content" : message_block ,
117
156
}
118
157
119
158
0 commit comments