Skip to content

Commit 789c726

Browse files
committed
fix(vertexai): ChatAnthropicVertex support images with langchain 1.X
fixes #1380
1 parent f58afb4 commit 789c726

File tree

2 files changed

+19
-39
lines changed

2 files changed

+19
-39
lines changed

libs/vertexai/langchain_google_vertexai/_anthropic_utils.py

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -187,39 +187,40 @@ def _format_message_anthropic(
187187
new_block[copy_attr] = block[copy_attr]
188188

189189
if block["type"] == "image":
190-
if block["source_type"] == "url":
191-
if block["url"].startswith("data:"):
190+
if "url" in block:
191+
url = block["url"]
192+
if url.startswith("data:"):
192193
# Data URI
193194
formatted_block = {
194195
"type": "image",
195-
"source": _format_image(block["url"], project),
196+
"source": _format_image(url, project),
196197
}
197198
else:
198199
formatted_block = {
199200
"type": "image",
200-
"source": {"type": "url", "url": block["url"]},
201+
"source": {"type": "url", "url": url},
201202
}
202-
elif block["source_type"] == "base64":
203+
elif "base64" in block:
203204
formatted_block = {
204205
"type": "image",
205206
"source": {
206207
"type": "base64",
207208
"media_type": block["mime_type"],
208-
"data": block["data"],
209+
"data": block["base64"],
209210
},
210211
}
211-
elif block["source_type"] == "id":
212+
elif "file_id" in block:
212213
formatted_block = {
213214
"type": "image",
214215
"source": {
215216
"type": "file",
216-
"file_id": block["id"],
217+
"file_id": block["file_id"],
217218
},
218219
}
219220
else:
220221
msg = (
221-
"Anthropic only supports 'url' and 'base64' source_type "
222-
"for image content blocks."
222+
"Image content blocks must have either 'url', 'base64', "
223+
"or 'file_id' field."
223224
)
224225
raise ValueError(msg)
225226
content.append(formatted_block)

libs/vertexai/tests/unit_tests/test_anthropic_utils.py

Lines changed: 8 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
import base64
44
from unittest.mock import patch
5+
56
import pytest
67
from anthropic.types import (
78
RawContentBlockDeltaEvent,
@@ -15,18 +16,18 @@
1516
SystemMessage,
1617
ToolMessage,
1718
)
19+
from langchain_core.messages.content import create_image_block, create_text_block
1820
from langchain_core.messages.tool import tool_call as create_tool_call
1921

2022
from langchain_google_vertexai._anthropic_utils import (
2123
_documents_in_params,
24+
_format_image,
2225
_format_message_anthropic,
2326
_format_messages_anthropic,
2427
_make_message_chunk_from_anthropic_event,
2528
_thinking_in_params,
2629
)
2730

28-
from langchain_google_vertexai._anthropic_utils import _format_image
29-
3031

3132
def test_format_message_anthropic_with_cache_control_in_kwargs() -> None:
3233
"""Test formatting a message with cache control in additional_kwargs."""
@@ -785,25 +786,11 @@ def test_format_messages_anthropic_with_mixed_messages() -> None:
785786
(
786787
[
787788
AIMessage(
788-
content=[
789-
{"type": "text", "text": "Text content"},
790-
{
791-
"type": "image",
792-
"source_type": "url",
793-
"url": "https://example.com/image.png",
794-
},
795-
{
796-
"type": "image",
797-
"source_type": "url",
798-
"url": "data:image/png;base64,/9j/4AAQSk",
799-
},
800-
{
801-
"type": "image",
802-
"source_type": "base64",
803-
"mime_type": "image/png",
804-
"data": "/9j/4AAQSk",
805-
},
806-
{"type": "image", "source_type": "id", "id": "1"},
789+
content_blocks=[
790+
create_text_block(text="Text content"),
791+
create_image_block(url="https://example.com/image.png"),
792+
create_image_block(base64="/9j/4AAQSk", mime_type="image/png"),
793+
create_image_block(file_id="1"),
807794
]
808795
),
809796
],
@@ -828,14 +815,6 @@ def test_format_messages_anthropic_with_mixed_messages() -> None:
828815
"data": "/9j/4AAQSk",
829816
},
830817
},
831-
{
832-
"type": "image",
833-
"source": {
834-
"type": "base64",
835-
"media_type": "image/png",
836-
"data": "/9j/4AAQSk",
837-
},
838-
},
839818
{"type": "image", "source": {"type": "file", "file_id": "1"}},
840819
],
841820
}

0 commit comments

Comments
 (0)