Hello. Im having issues with the vision api. im getting this response - I’m unable to view images directly or fetch content from URLs. However, if you can describe the image to me, I would be happy to help you analyze or interpret it! Im correctly implementing it and i have been debugging for hours. Can someone help me? Thanks
Hi!
Can you post the code you are using to test this?
def validate_image(image_data: bytes) → bool:
try:
with Image.open(io.BytesIO(image_data)) as img:
img_format = img.format.lower()
if img_format not in [‘jpeg’, ‘png’, ‘gif’, ‘webp’]:
print(f"Unsupported image format: {img_format}“)
return False
print(f"Valid image format: {img_format}”)
return True
except IOError as e:
print(f"Invalid image data: {e}")
return False
def upload_image_to_s3(image_data: bytes, filename: Optional[str] = None) → str:
if not image_data:
raise ValueError(“No image data provided for upload to S3.”)
try:
with Image.open(io.BytesIO(image_data)) as img:
img_format = img.format.lower()
if img_format not in ['jpeg', 'png', 'gif', 'webp']:
raise ValueError(f"Unsupported image format: {img_format}")
extension = img_format if img_format != 'jpeg' else 'jpg'
except IOError:
raise ValueError("Invalid image data provided.")
filename = filename or f"uploads/{uuid4()}.{extension}"
try:
s3_client.upload_fileobj(
Fileobj=io.BytesIO(image_data),
Bucket=bucket_name,
Key=filename,
ExtraArgs={'ContentType': f"image/{extension}"}
)
except (BotoCoreError, ClientError) as e:
raise RuntimeError(f"Failed to upload image to S3: {e}")
region = "us-east-2"
image_url = f"https://{bucket_name}.s3.{region}.amazonaws.com/{filename}"
return image_url
import base64
import re
def vision_node(state: Dict[str, Any]) → Dict[str, Any]:
“”"
Processes an image using OpenAI’s Vision API with S3-generated image URL.
Args:
state (dict): The state containing image data.
Returns:
dict: Updated state with stimulus_description.
"""
try:
image_data = state.get("image")
if not image_data:
raise ValueError("No image data provided.")
if not validate_image(image_data):
raise ValueError("Unsupported image format. Only JPEG, PNG, GIF, and WebP are allowed.")
print("Uploading image to S3...")
image_url = upload_image_to_s3(image_data)
print(f"Image uploaded successfully. URL: {image_url}")
prompt = "What is in this image?"
response = client.chat.completions.create(
model="gpt-4o-mini",
messages=[
{
"role": "user",
"content": f"{prompt}\n\nImage URL: {image_url}"
}
],
max_tokens=300,
)
stimulus_description = response.choices[0].message.content
state["stimulus_description"] = stimulus_description
print(stimulus_description)
return state
except Exception as e:
print(f"Error in vision_node: {e}")
raise ValueError(f"Error in vision_node: {e}")
im also doing image.read() in the endpoint and passing that in
Couple of things to try, use gpt-4o instead of mini as a test and also try uploading a Base64 encoded image rather that an S3 bucket link, also, is the S3 image upload gurenteeded to be compleated prior to the API call being made? i…e could there be a blank placeholder in the bucket when the AI API call is made?
thank u so much. the base64 encoding worked. ur a life saver