Skip to content
Closed
Show file tree
Hide file tree
Changes from 1 commit
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
23 changes: 23 additions & 0 deletions src/google/adk/models/anthropic_llm.py
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,14 @@ def _is_image_part(part: types.Part) -> bool:
)


def _is_document_part(part: types.Part) -> bool:
return (
part.inline_data
and part.inline_data.mime_type
and part.inline_data.mime_type == "application/pdf"
Comment thread
encounter marked this conversation as resolved.
Outdated
)


def part_to_message_block(
part: types.Part,
) -> Union[
Expand Down Expand Up @@ -151,6 +159,14 @@ def part_to_message_block(
type="base64", media_type=part.inline_data.mime_type, data=data
),
)
elif _is_document_part(part):
data = base64.b64encode(part.inline_data.data).decode()
return anthropic_types.DocumentBlockParam(
type="document",
source=dict(
type="base64", media_type=part.inline_data.mime_type, data=data
),
)
Comment thread
encounter marked this conversation as resolved.
elif part.executable_code:
return anthropic_types.TextBlockParam(
type="text",
Expand Down Expand Up @@ -179,6 +195,13 @@ def content_to_message_param(
)
continue

# Document data is not supported in Claude for assistant turns.
if content.role != "user" and _is_document_part(part):
logger.warning(
"Document data is not supported in Claude for assistant turns."
)
continue
Comment thread
encounter marked this conversation as resolved.

message_block.append(part_to_message_block(part))

return {
Expand Down
56 changes: 53 additions & 3 deletions tests/unittests/models/test_anthropic_llm.py
Original file line number Diff line number Diff line change
Expand Up @@ -526,6 +526,24 @@ def test_part_to_message_block_with_multiple_content_items():
assert result["content"] == "First part\nSecond part"


def test_part_to_message_block_with_pdf_document():
"""Test that part_to_message_block handles PDF document parts."""
pdf_data = b"%PDF-1.4 fake pdf content"
part = Part(
inline_data=types.Blob(mime_type="application/pdf", data=pdf_data)
)

result = part_to_message_block(part)

assert isinstance(result, dict)
assert result["type"] == "document"
assert result["source"]["type"] == "base64"
assert result["source"]["media_type"] == "application/pdf"
import base64
Comment thread
encounter marked this conversation as resolved.
Outdated

assert result["source"]["data"] == base64.b64encode(pdf_data).decode()


content_to_message_param_test_cases = [
(
"user_role_with_text_and_image",
Expand Down Expand Up @@ -578,6 +596,40 @@ def test_part_to_message_block_with_multiple_content_items():
1, # Image filtered out, only text remains
True, # Should log warning
),
(
"user_role_with_text_and_document",
Content(
role="user",
parts=[
Part.from_text(text="Summarize this document."),
Part(
inline_data=types.Blob(
mime_type="application/pdf", data=b"fake_pdf_data"
)
),
],
),
"user",
2, # Both text and document included
False, # Should not log warning
),
(
"model_role_with_text_and_document",
Content(
role="model",
parts=[
Part.from_text(text="Here is the summary."),
Part(
inline_data=types.Blob(
mime_type="application/pdf", data=b"fake_pdf_data"
)
),
],
),
"assistant",
1, # Document filtered out, only text remains
True, # Should log warning
),
]


Expand All @@ -597,9 +649,7 @@ def test_content_to_message_param_with_images(
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 assistant turns."
)
mock_logger.warning.assert_called_once()
else:
mock_logger.warning.assert_not_called()
Comment thread
encounter marked this conversation as resolved.
Outdated

Expand Down