Skip to content

Commit 0ca16b9

Browse files
committed
Add test for OpenAI UploadedFile
1 parent 28aa90a commit 0ca16b9

File tree

3 files changed

+119
-0
lines changed

3 files changed

+119
-0
lines changed

tests/assets/smiley.pdf

5.79 KB
Binary file not shown.
Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
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+
- '206'
12+
content-type:
13+
- application/json
14+
host:
15+
- api.openai.com
16+
method: POST
17+
parsed_body:
18+
messages:
19+
- content:
20+
- text: Give me a short description of this image
21+
type: text
22+
- file:
23+
file_id: file-7yEHnJNSSBeUYfkLq6G8KG
24+
type: file
25+
role: user
26+
model: gpt-4o
27+
stream: false
28+
uri: https://api.openai.com/v1/chat/completions
29+
response:
30+
headers:
31+
access-control-expose-headers:
32+
- X-Request-ID
33+
alt-svc:
34+
- h3=":443"; ma=86400
35+
connection:
36+
- keep-alive
37+
content-length:
38+
- '919'
39+
content-type:
40+
- application/json
41+
openai-organization:
42+
- coplane
43+
openai-processing-ms:
44+
- '2777'
45+
openai-project:
46+
- proj_KGkpeAYM2vPXvZOVtXfnuZ9r
47+
openai-version:
48+
- '2020-10-01'
49+
strict-transport-security:
50+
- max-age=31536000; includeSubDomains; preload
51+
transfer-encoding:
52+
- chunked
53+
parsed_body:
54+
choices:
55+
- finish_reason: stop
56+
index: 0
57+
logprobs: null
58+
message:
59+
annotations: []
60+
content: The image is a simple yellow smiley face with a round shape and black eyes and mouth, conveying a happy
61+
expression.
62+
refusal: null
63+
role: assistant
64+
created: 1755612211
65+
id: chatcmpl-C6HDvVjuYXrcPsCCSWarXSeHAKQVV
66+
model: gpt-4o-2024-08-06
67+
object: chat.completion
68+
service_tier: default
69+
system_fingerprint: fp_80956533cb
70+
usage:
71+
completion_tokens: 24
72+
completion_tokens_details:
73+
accepted_prediction_tokens: 0
74+
audio_tokens: 0
75+
reasoning_tokens: 0
76+
rejected_prediction_tokens: 0
77+
prompt_tokens: 312
78+
prompt_tokens_details:
79+
audio_tokens: 0
80+
cached_tokens: 0
81+
total_tokens: 336
82+
status:
83+
code: 200
84+
message: OK
85+
version: 1

tests/models/test_openai.py

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
import pytest
1313
from dirty_equals import IsListOrTuple
1414
from inline_snapshot import snapshot
15+
from openai.types import FileObject
1516
from pydantic import AnyUrl, BaseModel, ConfigDict, Discriminator, Field, Tag
1617
from typing_extensions import NotRequired, TypedDict
1718

@@ -35,6 +36,7 @@
3536
ThinkingPartDelta,
3637
ToolCallPart,
3738
ToolReturnPart,
39+
UploadedFile,
3840
UserPromptPart,
3941
)
4042
from pydantic_ai.models import ModelRequestParameters
@@ -2926,3 +2928,35 @@ async def test_openai_model_settings_temperature_ignored_on_gpt_5(allow_model_re
29262928

29272929
result = await agent.run('What is the capital of France?', model_settings=ModelSettings(temperature=0.0))
29282930
assert result.output == snapshot('Paris.')
2931+
2932+
2933+
async def test_uploaded_file_input(allow_model_requests: None, openai_api_key: str):
2934+
provider = OpenAIProvider(api_key=openai_api_key)
2935+
m = OpenAIModel('gpt-4o', provider=provider)
2936+
# VCR recording breaks when dealing with openai file upload request due to
2937+
# binary contents. For that reason, we have manually run once the upload
2938+
# and rebuild the FileObject manually (from the print command output).
2939+
# with open('tests/assets/smiley.pdf', 'rb') as f:
2940+
# file_bytes = f.read()
2941+
# openai_file = await provider.client.files.create(
2942+
# file=('image.pdf', file_bytes, 'application/pdf'),
2943+
# purpose='user_data',
2944+
# )
2945+
# print(openai_file)
2946+
openai_file = FileObject(
2947+
id='file-7yEHnJNSSBeUYfkLq6G8KG',
2948+
bytes=5930,
2949+
created_at=1755612061,
2950+
filename='image.pdf', # OpenAI file upload API only accepts pdf
2951+
object='file',
2952+
purpose='user_data',
2953+
status='processed',
2954+
expires_at=None,
2955+
status_details=None,
2956+
)
2957+
agent = Agent(m)
2958+
2959+
result = await agent.run(['Give me a short description of this image', UploadedFile(file=openai_file)])
2960+
assert result.output == snapshot(
2961+
'The image is a simple yellow smiley face with a round shape and black eyes and mouth, conveying a happy expression.'
2962+
)

0 commit comments

Comments
 (0)