Skip to content

Commit 5943f56

Browse files
committed
fix more tests
1 parent 0ee4be2 commit 5943f56

File tree

7 files changed

+134
-33
lines changed

7 files changed

+134
-33
lines changed

pydantic_ai_slim/pydantic_ai/models/function.py

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -135,6 +135,7 @@ async def request(
135135
assert isinstance(response_, ModelResponse), response_
136136
response = response_
137137
response.model_name = self._model_name
138+
response.provider_name = self._system
138139
# Add usage data if not already present
139140
if not response.usage.has_values(): # pragma: no branch
140141
response.usage = _estimate_usage(chain(messages, [response]))
@@ -169,6 +170,7 @@ async def request_stream(
169170
model_request_parameters=model_request_parameters,
170171
_model_name=self._model_name,
171172
_iter=response_stream,
173+
_provider_name=self._system,
172174
)
173175

174176
@property
@@ -261,6 +263,7 @@ class FunctionStreamedResponse(StreamedResponse):
261263
_model_name: str
262264
_iter: AsyncIterator[str | DeltaToolCalls | DeltaThinkingCalls]
263265
_timestamp: datetime = field(default_factory=_utils.now_utc)
266+
_provider_name: str
264267

265268
def __post_init__(self):
266269
self._usage += _estimate_usage([])
@@ -305,9 +308,9 @@ def model_name(self) -> str:
305308
return self._model_name
306309

307310
@property
308-
def provider_name(self) -> None:
311+
def provider_name(self) -> str:
309312
"""Get the provider name."""
310-
return None
313+
return self._provider_name
311314

312315
@property
313316
def timestamp(self) -> datetime:

pydantic_ai_slim/pydantic_ai/models/test.py

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -100,8 +100,6 @@ def __init__(
100100
self.custom_output_args = custom_output_args
101101
self.seed = seed
102102
self.last_model_request_parameters = None
103-
self._model_name = 'test'
104-
self._system = 'test'
105103
super().__init__(settings=settings, profile=profile)
106104

107105
async def request(

tests/models/cassettes/test_model_names/test_known_model_names.yaml

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -109,39 +109,39 @@ interactions:
109109
parsed_body:
110110
data:
111111
- created: 0
112-
id: llama-3.3-70b
112+
id: qwen-3-235b-a22b-thinking-2507
113113
object: model
114114
owned_by: Cerebras
115115
- created: 0
116-
id: qwen-3-235b-a22b-instruct-2507
116+
id: qwen-3-coder-480b
117117
object: model
118118
owned_by: Cerebras
119119
- created: 0
120-
id: gpt-oss-120b
120+
id: llama3.1-8b
121121
object: model
122122
owned_by: Cerebras
123123
- created: 0
124-
id: qwen-3-235b-a22b-thinking-2507
124+
id: llama-4-scout-17b-16e-instruct
125125
object: model
126126
owned_by: Cerebras
127127
- created: 0
128-
id: qwen-3-coder-480b
128+
id: qwen-3-235b-a22b-instruct-2507
129129
object: model
130130
owned_by: Cerebras
131131
- created: 0
132-
id: llama3.1-8b
132+
id: qwen-3-32b
133133
object: model
134134
owned_by: Cerebras
135135
- created: 0
136-
id: qwen-3-32b
136+
id: llama-3.3-70b
137137
object: model
138138
owned_by: Cerebras
139139
- created: 0
140-
id: llama-4-scout-17b-16e-instruct
140+
id: llama-4-maverick-17b-128e-instruct
141141
object: model
142142
owned_by: Cerebras
143143
- created: 0
144-
id: llama-4-maverick-17b-128e-instruct
144+
id: gpt-oss-120b
145145
object: model
146146
owned_by: Cerebras
147147
object: list
Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
interactions:
2+
- request:
3+
headers:
4+
accept:
5+
- application/json
6+
accept-encoding:
7+
- gzip, deflate
8+
connection:
9+
- keep-alive
10+
content-length:
11+
- '100'
12+
content-type:
13+
- application/json
14+
host:
15+
- api.openai.com
16+
method: POST
17+
parsed_body:
18+
messages:
19+
- content: Hello! How are you doing?
20+
role: user
21+
model: gpt-4o
22+
stream: false
23+
uri: https://api.openai.com/v1/chat/completions
24+
response:
25+
headers:
26+
access-control-expose-headers:
27+
- X-Request-ID
28+
alt-svc:
29+
- h3=":443"; ma=86400
30+
connection:
31+
- keep-alive
32+
content-length:
33+
- '927'
34+
content-type:
35+
- application/json
36+
openai-organization:
37+
- pydantic-28gund
38+
openai-processing-ms:
39+
- '825'
40+
openai-project:
41+
- proj_dKobscVY9YJxeEaDJen54e3d
42+
openai-version:
43+
- '2020-10-01'
44+
strict-transport-security:
45+
- max-age=31536000; includeSubDomains; preload
46+
transfer-encoding:
47+
- chunked
48+
parsed_body:
49+
choices:
50+
- finish_reason: stop
51+
index: 0
52+
logprobs: null
53+
message:
54+
annotations: []
55+
content: Hello! I'm just a computer program, so I don't have feelings, but I'm here and ready to help you. How can
56+
I assist you today?
57+
refusal: null
58+
role: assistant
59+
created: 1756380852
60+
id: chatcmpl-C9VBMiEq0GYAxsZn9U6FURAbS9Lau
61+
model: gpt-4o-2024-08-06
62+
object: chat.completion
63+
service_tier: default
64+
system_fingerprint: fp_07871e2ad8
65+
usage:
66+
completion_tokens: 30
67+
completion_tokens_details:
68+
accepted_prediction_tokens: 0
69+
audio_tokens: 0
70+
reasoning_tokens: 0
71+
rejected_prediction_tokens: 0
72+
prompt_tokens: 14
73+
prompt_tokens_details:
74+
audio_tokens: 0
75+
cached_tokens: 0
76+
total_tokens: 44
77+
status:
78+
code: 200
79+
message: OK
80+
version: 1

tests/models/mock_openai.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,7 @@ def completion_message(
8787
id='123',
8888
choices=choices,
8989
created=1704067200, # 2024-01-01
90-
model='gpt-4o-123',
90+
model='gpt-4o',
9191
object='chat.completion',
9292
usage=usage,
9393
)
@@ -150,7 +150,7 @@ def response_message(
150150
) -> responses.Response:
151151
return responses.Response(
152152
id='123',
153-
model='gpt-4o-123',
153+
model='gpt-4o',
154154
object='response',
155155
created_at=1704067200, # 2024-01-01
156156
output=list(output_items),

tests/models/test_openai.py

Lines changed: 37 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,12 @@
8787
pytest.mark.skipif(not imports_successful(), reason='openai not installed'),
8888
pytest.mark.anyio,
8989
pytest.mark.vcr,
90+
# TODO(Marcelo): genai-prices needs to include Cerebras prices: https://github.com/pydantic/genai-prices/issues/132
91+
pytest.mark.filterwarnings('ignore:The costs with provider "cerebras" and model:UserWarning'),
92+
# NOTE(Marcelo): The following model is old, so we are probably not including it on `genai-prices`.
93+
pytest.mark.filterwarnings(
94+
'ignore:The costs with provider "openai" and model "gpt-4o-search-preview-2025-03-11":UserWarning'
95+
),
9096
]
9197

9298

@@ -120,15 +126,15 @@ async def test_request_simple_success(allow_model_requests: None):
120126
ModelRequest(parts=[UserPromptPart(content='hello', timestamp=IsNow(tz=timezone.utc))]),
121127
ModelResponse(
122128
parts=[TextPart(content='world')],
123-
model_name='gpt-4o-123',
129+
model_name='gpt-4o',
124130
timestamp=datetime(2024, 1, 1, 0, 0, tzinfo=timezone.utc),
125131
provider_name='openai',
126132
provider_request_id='123',
127133
),
128134
ModelRequest(parts=[UserPromptPart(content='hello', timestamp=IsNow(tz=timezone.utc))]),
129135
ModelResponse(
130136
parts=[TextPart(content='world')],
131-
model_name='gpt-4o-123',
137+
model_name='gpt-4o',
132138
timestamp=datetime(2024, 1, 1, 0, 0, tzinfo=timezone.utc),
133139
provider_name='openai',
134140
provider_request_id='123',
@@ -155,18 +161,28 @@ async def test_request_simple_success(allow_model_requests: None):
155161
]
156162

157163

158-
async def test_request_simple_usage(allow_model_requests: None):
159-
c = completion_message(
160-
ChatCompletionMessage(content='world', role='assistant'),
161-
usage=CompletionUsage(completion_tokens=1, prompt_tokens=2, total_tokens=3),
162-
)
163-
mock_client = MockOpenAI.create_mock(c)
164-
m = OpenAIChatModel('gpt-4o', provider=OpenAIProvider(openai_client=mock_client))
164+
async def test_request_simple_usage(allow_model_requests: None, openai_api_key: str):
165+
m = OpenAIChatModel('gpt-4o', provider=OpenAIProvider(api_key=openai_api_key))
165166
agent = Agent(m)
166167

167-
result = await agent.run('Hello')
168-
assert result.output == 'world'
169-
assert result.usage() == snapshot(RunUsage(requests=1, input_tokens=2, output_tokens=1))
168+
result = await agent.run('Hello! How are you doing?')
169+
assert result.output == snapshot(
170+
"Hello! I'm just a computer program, so I don't have feelings, but I'm here and ready to help you. How can I assist you today?"
171+
)
172+
assert result.usage() == snapshot(
173+
RunUsage(
174+
requests=1,
175+
input_tokens=14,
176+
details={
177+
'accepted_prediction_tokens': 0,
178+
'audio_tokens': 0,
179+
'reasoning_tokens': 0,
180+
'rejected_prediction_tokens': 0,
181+
},
182+
output_tokens=30,
183+
cost=Decimal('0.000335'),
184+
)
185+
)
170186

171187

172188
async def test_request_structured_response(allow_model_requests: None):
@@ -200,7 +216,7 @@ async def test_request_structured_response(allow_model_requests: None):
200216
tool_call_id='123',
201217
)
202218
],
203-
model_name='gpt-4o-123',
219+
model_name='gpt-4o',
204220
timestamp=datetime(2024, 1, 1, tzinfo=timezone.utc),
205221
provider_name='openai',
206222
provider_request_id='123',
@@ -295,7 +311,7 @@ async def get_location(loc_name: str) -> str:
295311
cache_read_tokens=1,
296312
output_tokens=1,
297313
),
298-
model_name='gpt-4o-123',
314+
model_name='gpt-4o',
299315
timestamp=datetime(2024, 1, 1, tzinfo=timezone.utc),
300316
provider_name='openai',
301317
provider_request_id='123',
@@ -323,7 +339,7 @@ async def get_location(loc_name: str) -> str:
323339
cache_read_tokens=2,
324340
output_tokens=2,
325341
),
326-
model_name='gpt-4o-123',
342+
model_name='gpt-4o',
327343
timestamp=datetime(2024, 1, 1, tzinfo=timezone.utc),
328344
provider_name='openai',
329345
provider_request_id='123',
@@ -340,14 +356,16 @@ async def get_location(loc_name: str) -> str:
340356
),
341357
ModelResponse(
342358
parts=[TextPart(content='final response')],
343-
model_name='gpt-4o-123',
359+
model_name='gpt-4o',
344360
timestamp=datetime(2024, 1, 1, tzinfo=timezone.utc),
345361
provider_name='openai',
346362
provider_request_id='123',
347363
),
348364
]
349365
)
350-
assert result.usage() == snapshot(RunUsage(requests=3, cache_read_tokens=3, input_tokens=5, output_tokens=3))
366+
assert result.usage() == snapshot(
367+
RunUsage(requests=3, cache_read_tokens=3, input_tokens=5, output_tokens=3, cost=Decimal('0.00004625'))
368+
)
351369

