Home / Function/ convert_to_openai_tool() — langchain Function Reference

convert_to_openai_tool() — langchain Function Reference

Architecture documentation for the convert_to_openai_tool() function in function_calling.py from the langchain codebase.

Entity Profile

Dependency Diagram

graph TD
  985b3428_1598_3c9f_dd21_7949746dfdbf["convert_to_openai_tool()"]
  344b2838_87a8_d5dc_b550_fdb443ff6c4e["function_calling.py"]
  985b3428_1598_3c9f_dd21_7949746dfdbf -->|defined in| 344b2838_87a8_d5dc_b550_fdb443ff6c4e
  27d86494_848e_9b3b_d941_5d556a5b249d["convert_to_json_schema()"]
  27d86494_848e_9b3b_d941_5d556a5b249d -->|calls| 985b3428_1598_3c9f_dd21_7949746dfdbf
  8dd6ec0a_3761_ea6f_0c59_c034b831f0df["convert_to_openai_function()"]
  985b3428_1598_3c9f_dd21_7949746dfdbf -->|calls| 8dd6ec0a_3761_ea6f_0c59_c034b831f0df
  style 985b3428_1598_3c9f_dd21_7949746dfdbf fill:#6366f1,stroke:#818cf8,color:#fff

Relationship Graph

Source Code

libs/core/langchain_core/utils/function_calling.py lines 498–557

def convert_to_openai_tool(
    tool: Mapping[str, Any] | type[BaseModel] | Callable | BaseTool,
    *,
    strict: bool | None = None,
) -> dict[str, Any]:
    """Convert a tool-like object to an OpenAI tool schema.

    [OpenAI tool schema reference](https://platform.openai.com/docs/api-reference/chat/create#chat-create-tools)

    Args:
        tool: Either a dictionary, a `pydantic.BaseModel` class, Python function, or
            `BaseTool`.

            If a dictionary is passed in, it is assumed to already be a valid OpenAI
            function, a JSON schema with top-level `title` key specified, an Anthropic
            format tool, or an Amazon Bedrock Converse format tool.
        strict: If `True`, model output is guaranteed to exactly match the JSON Schema
            provided in the function definition.

            If `None`, `strict` argument will not be included in tool definition.

    Returns:
        A dict version of the passed in tool which is compatible with the OpenAI
            tool-calling API.

    !!! warning "Behavior changed in `langchain-core` 0.3.16"

        `description` and `parameters` keys are now optional. Only `name` is
        required and guaranteed to be part of the output.

    !!! warning "Behavior changed in `langchain-core` 0.3.44"

        Return OpenAI Responses API-style tools unchanged. This includes
        any dict with `"type"` in `"file_search"`, `"function"`,
        `"computer_use_preview"`, `"web_search_preview"`.

    !!! warning "Behavior changed in `langchain-core` 0.3.63"

        Added support for OpenAI's image generation built-in tool.
    """
    # Import locally to prevent circular import
    from langchain_core.tools import Tool  # noqa: PLC0415

    if isinstance(tool, dict):
        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"):
            return tool
    if isinstance(tool, Tool) and (tool.metadata or {}).get("type") == "custom_tool":
        oai_tool = {
            "type": "custom",
            "name": tool.name,
            "description": tool.description,
        }
        if tool.metadata is not None and "format" in tool.metadata:
            oai_tool["format"] = tool.metadata["format"]
        return oai_tool
    oai_function = convert_to_openai_function(tool, strict=strict)
    return {"type": "function", "function": oai_function}

Subdomains

Frequently Asked Questions

What does convert_to_openai_tool() do?
convert_to_openai_tool() is a function in the langchain codebase, defined in libs/core/langchain_core/utils/function_calling.py.
Where is convert_to_openai_tool() defined?
convert_to_openai_tool() is defined in libs/core/langchain_core/utils/function_calling.py at line 498.
What does convert_to_openai_tool() call?
convert_to_openai_tool() calls 1 function(s): convert_to_openai_function.
What calls convert_to_openai_tool()?
convert_to_openai_tool() is called by 1 function(s): convert_to_json_schema.

Analyze Your Own Codebase

Get architecture documentation, dependency graphs, and domain analysis for your codebase in minutes.

Try Supermodel Free