Skip to content

fix(genai): emit items schema for List[Any] tool parameters#1805

Open
obchain (obchain) wants to merge 1 commit into
langchain-ai:mainfrom
obchain:fix/genai-list-any-tool-items
Open

fix(genai): emit items schema for List[Any] tool parameters#1805
obchain (obchain) wants to merge 1 commit into
langchain-ai:mainfrom
obchain:fix/genai-list-any-tool-items

Conversation

@obchain

Copy link
Copy Markdown

Description

ChatGoogleGenerativeAI invocations with tools that take List[Any] parameters fail at runtime with:

400 INVALID_ARGUMENT: GenerateContentRequest.tools[N].function_declarations[N].parameters.properties[field].items: missing field

Root cause: Pydantic v2 emits a valid "items": {} for an untyped array. In _get_properties_from_schema (libs/genai/langchain_google_genai/_function_utils.py) the guard was v.get("items"), which is falsy for {}, so the items field was dropped from the FunctionDeclaration. Gemini rejects array fields without items.

langchain-google-vertexai already uses a key-existence check ("items" in property); this PR aligns langchain-google-genai with that. An empty items schema is now forwarded to _get_items_from_schema_any, which defaults the inner type to STRING — the minimal payload Gemini accepts for an untyped array.

Relevant issues

Fixes #1768

Type

🐛 Bug Fix

Changes

  • libs/genai/langchain_google_genai/_function_utils.py: switch v.get("items") to "items" in v at the array-property check.
  • libs/genai/tests/unit_tests/test_function_utils.py: add test_tool_with_list_any_param_emits_items regression covering the List[Any]items shape.

Testing

  • uv run --group test pytest tests/unit_tests/test_function_utils.py — 28 passed.
  • Verified the new test fails on main (stashing the source change) and passes with the fix.
  • make lint (ruff) and mypy clean for the touched files.

Pydantic v2 emits a valid `"items": {}` for `List[Any]` array fields.
`_get_properties_from_schema` guarded with `v.get("items")`, which is
falsy for `{}`, so the `items` field was dropped from the resulting
FunctionDeclaration. Gemini then rejects the request with
`400 INVALID_ARGUMENT: ...properties[...].items: missing field`.

Switch to `"items" in v` (matching the equivalent check in
`langchain-google-vertexai`) so an empty items schema is still
forwarded to `_get_items_from_schema_any`, which defaults the inner
type to STRING.

Fixes langchain-ai#1768
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Tools with List[Any] parameters fail with 400 errors

1 participant