19
19
from functools import cached_property
20
20
import logging
21
21
import os
22
+ from typing import Any
22
23
from typing import AsyncGenerator
23
24
from typing import Generator
24
25
from typing import Iterable
@@ -151,6 +152,24 @@ def message_to_generate_content_response(
151
152
)
152
153
153
154
155
+ def _update_type_string (value_dict : dict [str , Any ]):
156
+ """Updates 'type' field to expected JSON schema format."""
157
+ if "type" in value_dict :
158
+ value_dict ["type" ] = value_dict ["type" ].lower ()
159
+
160
+ if "items" in value_dict :
161
+ # 'type' field could exist for items as well, this would be the case if
162
+ # items represent primitive types.
163
+ _update_type_string (value_dict ["items" ])
164
+
165
+ if "properties" in value_dict ["items" ]:
166
+ # There could be properties as well on the items, especially if the items
167
+ # are complex object themselves. We recursively traverse each individual
168
+ # property as well and fix the "type" value.
169
+ for _ , value in value_dict ["items" ]["properties" ].items ():
170
+ _update_type_string (value )
171
+
172
+
154
173
def function_declaration_to_tool_param (
155
174
function_declaration : types .FunctionDeclaration ,
156
175
) -> anthropic_types .ToolParam :
@@ -163,8 +182,7 @@ def function_declaration_to_tool_param(
163
182
):
164
183
for key , value in function_declaration .parameters .properties .items ():
165
184
value_dict = value .model_dump (exclude_none = True )
166
- if "type" in value_dict :
167
- value_dict ["type" ] = value_dict ["type" ].lower ()
185
+ _update_type_string (value_dict )
168
186
properties [key ] = value_dict
169
187
170
188
return anthropic_types .ToolParam (
0 commit comments