Skip to content

Fall back on guessed MIME type when requested multi-modal content doesn't have Content-Type header #1613

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Draft
wants to merge 7 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 10 additions & 2 deletions pydantic_ai_slim/pydantic_ai/models/gemini.py
Original file line number Diff line number Diff line change
Expand Up @@ -346,9 +346,17 @@ async def _map_user_prompt(self, part: UserPromptPart) -> list[_GeminiPartUnion]
client = cached_async_http_client()
response = await client.get(item.url, follow_redirects=True)
response.raise_for_status()
mime_type = response.headers['Content-Type'].split(';')[0]
content_type = response.headers.get('Content-Type')
if content_type:
mime_type = content_type.split(';')[0]
else:
mime_type = item.media_type

inline_data = _GeminiInlineDataPart(
inline_data={'data': base64.b64encode(response.content).decode('utf-8'), 'mime_type': mime_type}
inline_data={
'data': base64.b64encode(response.content).decode('utf-8'),
'mime_type': mime_type,
}
)
content.append(inline_data)
else:
Expand Down
18 changes: 15 additions & 3 deletions pydantic_ai_slim/pydantic_ai/models/openai.py
Original file line number Diff line number Diff line change
Expand Up @@ -446,15 +446,23 @@ async def _map_user_prompt(part: UserPromptPart) -> chat.ChatCompletionUserMessa
response = await client.get(item.url)
response.raise_for_status()
base64_encoded = base64.b64encode(response.content).decode('utf-8')
audio_format: Any = response.headers['content-type'].removeprefix('audio/')
content_type = response.headers['content-type']
if content_type:
audio_format: Any = content_type.removeprefix('audio/')
else:
audio_format = item.media_type.removeprefix('audio/')
audio = InputAudio(data=base64_encoded, format=audio_format)
content.append(ChatCompletionContentPartInputAudioParam(input_audio=audio, type='input_audio'))
elif isinstance(item, DocumentUrl):
client = cached_async_http_client()
response = await client.get(item.url)
response.raise_for_status()
base64_encoded = base64.b64encode(response.content).decode('utf-8')
media_type = response.headers.get('content-type').split(';')[0]
content_type = response.headers.get('content-type')
if content_type:
media_type = content_type.split(';')[0]
else:
media_type = item.media_type
file_data = f'data:{media_type};base64,{base64_encoded}'
file = File(file=FileFile(file_data=file_data, filename=f'filename.{item.format}'), type='file')
content.append(file)
Expand Down Expand Up @@ -775,7 +783,11 @@ async def _map_user_prompt(part: UserPromptPart) -> responses.EasyInputMessagePa
response = await client.get(item.url)
response.raise_for_status()
base64_encoded = base64.b64encode(response.content).decode('utf-8')
media_type = response.headers.get('content-type').split(';')[0]
content_type = response.headers.get('content-type')
if content_type:
media_type = content_type.split(';')[0]
else:
media_type = item.media_type
content.append(
responses.ResponseInputFileParam(
type='input_file',
Expand Down
Loading