From a8885e992fb1968161fa563cadd031cb289c88d2 Mon Sep 17 00:00:00 2001 From: cyhhao Date: Wed, 26 Nov 2025 22:48:54 +0800 Subject: [PATCH] fix(genai): use tool_call_schema to exclude injected arguments When converting tools to Google Genai function declarations, use `tool.tool_call_schema` instead of `tool.args_schema` to properly exclude injected arguments like `ToolRuntime`, `InjectedState`, and `InjectedStore`. The `args_schema` includes all parameters including injected ones, which causes `PydanticInvalidForJsonSchema` errors when trying to generate JSON schema for types like `ToolRuntime` that contain `Callable` fields. The `tool_call_schema` property (from langchain-core BaseTool) already handles filtering out injected arguments, making it the correct schema to use when binding tools to models. Fixes issue where tools using `ToolRuntime` parameter fail with: `Cannot generate a JsonSchema for core_schema.CallableSchema` --- .../langchain_google_genai/_function_utils.py | 20 +++++++++++-------- 1 file changed, 12 insertions(+), 8 deletions(-) diff --git a/libs/genai/langchain_google_genai/_function_utils.py b/libs/genai/langchain_google_genai/_function_utils.py index e2fc82a9d..006b2351b 100644 --- a/libs/genai/langchain_google_genai/_function_utils.py +++ b/libs/genai/langchain_google_genai/_function_utils.py @@ -259,16 +259,20 @@ def _format_base_tool_to_function_declaration( ), ) - if isinstance(tool.args_schema, dict): - schema = tool.args_schema - elif issubclass(tool.args_schema, BaseModel): - schema = tool.args_schema.model_json_schema() - elif issubclass(tool.args_schema, BaseModelV1): - schema = tool.args_schema.schema() + # Use tool_call_schema instead of args_schema to properly exclude + # injected arguments (e.g., ToolRuntime, InjectedState, InjectedStore) + # that should not be exposed to the model. + tool_schema = tool.tool_call_schema + if isinstance(tool_schema, dict): + schema = tool_schema + elif issubclass(tool_schema, BaseModel): + schema = tool_schema.model_json_schema() + elif issubclass(tool_schema, BaseModelV1): + schema = tool_schema.schema() else: msg = ( - "args_schema must be a Pydantic BaseModel or JSON schema, " - f"got {tool.args_schema}." + "tool_call_schema must be a Pydantic BaseModel or JSON schema, " + f"got {tool_schema}." ) raise NotImplementedError(msg) parameters = _dict_to_gapic_schema(schema)