From c257b68466896f82b1cab8ea5d51eff362996d05 Mon Sep 17 00:00:00 2001 From: Sydney Runkle Date: Tue, 21 Apr 2026 09:45:16 -0400 Subject: [PATCH] perf(core): avoid repeated tool_call_schema access in _format_tool_to_openai_function --- libs/core/langchain_core/utils/function_calling.py | 13 +++++++------ 1 file changed, 7 insertions(+), 6 deletions(-) diff --git a/libs/core/langchain_core/utils/function_calling.py b/libs/core/langchain_core/utils/function_calling.py index 0fefd98b0eb..c2b59fc254c 100644 --- a/libs/core/langchain_core/utils/function_calling.py +++ b/libs/core/langchain_core/utils/function_calling.py @@ -340,17 +340,18 @@ def _format_tool_to_openai_function(tool: BaseTool) -> FunctionDescription: is_simple_oai_tool = ( isinstance(tool, langchain_core.tools.simple.Tool) and not tool.args_schema ) - if tool.tool_call_schema and not is_simple_oai_tool: - if isinstance(tool.tool_call_schema, dict): + schema = tool.tool_call_schema + if schema and not is_simple_oai_tool: + if isinstance(schema, dict): return _convert_json_schema_to_openai_function( - tool.tool_call_schema, name=tool.name, description=tool.description + schema, name=tool.name, description=tool.description ) - if issubclass(tool.tool_call_schema, (BaseModel, BaseModelV1)): + if issubclass(schema, (BaseModel, BaseModelV1)): return _convert_pydantic_to_openai_function( - tool.tool_call_schema, name=tool.name, description=tool.description + schema, name=tool.name, description=tool.description ) error_msg = ( - f"Unsupported tool call schema: {tool.tool_call_schema}. " + f"Unsupported tool call schema: {schema}. " "Tool call schema must be a JSON schema dict or a Pydantic model." ) raise ValueError(error_msg)