Skip to content

Commit 73c5fc8

Browse files
committed
Add test for OpenAI UploadedFile
1 parent 61b9782 commit 73c5fc8

File tree

3 files changed

+119
-1
lines changed

3 files changed

+119
-1
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 & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@
3535
ThinkingPartDelta,
3636
ToolCallPart,
3737
ToolReturnPart,
38+
UploadedFile,
3839
UserPromptPart,
3940
)
4041
from pydantic_ai.models import ModelRequestParameters
@@ -52,7 +53,7 @@
5253

5354
with try_import() as imports_successful:
5455
from openai import NOT_GIVEN, APIStatusError, AsyncOpenAI
55-
from openai.types import chat
56+
from openai.types import FileObject, chat
5657
from openai.types.chat.chat_completion import Choice, ChoiceLogprobs
5758
from openai.types.chat.chat_completion_chunk import (
5859
Choice as ChunkChoice,
@@ -2926,3 +2927,35 @@ async def test_openai_model_settings_temperature_ignored_on_gpt_5(allow_model_re
29262927

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

0 commit comments

Comments
 (0)