Skip to content

Commit bea1605

Browse files
Kludexdmontagu
andauthored
Add OpenAI Responses API (#1256)
Co-authored-by: David Montague <[email protected]>
1 parent 867fc7a commit bea1605

21 files changed

+2787
-21
lines changed

docs/models.md

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -128,6 +128,29 @@ agent = Agent(model)
128128
...
129129
```
130130

131+
### OpenAI Responses API
132+
133+
PydanticAI also supports OpenAI's [Responses API](https://platform.openai.com/docs/api-reference/responses) through the [`OpenAIResponsesModel`][pydantic_ai.models.openai.OpenAIResponsesModel] class.
134+
135+
The Responses API has built-in tools that you can use instead of building your own:
136+
- [Web search](https://platform.openai.com/docs/guides/tools-web-search)
137+
- [File search](https://platform.openai.com/docs/guides/tools-file-search)
138+
- [Computer use](https://platform.openai.com/docs/guides/tools-computer-use)
139+
140+
!!! warning "Work in progress"
141+
We currently don't support the native OpenAI tools listed above in the `OpenAIResponsesModel` class.
142+
143+
You can learn more about the differences between the Responses API and Chat Completions API in the [OpenAI API docs](https://platform.openai.com/docs/guides/responses-vs-chat-completions).
144+
145+
```python {title="openai_responses_model.py"}
146+
from pydantic_ai import Agent
147+
from pydantic_ai.models.openai import OpenAIResponsesModel
148+
149+
model = OpenAIResponsesModel('gpt-4o')
150+
agent = Agent(model)
151+
...
152+
```
153+
131154
## Anthropic
132155

133156
### Install

pydantic_ai_slim/pydantic_ai/_result.py

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
from . import _utils, messages as _messages
1414
from .exceptions import ModelRetry
1515
from .result import ResultDataT, ResultDataT_inv, ResultValidatorFunc
16-
from .tools import AgentDepsT, RunContext, ToolDefinition
16+
from .tools import AgentDepsT, GenerateToolJsonSchema, RunContext, ToolDefinition
1717

1818
T = TypeVar('T')
1919
"""An invariant TypeVar."""
@@ -159,7 +159,9 @@ def __init__(self, response_type: type[ResultDataT], name: str, description: str
159159
self.type_adapter = TypeAdapter(response_type)
160160
outer_typed_dict_key: str | None = None
161161
# noinspection PyArgumentList
162-
parameters_json_schema = _utils.check_object_json_schema(self.type_adapter.json_schema())
162+
parameters_json_schema = _utils.check_object_json_schema(
163+
self.type_adapter.json_schema(schema_generator=GenerateToolJsonSchema)
164+
)
163165
else:
164166
response_data_typed_dict = TypedDict( # noqa: UP013
165167
'response_data_typed_dict',
@@ -168,7 +170,9 @@ def __init__(self, response_type: type[ResultDataT], name: str, description: str
168170
self.type_adapter = TypeAdapter(response_data_typed_dict)
169171
outer_typed_dict_key = 'response'
170172
# noinspection PyArgumentList
171-
parameters_json_schema = _utils.check_object_json_schema(self.type_adapter.json_schema())
173+
parameters_json_schema = _utils.check_object_json_schema(
174+
self.type_adapter.json_schema(schema_generator=GenerateToolJsonSchema)
175+
)
172176
# including `response_data_typed_dict` as a title here doesn't add anything and could confuse the LLM
173177
parameters_json_schema.pop('title')
174178

0 commit comments

Comments
 (0)