From e6633a7efb10c0de3df4ff925d5a2f3476e1ff7b Mon Sep 17 00:00:00 2001 From: Eugene Yurtsev Date: Thu, 29 May 2025 13:13:21 -0400 Subject: [PATCH] langchain-core: Add image_generation tool to list of known openai tools (#31396) Add image generation tool to the list of well known tools. This is needed for changes in the ChatOpenAI client. TODO: Some of this logic needs to be moved from core directly into the client as changes in core should not be required to add a new tool to the openai chat client. --- .../langchain_core/utils/function_calling.py | 26 ++++++++++++++----- 1 file changed, 19 insertions(+), 7 deletions(-) diff --git a/libs/core/langchain_core/utils/function_calling.py b/libs/core/langchain_core/utils/function_calling.py index 61b705e4ace..0c3e2b88cdb 100644 --- a/libs/core/langchain_core/utils/function_calling.py +++ b/libs/core/langchain_core/utils/function_calling.py @@ -506,6 +506,20 @@ def convert_to_openai_function( return oai_function +# List of well known tools supported by OpenAI's chat models or responses API. +# These tools are not expected to be supported by other chat model providers +# that conform to the OpenAI function-calling API. +_WellKnownOpenAITools = ( + "function", + "file_search", + "computer_use_preview", + "code_interpreter", + "mcp", + "image_generation", + "web_search_preview", +) + + def convert_to_openai_tool( tool: Union[dict[str, Any], type[BaseModel], Callable, BaseTool], *, @@ -558,15 +572,13 @@ def convert_to_openai_tool( .. versionchanged:: 0.3.61 Added support for OpenAI's built-in code interpreter and remote MCP tools. + + .. versionchanged:: 0.3.63 + + Added support for OpenAI's image generation built-in tool. """ if isinstance(tool, dict): - if tool.get("type") in ( - "function", - "file_search", - "computer_use_preview", - "code_interpreter", - "mcp", - ): + if tool.get("type") in _WellKnownOpenAITools: return tool # As of 03.12.25 can be "web_search_preview" or "web_search_preview_2025_03_11" if (tool.get("type") or "").startswith("web_search_preview"):