Skip to content

Utils

langchain_core.utils

Utility functions for LangChain.

These functions do not depend on any other LangChain module.

langchain_core.utils.function_calling

Methods for creating function specs in the style of OpenAI Functions.

FUNCTION DESCRIPTION
convert_to_json_schema

Convert a schema representation to a JSON schema.

convert_to_openai_tool

Convert a tool-like object to an OpenAI tool schema.

convert_to_json_schema

convert_to_json_schema(
    schema: dict[str, Any] | type[BaseModel] | Callable | BaseTool,
    *,
    strict: bool | None = None,
) -> dict[str, Any]

Convert a schema representation to a JSON schema.

PARAMETER DESCRIPTION
schema

The schema to convert.

TYPE: dict[str, Any] | type[BaseModel] | Callable | BaseTool

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 function definition.

TYPE: bool | None DEFAULT: None

RAISES DESCRIPTION
ValueError

If the input is not a valid OpenAI-format tool.

RETURNS DESCRIPTION
dict[str, Any]

A JSON schema representation of the input schema.

convert_to_openai_tool

convert_to_openai_tool(
    tool: dict[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

PARAMETER DESCRIPTION
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.

TYPE: dict[str, Any] | type[BaseModel] | Callable | BaseTool

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.

TYPE: bool | None DEFAULT: None

RETURNS DESCRIPTION
dict[str, Any]

A dict version of the passed in tool which is compatible with the

dict[str, Any]

OpenAI tool-calling API.

Behavior changed in 0.3.16

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

Behavior changed in 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".

Behavior changed in 0.3.63

Added support for OpenAI's image generation built-in tool.