convert_to_openai_function#

langchain_core.utils.function_calling.convert_to_openai_function(function: dict[str, Any] | type | Callable | BaseTool, *, strict: bool | None = None) dict[str, Any][source]#

Convert a raw function/class to an OpenAI function. :param function: A dictionary, Pydantic BaseModel class, TypedDict class, a LangChain

Tool object, or a Python function. 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.

Parameters:
  • strict (Optional[bool]) – 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.

  • function (Union[dict[str, Any], type, Callable, BaseTool])

Returns:

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

Raises:

ValueError – If function is not in a supported format.

Return type:

dict[str, Any]

Changed in version 0.2.29: strict arg added.

Changed in version 0.3.13: Support for Anthropic format tools added.

Changed in version 0.3.14: Support for Amazon Bedrock Converse format tools added.

Changed in version 0.3.16: ‘description’ and ‘parameters’ keys are now optional. Only ‘name’ is required and guaranteed to be part of the output.

Examples using convert_to_openai_function