Home / Function/ _format_tool_to_openai_function() — langchain Function Reference

_format_tool_to_openai_function() — langchain Function Reference

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

Entity Profile

Dependency Diagram

graph TD
  4776d470_7c16_396f_5ac7_df8d1e1eeb46["_format_tool_to_openai_function()"]
  344b2838_87a8_d5dc_b550_fdb443ff6c4e["function_calling.py"]
  4776d470_7c16_396f_5ac7_df8d1e1eeb46 -->|defined in| 344b2838_87a8_d5dc_b550_fdb443ff6c4e
  8dd6ec0a_3761_ea6f_0c59_c034b831f0df["convert_to_openai_function()"]
  8dd6ec0a_3761_ea6f_0c59_c034b831f0df -->|calls| 4776d470_7c16_396f_5ac7_df8d1e1eeb46
  0edd121c_051a_5776_8748_581f03d9e57c["_convert_json_schema_to_openai_function()"]
  4776d470_7c16_396f_5ac7_df8d1e1eeb46 -->|calls| 0edd121c_051a_5776_8748_581f03d9e57c
  ea8becf8_0367_1814_460c_decdc8cfa61d["_convert_pydantic_to_openai_function()"]
  4776d470_7c16_396f_5ac7_df8d1e1eeb46 -->|calls| ea8becf8_0367_1814_460c_decdc8cfa61d
  style 4776d470_7c16_396f_5ac7_df8d1e1eeb46 fill:#6366f1,stroke:#818cf8,color:#fff

Relationship Graph

Source Code

libs/core/langchain_core/utils/function_calling.py lines 314–358

def _format_tool_to_openai_function(tool: BaseTool) -> FunctionDescription:
    """Format tool into the OpenAI function API.

    Args:
        tool: The tool to format.

    Raises:
        ValueError: If the tool call schema is not supported.

    Returns:
        The function description.
    """
    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):
            return _convert_json_schema_to_openai_function(
                tool.tool_call_schema, name=tool.name, description=tool.description
            )
        if issubclass(tool.tool_call_schema, (BaseModel, BaseModelV1)):
            return _convert_pydantic_to_openai_function(
                tool.tool_call_schema, name=tool.name, description=tool.description
            )
        error_msg = (
            f"Unsupported tool call schema: {tool.tool_call_schema}. "
            "Tool call schema must be a JSON schema dict or a Pydantic model."
        )
        raise ValueError(error_msg)
    return {
        "name": tool.name,
        "description": tool.description,
        "parameters": {
            # This is a hack to get around the fact that some tools
            # do not expose an args_schema, and expect an argument
            # which is a string.
            # And Open AI does not support an array type for the
            # parameters.
            "properties": {
                "__arg1": {"title": "__arg1", "type": "string"},
            },
            "required": ["__arg1"],
            "type": "object",
        },
    }

Subdomains

Frequently Asked Questions

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

Analyze Your Own Codebase

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

Try Supermodel Free