Skip to content

Commit 3bc8e43

Browse files
authored
Make OpenAIResponsesModelSettings.openai_builtin_tools work again (#2520)
1 parent e81ff69 commit 3bc8e43

File tree

6 files changed

+69
-47
lines changed

6 files changed

+69
-47
lines changed

pydantic_ai_slim/pydantic_ai/models/anthropic.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
from .. import ModelHTTPError, UnexpectedModelBehavior, _utils, usage
2424
from .._run_context import RunContext
2525
from .._utils import guard_tool_call_id as _guard_tool_call_id
26+
from ..exceptions import UserError
2627
from ..messages import (
2728
BinaryContent,
2829
BuiltinToolCallPart,
@@ -367,6 +368,10 @@ def _get_builtin_tools(self, model_request_parameters: ModelRequestParameters) -
367368
)
368369
elif isinstance(tool, CodeExecutionTool): # pragma: no branch
369370
tools.append(BetaCodeExecutionTool20250522Param(name='code_execution', type='code_execution_20250522'))
371+
else: # pragma: no cover
372+
raise UserError(
373+
f'`{tool.__class__.__name__}` is not supported by `AnthropicModel`. If it should be, please file an issue.'
374+
)
370375
return tools
371376

372377
async def _map_message(self, messages: list[ModelMessage]) -> tuple[str, list[BetaMessageParam]]: # noqa: C901

pydantic_ai_slim/pydantic_ai/models/google.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -221,6 +221,10 @@ def _get_tools(self, model_request_parameters: ModelRequestParameters) -> list[T
221221
tools.append(ToolDict(google_search=GoogleSearchDict()))
222222
elif isinstance(tool, CodeExecutionTool): # pragma: no branch
223223
tools.append(ToolDict(code_execution=ToolCodeExecutionDict()))
224+
else: # pragma: no cover
225+
raise UserError(
226+
f'`{tool.__class__.__name__}` is not supported by `GoogleModel`. If it should be, please file an issue.'
227+
)
224228
return tools or None
225229

226230
def _get_tool_config(

pydantic_ai_slim/pydantic_ai/models/groq.py

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
from .._run_context import RunContext
1414
from .._thinking_part import split_content_into_text_and_thinking
1515
from .._utils import generate_tool_call_id, guard_tool_call_id as _guard_tool_call_id, number_to_datetime
16-
from ..builtin_tools import CodeExecutionTool, WebSearchTool
16+
from ..builtin_tools import WebSearchTool
1717
from ..exceptions import UserError
1818
from ..messages import (
1919
BinaryContent,
@@ -318,8 +318,10 @@ def _get_builtin_tools(
318318
if isinstance(tool, WebSearchTool):
319319
if not GroqModelProfile.from_profile(self.profile).groq_always_has_web_search_builtin_tool:
320320
raise UserError('`WebSearchTool` is not supported by Groq') # pragma: no cover
321-
elif isinstance(tool, CodeExecutionTool): # pragma: no branch
322-
raise UserError('`CodeExecutionTool` is not supported by Groq')
321+
else:
322+
raise UserError(
323+
f'`{tool.__class__.__name__}` is not supported by `GroqModel`. If it should be, please file an issue.'
324+
)
323325
return tools
324326

325327
def _map_messages(self, messages: list[ModelMessage]) -> list[chat.ChatCompletionMessageParam]:

pydantic_ai_slim/pydantic_ai/models/openai.py

Lines changed: 13 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -462,8 +462,10 @@ def _get_web_search_options(self, model_request_parameters: ModelRequestParamete
462462
),
463463
)
464464
return WebSearchOptions(search_context_size=tool.search_context_size)
465-
elif isinstance(tool, CodeExecutionTool): # pragma: no branch
466-
raise UserError('`CodeExecutionTool` is not supported by OpenAI')
465+
else:
466+
raise UserError(
467+
f'`{tool.__class__.__name__}` is not supported by `OpenAIModel`. If it should be, please file an issue.'
468+
)
467469

468470
async def _map_messages(self, messages: list[ModelMessage]) -> list[chat.ChatCompletionMessageParam]:
469471
"""Just maps a `pydantic_ai.Message` to a `openai.types.ChatCompletionMessageParam`."""
@@ -632,14 +634,6 @@ class OpenAIResponsesModel(Model):
632634
The [OpenAI Responses API](https://platform.openai.com/docs/api-reference/responses) is the
633635
new API for OpenAI models.
634636
635-
The Responses API has built-in tools, that you can use instead of building your own:
636-
637-
- [Web search](https://platform.openai.com/docs/guides/tools-web-search)
638-
- [File search](https://platform.openai.com/docs/guides/tools-file-search)
639-
- [Computer use](https://platform.openai.com/docs/guides/tools-computer-use)
640-
641-
Use the `openai_builtin_tools` setting to add these tools to your model.
642-
643637
If you are interested in the differences between the Responses API and the Chat Completions API,
644638
see the [OpenAI API docs](https://platform.openai.com/docs/guides/responses-vs-chat-completions).
645639
"""
@@ -780,8 +774,11 @@ async def _responses_create(
780774
model_settings: OpenAIResponsesModelSettings,
781775
model_request_parameters: ModelRequestParameters,
782776
) -> responses.Response | AsyncStream[responses.ResponseStreamEvent]:
783-
tools = self._get_tools(model_request_parameters)
784-
tools = self._get_builtin_tools(model_request_parameters) + tools
777+
tools = (
778+
self._get_builtin_tools(model_request_parameters)
779+
+ list(model_settings.get('openai_builtin_tools', []))
780+
+ self._get_tools(model_request_parameters)
781+
)
785782

786783
if not tools:
787784
tool_choice: Literal['none', 'required', 'auto'] | None = None
@@ -880,6 +877,10 @@ def _get_builtin_tools(self, model_request_parameters: ModelRequestParameters) -
880877
tools.append(web_search_tool)
881878
elif isinstance(tool, CodeExecutionTool): # pragma: no branch
882879
tools.append({'type': 'code_interpreter', 'container': {'type': 'auto'}})
880+
else:
881+
raise UserError( # pragma: no cover
882+
f'`{tool.__class__.__name__}` is not supported by `OpenAIResponsesModel`. If it should be, please file an issue.'
883+
)
883884
return tools
884885

885886
def _map_tool_definition(self, f: ToolDefinition) -> responses.FunctionToolParam:

tests/models/cassettes/test_openai_responses/test_openai_responses_model_builtin_tools.yaml

Lines changed: 36 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ interactions:
88
connection:
99
- keep-alive
1010
content-length:
11-
- '217'
11+
- '199'
1212
content-type:
1313
- application/json
1414
host:
@@ -18,7 +18,6 @@ interactions:
1818
input:
1919
- content: Give me the best news about LLMs from the last 24 hours. Be short.
2020
role: user
21-
instructions: ''
2221
model: gpt-4o
2322
stream: false
2423
tool_choice: auto
@@ -32,67 +31,77 @@ interactions:
3231
connection:
3332
- keep-alive
3433
content-length:
35-
- '3210'
34+
- '2994'
3635
content-type:
3736
- application/json
3837
openai-organization:
3938
- pydantic-28gund
4039
openai-processing-ms:
41-
- '2843'
40+
- '8814'
41+
openai-project:
42+
- proj_dKobscVY9YJxeEaDJen54e3d
4243
openai-version:
4344
- '2020-10-01'
4445
strict-transport-security:
4546
- max-age=31536000; includeSubDomains; preload
4647
transfer-encoding:
4748
- chunked
4849
parsed_body:
49-
created_at: 1743506361
50+
background: false
51+
created_at: 1755020432
5052
error: null
51-
id: resp_67ebcbb93728819197f923ff16e98bce04f5055a2a33abc3
53+
id: resp_689b7c90010c8196ac0efd68b021490f07450cfc2d48b975
5254
incomplete_details: null
53-
instructions: ''
55+
instructions: null
5456
max_output_tokens: null
57+
max_tool_calls: null
5558
metadata: {}
5659
model: gpt-4o-2024-08-06
5760
object: response
5861
output:
59-
- id: ws_67ebcbb9ab4481918bebf63b66dbb67c04f5055a2a33abc3
62+
- action:
63+
query: latest news on LLMs October 2023
64+
type: search
65+
id: ws_689b7c90de788196b18c6d68e7894def07450cfc2d48b975
6066
status: completed
6167
type: web_search_call
6268
- content:
6369
- annotations:
64-
- end_index: 571
65-
start_index: 404
66-
title: OpenAI plans to release open-weight language model in coming months
70+
- end_index: 364
71+
start_index: 273
72+
title: OpenAI's big GPT-5 launch gets bumpy
6773
type: url_citation
68-
url: https://www.reuters.com/technology/artificial-intelligence/openai-plans-release-open-weight-language-model-coming-months-2025-03-31/?utm_source=openai
69-
- end_index: 846
70-
start_index: 625
71-
title: OpenAI plans to release open-weight language model in coming months
74+
url: https://www.axios.com/2025/08/12/gpt-5-bumpy-launch-openai?utm_source=openai
75+
- end_index: 533
76+
start_index: 417
77+
title: OpenAI's big GPT-5 launch gets bumpy
7278
type: url_citation
73-
url: https://www.reuters.com/technology/artificial-intelligence/openai-plans-release-open-weight-language-model-coming-months-2025-03-31/?utm_source=openai
74-
text: "In the past 24 hours, OpenAI announced plans to release its first open-weight language model with reasoning
75-
capabilities since GPT-2. This model will allow developers to fine-tune it for specific applications without needing
76-
the original training data. To gather feedback and refine the model, OpenAI will host developer events starting
77-
in San Francisco and expanding to Europe and Asia-Pacific regions. ([reuters.com](https://www.reuters.com/technology/artificial-intelligence/openai-plans-release-open-weight-language-model-coming-months-2025-03-31/?utm_source=openai))\n\n\n##
78-
OpenAI to Release Open-Weight Language Model:\n- [OpenAI plans to release open-weight language model in coming
79-
months](https://www.reuters.com/technology/artificial-intelligence/openai-plans-release-open-weight-language-model-coming-months-2025-03-31/?utm_source=openai) "
79+
url: https://www.axios.com/2025/08/12/gpt-5-bumpy-launch-openai?utm_source=openai
80+
logprobs: []
81+
text: "OpenAI's recent launch of GPT-5 has faced mixed reactions. Despite strong benchmark performance and early
82+
praise, users have reported issues like errors in basic math and geography. CEO Sam Altman has acknowledged these
83+
concerns and assured that improvements are underway. ([axios.com](https://www.axios.com/2025/08/12/gpt-5-bumpy-launch-openai?utm_source=openai))\n\n\n##
84+
OpenAI's GPT-5 Launch Faces Mixed Reactions:\n- [OpenAI's big GPT-5 launch gets bumpy](https://www.axios.com/2025/08/12/gpt-5-bumpy-launch-openai?utm_source=openai) "
8085
type: output_text
81-
id: msg_67ebcbbaf988819192d44919020b82e704f5055a2a33abc3
86+
id: msg_689b7c951b7481968513d007e75151fd07450cfc2d48b975
8287
role: assistant
8388
status: completed
8489
type: message
8590
parallel_tool_calls: true
8691
previous_response_id: null
92+
prompt_cache_key: null
8793
reasoning:
8894
effort: null
89-
generate_summary: null
95+
summary: null
96+
safety_identifier: null
97+
service_tier: default
9098
status: completed
9199
store: true
92100
temperature: 1.0
93101
text:
94102
format:
95103
type: text
104+
verbosity: medium
96105
tool_choice: auto
97106
tools:
98107
- search_context_size: medium
@@ -103,16 +112,17 @@ interactions:
103112
region: null
104113
timezone: null
105114
type: approximate
115+
top_logprobs: 0
106116
top_p: 1.0
107117
truncation: disabled
108118
usage:
109119
input_tokens: 320
110120
input_tokens_details:
111121
cached_tokens: 0
112-
output_tokens: 200
122+
output_tokens: 159
113123
output_tokens_details:
114124
reasoning_tokens: 0
115-
total_tokens: 520
125+
total_tokens: 479
116126
user: null
117127
status:
118128
code: 200

tests/models/test_openai_responses.py

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -428,23 +428,23 @@ async def test_openai_responses_model_builtin_tools(allow_model_requests: None,
428428
parts=[
429429
TextPart(
430430
content="""\
431-
In the past 24 hours, OpenAI announced plans to release its first open-weight language model with reasoning capabilities since GPT-2. This model will allow developers to fine-tune it for specific applications without needing the original training data. To gather feedback and refine the model, OpenAI will host developer events starting in San Francisco and expanding to Europe and Asia-Pacific regions. ([reuters.com](https://www.reuters.com/technology/artificial-intelligence/openai-plans-release-open-weight-language-model-coming-months-2025-03-31/?utm_source=openai))
431+
OpenAI's recent launch of GPT-5 has faced mixed reactions. Despite strong benchmark performance and early praise, users have reported issues like errors in basic math and geography. CEO Sam Altman has acknowledged these concerns and assured that improvements are underway. ([axios.com](https://www.axios.com/2025/08/12/gpt-5-bumpy-launch-openai?utm_source=openai))
432432
433433
434-
## OpenAI to Release Open-Weight Language Model:
435-
- [OpenAI plans to release open-weight language model in coming months](https://www.reuters.com/technology/artificial-intelligence/openai-plans-release-open-weight-language-model-coming-months-2025-03-31/?utm_source=openai) \
434+
## OpenAI's GPT-5 Launch Faces Mixed Reactions:
435+
- [OpenAI's big GPT-5 launch gets bumpy](https://www.axios.com/2025/08/12/gpt-5-bumpy-launch-openai?utm_source=openai) \
436436
"""
437437
)
438438
],
439439
usage=Usage(
440440
request_tokens=320,
441-
response_tokens=200,
442-
total_tokens=520,
441+
response_tokens=159,
442+
total_tokens=479,
443443
details={'reasoning_tokens': 0, 'cached_tokens': 0},
444444
),
445445
model_name='gpt-4o-2024-08-06',
446446
timestamp=IsDatetime(),
447-
vendor_id='resp_67ebcbb93728819197f923ff16e98bce04f5055a2a33abc3',
447+
vendor_id='resp_689b7c90010c8196ac0efd68b021490f07450cfc2d48b975',
448448
),
449449
]
450450
)

0 commit comments

Comments
 (0)