Skip to content

Commit e7eb8a8

Browse files
Respect FileUrl.force_download flag in OpenAI Chat and Responses models (#3106)
1 parent dc3c9e9 commit e7eb8a8

File tree

5 files changed

+1434
-2
lines changed

5 files changed

+1434
-2
lines changed

pydantic_ai_slim/pydantic_ai/messages.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -129,7 +129,7 @@ class FileUrl(ABC):
129129
"""
130130

131131
force_download: bool = False
132-
"""If the model supports it:
132+
"""For OpenAI and Google APIs it:
133133
134134
* If True, the file is downloaded and the data is sent to the model as bytes.
135135
* If False, the URL is sent directly to the model and no download is performed.

pydantic_ai_slim/pydantic_ai/models/openai.py

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -776,6 +776,9 @@ async def _map_user_prompt(self, part: UserPromptPart) -> chat.ChatCompletionUse
776776
image_url: ImageURL = {'url': item.url}
777777
if metadata := item.vendor_metadata:
778778
image_url['detail'] = metadata.get('detail', 'auto')
779+
if item.force_download:
780+
image_content = await download_item(item, data_format='base64_uri', type_format='extension')
781+
image_url['url'] = image_content['data']
779782
content.append(ChatCompletionContentPartImageParam(image_url=image_url, type='image_url'))
780783
elif isinstance(item, BinaryContent):
781784
if self._is_text_like_media_type(item.media_type):
@@ -1529,11 +1532,16 @@ async def _map_user_prompt(part: UserPromptPart) -> responses.EasyInputMessagePa
15291532
raise RuntimeError(f'Unsupported binary content type: {item.media_type}')
15301533
elif isinstance(item, ImageUrl):
15311534
detail: Literal['auto', 'low', 'high'] = 'auto'
1535+
image_url = item.url
15321536
if metadata := item.vendor_metadata:
15331537
detail = cast(Literal['auto', 'low', 'high'], metadata.get('detail', 'auto'))
1538+
if item.force_download:
1539+
downloaded_item = await download_item(item, data_format='base64_uri', type_format='extension')
1540+
image_url = downloaded_item['data']
1541+
15341542
content.append(
15351543
responses.ResponseInputImageParam(
1536-
image_url=item.url,
1544+
image_url=image_url,
15371545
type='input_image',
15381546
detail=detail,
15391547
)

0 commit comments

Comments
 (0)