Skip to content

Commit b91d11d

Browse files
committed
genai: Fix handling of optional arrays in tool input
1 parent 49d3844 commit b91d11d

File tree

2 files changed

+17
-0
lines changed

2 files changed

+17
-0
lines changed

libs/genai/langchain_google_genai/_function_utils.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -314,6 +314,14 @@ def _get_properties_from_schema(schema: Dict) -> Dict[str, Any]:
314314

315315
if properties_item.get("type_") == glm.Type.ARRAY and v.get("items"):
316316
properties_item["items"] = _get_items_from_schema_any(v.get("items"))
317+
elif properties_item.get("type_") == glm.Type.ARRAY and v.get("anyOf"):
318+
types_with_items = [t for t in v.get("anyOf") if t.get("items")]
319+
if len(types_with_items) > 1:
320+
logger.warning(
321+
"Only first value for 'anyOf' key is supported in array types."
322+
f"Got {len(types_with_items)} types, using first one: {types_with_items[0]}"
323+
)
324+
properties_item["items"] = _get_items_from_schema_any(types_with_items[0]['items'])
317325

318326
if properties_item.get("type_") == glm.Type.OBJECT:
319327
if (

libs/genai/tests/unit_tests/test_function_utils.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -525,3 +525,12 @@ class MyModel(BaseModel):
525525
gapic_tool = convert_to_genai_function_declarations([MyModel])
526526
tool_dict = tool_to_dict(gapic_tool)
527527
assert gapic_tool == convert_to_genai_function_declarations([tool_dict])
528+
529+
530+
def test_tool_input_can_have_optional_arrays() -> None:
531+
class ExampleToolInput(BaseModel):
532+
numbers: Optional[List[str]] = Field()
533+
534+
gapic_tool = convert_to_genai_function_declarations([ExampleToolInput])
535+
assert gapic_tool.function_declarations[0].parameters.properties.get('numbers').items.type_ == 1
536+

0 commit comments

Comments
 (0)