Skip to content

Commit a0027c8

Browse files
authored
genai: fix function_utils to parse object/array json schemas properly (#715)
1 parent 3915fc1 commit a0027c8

File tree

2 files changed

+9
-6
lines changed

2 files changed

+9
-6
lines changed

libs/genai/langchain_google_genai/_function_utils.py

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -301,10 +301,18 @@ def _get_properties_from_schema(schema: Dict) -> Dict[str, Any]:
301301
continue
302302
properties_item: Dict[str, Union[str, int, Dict, List]] = {}
303303
if v.get("type") or v.get("anyOf") or v.get("type_"):
304-
properties_item["type_"] = _get_type_from_schema(v)
304+
item_type_ = _get_type_from_schema(v)
305+
properties_item["type_"] = item_type_
305306
if _is_nullable_schema(v):
306307
properties_item["nullable"] = True
307308

309+
# Replace `v` with chosen definition for array / object json types
310+
any_of_types = v.get("anyOf")
311+
if any_of_types and item_type_ in [glm.Type.ARRAY, glm.Type.OBJECT]:
312+
json_type_ = "array" if item_type_ == glm.Type.ARRAY else "object"
313+
# Use Index -1 for consistency with `_get_nullable_type_from_schema`
314+
v = [val for val in any_of_types if val.get("type") == json_type_][-1]
315+
308316
if v.get("enum"):
309317
properties_item["enum"] = v["enum"]
310318

libs/genai/tests/unit_tests/test_function_utils.py

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -84,11 +84,6 @@ def possibly_none_list(
8484
# Convert to OpenAI tool
8585
oai_tool = convert_to_openai_tool(possibly_none_list)
8686

87-
# Manually assign the 'items' type in the parameters
88-
oai_tool["function"]["parameters"]["properties"]["items"]["items"] = {
89-
"type": "string"
90-
}
91-
9287
# Convert to GenAI, then to dict
9388
genai_tool = convert_to_genai_function_declarations([oai_tool])
9489
genai_tool_dict = tool_to_dict(genai_tool)

0 commit comments

Comments
 (0)