Skip to content

Update handling of ImageUrl for Anthropic Models to use image URLs not binary content #1318

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

Merged
merged 3 commits into from
Apr 1, 2025
Merged
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
44 changes: 4 additions & 40 deletions pydantic_ai_slim/pydantic_ai/models/anthropic.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
from __future__ import annotations as _annotations

import base64
import io
from collections.abc import AsyncGenerator, AsyncIterable, AsyncIterator
from contextlib import asynccontextmanager
Expand Down Expand Up @@ -354,48 +353,13 @@ async def _map_user_prompt(
else:
raise RuntimeError('Only images and PDFs are supported for binary content')
elif isinstance(item, ImageUrl):
try:
response = await cached_async_http_client().get(item.url)
response.raise_for_status()
yield ImageBlockParam(
source={
'data': io.BytesIO(response.content),
'media_type': item.media_type,
'type': 'base64',
},
type='image',
)
except ValueError:
# Download the file if can't find the mime type.
client = cached_async_http_client()
response = await client.get(item.url, follow_redirects=True)
response.raise_for_status()
base64_encoded = base64.b64encode(response.content).decode('utf-8')
if (mime_type := response.headers['Content-Type']) in (
'image/jpeg',
'image/png',
'image/gif',
'image/webp',
):
yield ImageBlockParam(
source={'data': base64_encoded, 'media_type': mime_type, 'type': 'base64'},
type='image',
)
else: # pragma: no cover
raise RuntimeError(f'Unsupported image type: {mime_type}')
yield ImageBlockParam(source={'type': 'url', 'url': item.url}, type='image')
elif isinstance(item, DocumentUrl):
response = await cached_async_http_client().get(item.url)
response.raise_for_status()
if item.media_type == 'application/pdf':
yield DocumentBlockParam(
source=Base64PDFSourceParam(
data=io.BytesIO(response.content),
media_type=item.media_type,
type='base64',
),
type='document',
)
yield DocumentBlockParam(source={'url': item.url, 'type': 'url'}, type='document')
elif item.media_type == 'text/plain':
response = await cached_async_http_client().get(item.url)
response.raise_for_status()
yield DocumentBlockParam(
source=PlainTextSourceParam(data=response.text, media_type=item.media_type, type='text'),
type='document',
Expand Down
Loading