convert_to_openai_tool#
- langchain_core.utils.function_calling.convert_to_openai_tool(tool: dict[str, Any] | type[BaseModel] | Callable | BaseTool, *, strict: bool | None = None) dict[str, Any] [source]#
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
- Parameters:
tool (Union[dict[str, Any], type[BaseModel], Callable, BaseTool]) – 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 (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 tool definition.
- Returns:
A dict version of the passed in tool which is compatible with the OpenAI tool-calling API.
- 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_tool