Skip to content
Open
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
14 changes: 13 additions & 1 deletion src/strands/models/anthropic.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,14 @@

T = TypeVar("T", bound=BaseModel)

IMAGE_MEDIA_TYPES = {
"gif": "image/gif",
"jpeg": "image/jpeg",
"jpg": "image/jpeg",
"png": "image/png",
"webp": "image/webp",
}


class AnthropicModel(Model):
"""Anthropic model provider implementation."""
Expand Down Expand Up @@ -131,10 +139,14 @@ def _format_request_message_content(self, content: ContentBlock) -> dict[str, An
}

if "image" in content:
image_format = content["image"]["format"]
return {
"source": {
"data": base64.b64encode(content["image"]["source"]["bytes"]).decode("utf-8"),
"media_type": mimetypes.types_map.get(f".{content['image']['format']}", "application/octet-stream"),
"media_type": IMAGE_MEDIA_TYPES.get(
image_format,
mimetypes.types_map.get(f".{image_format}", "application/octet-stream"),
),
"type": "base64",
},
"type": "image",
Expand Down
16 changes: 16 additions & 0 deletions tests/strands/models/test_anthropic.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import logging
import mimetypes
import unittest.mock
import warnings

Expand Down Expand Up @@ -254,6 +255,21 @@ def test_format_request_with_image(model, model_id, max_tokens):
assert tru_request == exp_request


def test_format_request_with_webp_image_does_not_depend_on_mimetypes(model, model_id, max_tokens, monkeypatch):
monkeypatch.delitem(mimetypes.types_map, ".webp", raising=False)

messages = [
{
"role": "user",
"content": [{"image": {"format": "webp", "source": {"bytes": b"webpimage"}}}],
},
]

tru_request = model.format_request(messages)

assert tru_request["messages"][0]["content"][0]["source"]["media_type"] == "image/webp"


def test_format_request_with_reasoning(model, model_id, max_tokens):
messages = [
{
Expand Down