Skip to content

Commit

Permalink
genai: Fix handling of optional arrays in tool input
Browse files Browse the repository at this point in the history
  • Loading branch information
piotr-rudnik committed Dec 22, 2024
1 parent 7382d23 commit 2a723c2
Show file tree
Hide file tree
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
Expand Up @@ -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

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 @@ 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 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)

0 comments on commit 2a723c2

Please sign in to comment.