352370

353371
FinishReason = Literal['stop', 'length', 'tool_calls', 'content_filter', 'function_call']
@@ -2273,6 +2291,8 @@ def test_model_profile_strict_not_supported():
22732291
)
22742292

22752293

2294+
# NOTE(Marcelo): You wouldn't do this because you'd use the GoogleModel. I'm unsure if this test brings any value.
2295+
@pytest.mark.filterwarnings('ignore:The costs with provider "openai" and model "gemini-2.5-pro:UserWarning')
22762296
async def test_compatible_api_with_tool_calls_without_id(allow_model_requests: None, gemini_api_key: str):
22772297
provider = OpenAIProvider(
22782298
openai_client=AsyncOpenAI(

tests/models/test_openai_responses.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1115,7 +1115,7 @@ async def test_openai_responses_usage_without_tokens_details(allow_model_request
11151115
ModelResponse(
11161116
parts=[TextPart(content='4')],
11171117
usage=RequestUsage(input_tokens=14, output_tokens=1, details={'reasoning_tokens': 0}),
1118-
model_name='gpt-4o-123',
1118+
model_name='gpt-4o',
11191119
timestamp=IsDatetime(),
11201120
provider_name='openai',
11211121
provider_request_id='123',

0 commit comments

Comments
 (0)