8000 models - openai - images - b64 validate by pgrayy · Pull Request #251 · strands-agents/sdk-python · GitHub
[go: up one dir, main page]

Skip to content

models - openai - images - b64 validate #251

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Jun 19, 2025
Merged
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
12 changes: 11 additions & 1 deletion src/strands/types/models/openai.py
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,17 @@ def format_request_message_content(cls, content: ContentBlock) -> dict[str, Any]

if "image" in content:
mime_type = mimetypes.types_map.get(f".{content['image']['format']}", "application/octet-stream")
image_data = content["image"]["source"]["bytes"].decode("utf-8")
image_bytes = content["image"]["source"]["bytes"]
try:
base64.b64decode(image_bytes, validate=True)
logger.warning(
"issue=<%s> | base64 encoded images will not be accepted in a future version",
"https://github.com/strands-agents/sdk-python/issues/252"
)
except ValueError:
image_bytes = base64.b64encode(image_bytes)

image_data = image_bytes.decode("utf-8")
return {
"image_url": {
"detail": "auto",
Expand Down
20 changes: 19 additions & 1 deletion tests/strands/types/models/test_openai.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import base64
import unittest.mock

import pytest
Expand Down Expand Up @@ -90,7 +91,24 @@ def system_prompt():
"image_url": {
"detail": "auto",
"format": "image/jpeg",
"url": "data:image/jpeg;base64,image",
"url": "data:image/jpeg;base64,aW1hZ2U=",
},
"type": "image_url",
},
),
# Image - base64 encoded
(
{
"image": {
"format": "jpg",
"source": {"bytes": base64.b64encode(b"image")},
},
},
{
"image_url": {
"detail": "auto",
"format": "image/jpeg",
"url": "data:image/jpeg;base64,aW1hZ2U=",
},
"type": "image_url",
},
Expand Down
0