Skip to content
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

genai: Fix handling of optional arrays in tool input #661

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
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
8 changes: 8 additions & 0 deletions libs/genai/langchain_google_genai/_function_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -314,6 +314,14 @@

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

View workflow job for this annotation

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

View workflow job for this annotation

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

View workflow job for this annotation

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

View workflow job for this annotation

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 and v.get("properties"):
properties_item["properties"] = _get_properties_from_schema_any(
Expand Down
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
Expand Up @@ -309,3 +309,12 @@
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 316 in libs/genai/tests/unit_tests/test_function_utils.py

View workflow job for this annotation

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

Ruff (F821)

tests/unit_tests/test_function_utils.py:316:27: F821 Undefined name `List`

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

View workflow job for this annotation

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

Ruff (F821)

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

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

View workflow job for this annotation

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

Ruff (F821)

tests/unit_tests/test_function_utils.py:316:27: F821 Undefined name `List`

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

View workflow job for this annotation

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

Ruff (F821)

tests/unit_tests/test_function_utils.py:316: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 319 in libs/genai/tests/unit_tests/test_function_utils.py

View workflow job for this annotation

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

Ruff (E501)

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

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

View workflow job for this annotation

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

Ruff (E501)

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

Loading