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
21 changes: 11 additions & 10 deletions libs/vertexai/langchain_google_vertexai/_anthropic_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -187,39 +187,40 @@ def _format_message_anthropic(
new_block[copy_attr] = block[copy_attr]

if block["type"] == "image":
if block["source_type"] == "url":
if block["url"].startswith("data:"):
if "url" in block:
url = block["url"]
if url.startswith("data:"):
# Data URI
formatted_block = {
"type": "image",
"source": _format_image(block["url"], project),
"source": _format_image(url, project),
}
else:
formatted_block = {
"type": "image",
"source": {"type": "url", "url": block["url"]},
"source": {"type": "url", "url": url},
}
elif block["source_type"] == "base64":
elif "base64" in block:
formatted_block = {
"type": "image",
"source": {
"type": "base64",
"media_type": block["mime_type"],
"data": block["data"],
"data": block["base64"],
},
}
elif block["source_type"] == "id":
elif "file_id" in block:
formatted_block = {
"type": "image",
"source": {
"type": "file",
"file_id": block["id"],
"file_id": block["file_id"],
},
}
else:
msg = (
"Anthropic only supports 'url' and 'base64' source_type "
"for image content blocks."
"Image content blocks must have either 'url', 'base64', "
"or 'file_id' field."
)
raise ValueError(msg)
content.append(formatted_block)
Expand Down
33 changes: 6 additions & 27 deletions libs/vertexai/tests/unit_tests/test_anthropic_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
SystemMessage,
ToolMessage,
)
from langchain_core.messages.content import create_image_block, create_text_block
from langchain_core.messages.tool import tool_call as create_tool_call

from langchain_google_vertexai._anthropic_utils import (
Expand Down Expand Up @@ -785,25 +786,11 @@ def test_format_messages_anthropic_with_mixed_messages() -> None:
(
[
AIMessage(
content=[
{"type": "text", "text": "Text content"},
{
"type": "image",
"source_type": "url",
"url": "https://example.com/image.png",
},
{
"type": "image",
"source_type": "url",
"url": "data:image/png;base64,/9j/4AAQSk",
},
{
"type": "image",
"source_type": "base64",
"mime_type": "image/png",
"data": "/9j/4AAQSk",
},
{"type": "image", "source_type": "id", "id": "1"},
Comment on lines -788 to -806
Copy link
Member

Choose a reason for hiding this comment

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

we must continue to maintain support for these input formats; we should not remove these tests

content_blocks=[
create_text_block(text="Text content"),
create_image_block(url="https://example.com/image.png"),
create_image_block(base64="/9j/4AAQSk", mime_type="image/png"),
create_image_block(file_id="1"),
]
),
],
Expand All @@ -828,14 +815,6 @@ def test_format_messages_anthropic_with_mixed_messages() -> None:
"data": "/9j/4AAQSk",
},
},
{
"type": "image",
"source": {
"type": "base64",
"media_type": "image/png",
"data": "/9j/4AAQSk",
},
},
Comment on lines -831 to -838
Copy link
Member

Choose a reason for hiding this comment

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

same here

{"type": "image", "source": {"type": "file", "file_id": "1"}},
],
}
Expand Down