fix(genai): use tool_call_schema to exclude injected arguments #1387
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Problem
When using tools that have injected parameters like
ToolRuntime,InjectedState, orInjectedStore, Google Genai's tool conversion fails with:This happens because
_format_base_tool_to_function_declarationusestool.args_schemawhich includes all parameters, including injected ones that containCallabletypes that Pydantic cannot serialize to JSON schema.Solution
Use
tool.tool_call_schemainstead oftool.args_schema. Thetool_call_schemaproperty (from langchain-core'sBaseTool) already handles filtering out injected arguments, making it the correct schema to use when binding tools to models.This is consistent with how
langchain-core'sconvert_to_openai_functionhandles tools - it usestool.tool_call_schema(see_format_tool_to_openai_function).Testing
Tested locally with a tool using
ToolRuntimeparameter - the error is resolved and the tool works correctly with Google Gemini models.Related
BaseTool.tool_call_schemaproperty: filters out injected argumentstool_call_schema