Implementation:Openai Openai python Pydantic Function Tool
| Knowledge Sources | |
|---|---|
| Domains | NLP, Data_Extraction |
| Last Updated | 2026-02-15 00:00 GMT |
Overview
Concrete tool for converting Pydantic models into API-compatible tool definitions and JSON schemas provided by the OpenAI Python SDK.
Description
The pydantic_function_tool() function converts a Pydantic model class into a ChatCompletionToolParam dictionary suitable for the tools parameter. Internally it uses to_strict_json_schema() to convert the model's fields into a strict JSON schema. The function name defaults to the class name, and the description defaults to the class docstring.
Usage
Use when defining tools for function calling or when you want to convert a Pydantic model to a tool definition without manually writing JSON schemas.
Code Reference
Source Location
- Repository: openai-python
- File: src/openai/lib/_tools.py
- Lines: L40-66
- File: src/openai/lib/_pydantic.py
- Lines: L1-155 (to_strict_json_schema)
Signature
def pydantic_function_tool(
model: type[pydantic.BaseModel],
*,
name: str | None = None,
description: str | None = None,
) -> ChatCompletionToolParam:
"""
Converts a Pydantic model into a function tool definition.
Args:
model: Pydantic model class defining the tool's parameters.
name: Override tool name (defaults to model class name).
description: Override description (defaults to model docstring).
Returns:
ChatCompletionToolParam dict with type="function" and strict JSON schema.
"""
Import
from openai.lib import pydantic_function_tool
I/O Contract
Inputs
| Name | Type | Required | Description |
|---|---|---|---|
| model | type[pydantic.BaseModel] | Yes | Pydantic model defining tool parameters |
| name | str or None | No | Custom tool name (default: class name) |
| description | str or None | No | Custom description (default: class docstring) |
Outputs
| Name | Type | Description |
|---|---|---|
| tool_param | ChatCompletionToolParam | Dict with type="function", function name, description, and strict JSON schema |
Usage Examples
Basic Tool Definition
from pydantic import BaseModel
from openai.lib import pydantic_function_tool
class GetWeather(BaseModel):
"""Get the current weather for a location."""
city: str
unit: str = "celsius"
tool = pydantic_function_tool(GetWeather)
# Result:
# {
# "type": "function",
# "function": {
# "name": "GetWeather",
# "description": "Get the current weather for a location.",
# "parameters": {"type": "object", "properties": {...}, "required": [...]}
# }
# }
With Custom Name
tool = pydantic_function_tool(
GetWeather,
name="weather_lookup",
description="Look up weather conditions",
)
Used with Chat Completions
from openai import OpenAI
from openai.lib import pydantic_function_tool
from pydantic import BaseModel
class SearchWeb(BaseModel):
"""Search the web for information."""
query: str
max_results: int = 5
client = OpenAI()
response = client.chat.completions.create(
model="gpt-4o",
messages=[{"role": "user", "content": "Find info about Python 3.12"}],
tools=[pydantic_function_tool(SearchWeb)],
)