Skip to content

genai: fix function_utils to parse object/array json schemas properly #715

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Feb 4, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading