Skip to content

Commit

Permalink
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
genai: Fix handling of optional arrays in tool input
Browse files Browse the repository at this point in the history
piotr-rudnik committed Jan 12, 2025

Verified

This commit was created on GitHub.com and signed with GitHub’s verified signature. The key has expired.
1 parent 49d3844 commit b91d11d
Showing 2 changed files with 17 additions and 0 deletions.
8 changes: 8 additions & 0 deletions libs/genai/langchain_google_genai/_function_utils.py
Original file line number Diff line number Diff line change
@@ -314,6 +314,14 @@ def _get_properties_from_schema(schema: Dict) -> Dict[str, Any]:

if properties_item.get("type_") == glm.Type.ARRAY and v.get("items"):
properties_item["items"] = _get_items_from_schema_any(v.get("items"))
elif properties_item.get("type_") == glm.Type.ARRAY and v.get("anyOf"):
types_with_items = [t for t in v.get("anyOf") if t.get("items")]
if len(types_with_items) > 1:
logger.warning(
"Only first value for 'anyOf' key is supported in array types."
f"Got {len(types_with_items)} types, using first one: {types_with_items[0]}"

Check failure on line 322 in libs/genai/langchain_google_genai/_function_utils.py

GitHub Actions / cd libs/genai / - / make lint #3.9

Ruff (E501)

langchain_google_genai/_function_utils.py:322:89: E501 Line too long (96 > 88)

Check failure on line 322 in libs/genai/langchain_google_genai/_function_utils.py

GitHub Actions / cd libs/genai / - / make lint #3.12

Ruff (E501)

langchain_google_genai/_function_utils.py:322:89: E501 Line too long (96 > 88)
)
properties_item["items"] = _get_items_from_schema_any(types_with_items[0]['items'])

Check failure on line 324 in libs/genai/langchain_google_genai/_function_utils.py

GitHub Actions / cd libs/genai / - / make lint #3.9

Ruff (E501)

langchain_google_genai/_function_utils.py:324:89: E501 Line too long (95 > 88)

Check failure on line 324 in libs/genai/langchain_google_genai/_function_utils.py

GitHub Actions / cd libs/genai / - / make lint #3.12

Ruff (E501)

langchain_google_genai/_function_utils.py:324:89: E501 Line too long (95 > 88)

if properties_item.get("type_") == glm.Type.OBJECT:
if (
9 changes: 9 additions & 0 deletions libs/genai/tests/unit_tests/test_function_utils.py
Original file line number Diff line number Diff line change
@@ -525,3 +525,12 @@ class MyModel(BaseModel):
gapic_tool = convert_to_genai_function_declarations([MyModel])
tool_dict = tool_to_dict(gapic_tool)
assert gapic_tool == convert_to_genai_function_declarations([tool_dict])


def test_tool_input_can_have_optional_arrays() -> None:
class ExampleToolInput(BaseModel):
numbers: Optional[List[str]] = Field()

Check failure on line 532 in libs/genai/tests/unit_tests/test_function_utils.py

GitHub Actions / cd libs/genai / - / make lint #3.9

Ruff (F821)

tests/unit_tests/test_function_utils.py:532:40: F821 Undefined name `Field`

Check failure on line 532 in libs/genai/tests/unit_tests/test_function_utils.py

GitHub Actions / cd libs/genai / - / make lint #3.12

Ruff (F821)

tests/unit_tests/test_function_utils.py:532:40: F821 Undefined name `Field`

gapic_tool = convert_to_genai_function_declarations([ExampleToolInput])
assert gapic_tool.function_declarations[0].parameters.properties.get('numbers').items.type_ == 1

Check failure on line 535 in libs/genai/tests/unit_tests/test_function_utils.py

GitHub Actions / cd libs/genai / - / make lint #3.9

Ruff (E501)

tests/unit_tests/test_function_utils.py:535:89: E501 Line too long (100 > 88)

Check failure on line 535 in libs/genai/tests/unit_tests/test_function_utils.py

GitHub Actions / cd libs/genai / - / make lint #3.12

Ruff (E501)

tests/unit_tests/test_function_utils.py:535:89: E501 Line too long (100 > 88)

0 comments on commit b91d11d

Please sign in to comment.