Skip to content

Commit

Permalink
genai: fix function_utils to parse object/array json schemas properly (
Browse files Browse the repository at this point in the history
  • Loading branch information
RazCrimson authored Feb 4, 2025
1 parent 3915fc1 commit a0027c8
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 6 deletions.
10 changes: 9 additions & 1 deletion libs/genai/langchain_google_genai/_function_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -301,10 +301,18 @@ def _get_properties_from_schema(schema: Dict) -> Dict[str, Any]:
continue
properties_item: Dict[str, Union[str, int, Dict, List]] = {}
if v.get("type") or v.get("anyOf") or v.get("type_"):
properties_item["type_"] = _get_type_from_schema(v)
item_type_ = _get_type_from_schema(v)
properties_item["type_"] = item_type_
if _is_nullable_schema(v):
properties_item["nullable"] = True

# Replace `v` with chosen definition for array / object json types
any_of_types = v.get("anyOf")
if any_of_types and item_type_ in [glm.Type.ARRAY, glm.Type.OBJECT]:
json_type_ = "array" if item_type_ == glm.Type.ARRAY else "object"
# Use Index -1 for consistency with `_get_nullable_type_from_schema`
v = [val for val in any_of_types if val.get("type") == json_type_][-1]

if v.get("enum"):
properties_item["enum"] = v["enum"]

Expand Down
5 changes: 0 additions & 5 deletions libs/genai/tests/unit_tests/test_function_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -84,11 +84,6 @@ def possibly_none_list(
# Convert to OpenAI tool
oai_tool = convert_to_openai_tool(possibly_none_list)

# Manually assign the 'items' type in the parameters
oai_tool["function"]["parameters"]["properties"]["items"]["items"] = {
"type": "string"
}

# Convert to GenAI, then to dict
genai_tool = convert_to_genai_function_declarations([oai_tool])
genai_tool_dict = tool_to_dict(genai_tool)
Expand Down

0 comments on commit a0027c8

Please sign in to comment.