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
2 changes: 1 addition & 1 deletion src/google/adk/models/anthropic_llm.py
Original file line number Diff line number Diff line change
Expand Up @@ -142,7 +142,7 @@ def content_to_message_param(
message_block = []
for part in content.parts or []:
# Image data is not supported in Claude for model turns.
if _is_image_part(part):
if content.role != "user" and _is_image_part(part):
logger.warning("Image data is not supported in Claude for model turns.")
continue
Comment on lines +145 to 147

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

This logic correctly filters images for non-user roles. However, the accompanying warning message on line 146 is now slightly misleading as it only mentions "model turns", but this block also executes for the "assistant" role. To improve debugging clarity, consider making the log message more general or dynamic. Note that this would require updating the assertion in the new unit test.

Suggested change
if content.role != "user" and _is_image_part(part):
logger.warning("Image data is not supported in Claude for model turns.")
continue
if content.role != "user" and _is_image_part(part):
logger.warning("Image data is not supported in Claude for role '%s'. Filtering image part.", content.role)
continue


Expand Down
79 changes: 79 additions & 0 deletions tests/unittests/models/test_anthropic_llm.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
from google.adk import version as adk_version
from google.adk.models import anthropic_llm
from google.adk.models.anthropic_llm import Claude
from google.adk.models.anthropic_llm import content_to_message_param
from google.adk.models.anthropic_llm import function_declaration_to_tool_param
from google.adk.models.llm_request import LlmRequest
from google.adk.models.llm_response import LlmResponse
Expand Down Expand Up @@ -346,3 +347,81 @@ async def mock_coro():
mock_client.messages.create.assert_called_once()
_, kwargs = mock_client.messages.create.call_args
assert kwargs["max_tokens"] == 4096


content_to_message_param_test_cases = [
(
"user_role_with_text_and_image",
Content(
role="user",
parts=[
Part.from_text(text="What's in this image?"),
Part(
inline_data=types.Blob(
mime_type="image/jpeg", data=b"fake_image_data"
)
),
],
),
"user",
2, # Expected content length
False, # Should not log warning
),
(
"model_role_with_text_and_image",
Content(
role="model",
parts=[
Part.from_text(text="I see a cat."),
Part(
inline_data=types.Blob(
mime_type="image/png", data=b"fake_image_data"
)
),
],
),
"assistant",
1, # Image filtered out, only text remains
True, # Should log warning
),
(
"assistant_role_with_text_and_image",
Content(
role="assistant",
parts=[
Part.from_text(text="Here's what I found."),
Part(
inline_data=types.Blob(
mime_type="image/webp", data=b"fake_image_data"
)
),
],
),
"assistant",
1, # Image filtered out, only text remains
True, # Should log warning
),
]


@pytest.mark.parametrize(
"_, content, expected_role, expected_content_length, should_log_warning",
content_to_message_param_test_cases,
ids=[case[0] for case in content_to_message_param_test_cases],
)
def test_content_to_message_param_with_images(
_, content, expected_role, expected_content_length, should_log_warning
):
"""Test content_to_message_param handles images correctly based on role."""
with mock.patch("google.adk.models.anthropic_llm.logger") as mock_logger:
result = content_to_message_param(content)

assert result["role"] == expected_role
assert len(result["content"]) == expected_content_length

if should_log_warning:
mock_logger.warning.assert_called_once_with(
"Image data is not supported in Claude for model turns."
)
else:
mock_logger.warning.assert_not_called()