8000 Update the 'type' value on the items/properties nested structures for… · Rakshasv18/adk-python@4289e02 · GitHub
[go: up one dir, main page]

Skip to content

Commit 4289e02

Browse files
ankursharmascopybara-github
authored andcommitted
Update the 'type' value on the items/properties nested structures for Anthropic models to adhere to JSON schema.
PiperOrigin-RevId: 754154596
1 parent bcf1deb commit 4289e02

File tree

1 file changed

+20
-2
lines changed

1 file changed

+20
-2
lines changed

src/google/adk/models/anthropic_llm.py

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
from functools import cached_property
2020
import logging
2121
import os
22+
from typing import Any
2223
from typing import AsyncGenerator
2324
from typing import Generator
2425
from typing import Iterable
@@ -151,6 +152,24 @@ def message_to_generate_content_response(
151152
)
152153

153154

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+
154173
def function_declaration_to_tool_param(
155174
function_declaration: types.FunctionDeclaration,
156175
) -> anthropic_types.ToolParam:
@@ -163,8 +182,7 @@ def function_declaration_to_tool_param(
163182
):
164183
for key, value in function_declaration.parameters.properties.items():
165184
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)
168186
properties[key] = value_dict
169187

170188
return anthropic_types.ToolParam(

0 commit comments

Comments
 (0)
0