Skip to content

Commit aebe2db

Browse files
author
Evan Lin
committed
🛠️ Update image handling in generate_vision_with_langchain to use base64 encoding for improved compatibility with Vertex AI
1 parent 4a5558f commit aebe2db

1 file changed

Lines changed: 19 additions & 8 deletions

File tree

‎main.py‎

Lines changed: 19 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
from io import BytesIO
1515
import aiohttp
1616
import PIL.Image
17+
import base64
1718

1819
# Import LangChain components with Vertex AI
1920
from langchain_google_vertexai import ChatVertexAI
@@ -136,21 +137,31 @@ def generate_vision_with_langchain(img, prompt):
136137
"""
137138
Generate a image vision result using LangChain with Vertex AI model.
138139
"""
139-
# Convert PIL Image to bytes
140+
# Convert PIL Image to base64 encoded string
140141
img_byte_arr = BytesIO()
141142
img.save(img_byte_arr, format=img.format or 'JPEG')
142143
img_bytes = img_byte_arr.getvalue()
144+
base64_image = base64.b64encode(img_bytes).decode('utf-8')
143145

144-
# Create a message with both text and image using proper Vertex AI format
146+
# Create a message with both text and image using the correct Vertex AI format
145147
message = HumanMessage(
146148
content=[
147149
{"type": "text", "text": prompt},
148-
# Use blob for binary data
149-
{"type": "image_url", "image_url": {"blob": img_bytes}}
150+
{
151+
"type": "image",
152+
"source": {
153+
"type": "base64",
154+
"media_type": "image/jpeg",
155+
"data": base64_image
156+
}
157+
}
150158
]
151159
)
152160

153-
# Call the vision model
154-
response = vision_model.invoke([message])
155-
156-
return response.content
161+
try:
162+
# Call the vision model
163+
response = vision_model.invoke([message])
164+
return response.content
165+
except Exception as e:
166+
print(f"Error in vision model: {str(e)}")
167+
return f"I encountered an error processing this image: {str(e)}"

0 commit comments

Comments
 (0)