Skip to content

Commit 40f622a

Browse files
authored
models - openai - images - b64 validate (#251)
1 parent 4d7bb98 commit 40f622a

File tree

2 files changed

+30
-2
lines changed

2 files changed

+30
-2
lines changed

src/strands/types/models/openai.py

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,17 @@ def format_request_message_content(cls, content: ContentBlock) -> dict[str, Any]
5757

5858
if "image" in content:
5959
mime_type = mimetypes.types_map.get(f".{content['image']['format']}", "application/octet-stream")
60-
image_data = content["image"]["source"]["bytes"].decode("utf-8")
60+
image_bytes = content["image"]["source"]["bytes"]
61+
try:
62+
base64.b64decode(image_bytes, validate=True)
63+
logger.warning(
64+
"issue=<%s> | base64 encoded images will not be accepted in a future version",
65+
"https://github.com/strands-agents/sdk-python/issues/252"
66+
)
67+
except ValueError:
68+
image_bytes = base64.b64encode(image_bytes)
69+
70+
image_data = image_bytes.decode("utf-8")
6171
return {
6272
"image_url": {
6373
"detail": "auto",

tests/strands/types/models/test_openai.py

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import base64
12
import unittest.mock
23

34
import pytest
@@ -90,7 +91,24 @@ def system_prompt():
9091
"image_url": {
9192
"detail": "auto",
9293
"format": "image/jpeg",
93-
"url": "data:image/jpeg;base64,image",
94+
"url": "data:image/jpeg;base64,aW1hZ2U=",
95+
},
96+
"type": "image_url",
97+
},
98+
),
99+
# Image - base64 encoded
100+
(
101+
{
102+
"image": {
103+
"format": "jpg",
104+
"source": {"bytes": base64.b64encode(b"image")},
105+
},
106+
},
107+
{
108+
"image_url": {
109+
"detail": "auto",
110+
"format": "image/jpeg",
111+
"url": "data:image/jpeg;base64,aW1hZ2U=",
94112
},
95113
"type": "image_url",
96114
},

0 commit comments

Comments
 (0)