Skip to content

Commit 5f99595

Browse files
ryx2claudeDouweM
authored
Add support for OpenAI verbosity parameter in Responses API (#2493)
Co-authored-by: Claude <[email protected]> Co-authored-by: Douwe Maan <[email protected]>
1 parent cef7a91 commit 5f99595

File tree

4 files changed

+100
-7
lines changed

4 files changed

+100
-7
lines changed

pydantic_ai_slim/pydantic_ai/models/openai.py

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -177,6 +177,14 @@ class OpenAIResponsesModelSettings(OpenAIModelSettings, total=False):
177177
middle of the conversation.
178178
"""
179179

180+
openai_text_verbosity: Literal['low', 'medium', 'high']
181+
"""Constrains the verbosity of the model's text response.
182+
183+
Lower values will result in more concise responses, while higher values will
184+
result in more verbose responses. Currently supported values are `low`,
185+
`medium`, and `high`.
186+
"""
187+
180188

181189
@dataclass(init=False)
182190
class OpenAIModel(Model):
@@ -819,6 +827,10 @@ async def _responses_create(
819827
openai_messages.insert(0, responses.EasyInputMessageParam(role='system', content=instructions))
820828
instructions = NOT_GIVEN
821829

830+
if verbosity := model_settings.get('openai_text_verbosity'):
831+
text = text or {}
832+
text['verbosity'] = verbosity
833+
822834
sampling_settings = (
823835
model_settings
824836
if OpenAIModelProfile.from_profile(self.profile).openai_supports_sampling_settings
Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
interactions:
2+
- request:
3+
headers:
4+
accept:
5+
- application/json
6+
accept-encoding:
7+
- gzip, deflate
8+
connection:
9+
- keep-alive
10+
content-type:
11+
- application/json
12+
host:
13+
- api.openai.com
14+
method: POST
15+
parsed_body:
16+
input:
17+
- content: What is 2+2?
18+
role: user
19+
instructions: ''
20+
model: gpt-5
21+
text:
22+
format:
23+
type: text
24+
verbosity: low
25+
uri: https://api.openai.com/v1/responses
26+
response:
27+
headers:
28+
content-type:
29+
- application/json
30+
parsed_body:
31+
created_at: 1743075630
32+
error: null
33+
id: resp_test_verbosity_simple
34+
incomplete_details: null
35+
instructions: ''
36+
max_output_tokens: null
37+
metadata: {}
38+
model: gpt-5
39+
object: response
40+
output:
41+
- content:
42+
- annotations: []
43+
text: "4"
44+
type: output_text
45+
id: msg_test_verbosity_simple
46+
role: assistant
47+
status: completed
48+
type: message
49+
parallel_tool_calls: true
50+
previous_response_id: null
51+
reasoning: null
52+
status: complete
53+
status_details: null
54+
tool_calls: null
55+
total_tokens: 15
56+
usage:
57+
input_tokens: 10
58+
input_tokens_details:
59+
cached_tokens: 0
60+
output_tokens: 1
61+
output_tokens_details:
62+
reasoning_tokens: 0
63+
total_tokens: 11
64+
status:
65+
code: 200
66+
message: OK
67+
version: 1

tests/models/test_openai_responses.py

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1134,3 +1134,17 @@ async def get_user_country() -> str:
11341134
),
11351135
]
11361136
)
1137+
1138+
1139+
@pytest.mark.vcr()
1140+
async def test_openai_responses_verbosity(allow_model_requests: None, openai_api_key: str):
1141+
"""Test that verbosity setting is properly passed to the OpenAI API"""
1142+
# Following GPT-5 + verbosity documentation pattern
1143+
provider = OpenAIProvider(
1144+
api_key=openai_api_key,
1145+
base_url='https://api.openai.com/v1', # Explicitly set base URL
1146+
)
1147+
model = OpenAIResponsesModel('gpt-5', provider=provider)
1148+
agent = Agent(model=model, model_settings=OpenAIResponsesModelSettings(openai_text_verbosity='low'))
1149+
result = await agent.run('What is 2+2?')
1150+
assert result.output == snapshot('4')

uv.lock

Lines changed: 7 additions & 7 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)