Skip to content

Commit d3a5240

Browse files
pgrayyjsamuel1
authored andcommitted
models - openai - do not accept b64 images (strands-agents#368)
1 parent 2094c0a commit d3a5240

File tree

2 files changed

+1
-57
lines changed

2 files changed

+1
-57
lines changed

src/strands/types/models/openai.py

Lines changed: 1 addition & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -34,32 +34,6 @@ class OpenAIModel(Model, abc.ABC):
3434

3535
config: dict[str, Any]
3636

37-
@staticmethod
38-
def b64encode(data: bytes) -> bytes:
39-
"""Base64 encode the provided data.
40-
41-
If the data is already base64 encoded, we do nothing.
42-
Note, this is a temporary method used to provide a warning to users who pass in base64 encoded data. In future
43-
versions, images and documents will be base64 encoded on behalf of customers for consistency with the other
44-
providers and general convenience.
45-
46-
Args:
47-
data: Data to encode.
48-
49-
Returns:
50-
Base64 encoded data.
51-
"""
52-
try:
53-
base64.b64decode(data, validate=True)
54-
logger.warning(
55-
"issue=<%s> | base64 encoded images and documents will not be accepted in future versions",
56-
"https://github.com/strands-agents/sdk-python/issues/252",
57-
)
58-
except ValueError:
59-
data = base64.b64encode(data)
60-
61-
return data
62-
6337
@classmethod
6438
def format_request_message_content(cls, content: ContentBlock) -> dict[str, Any]:
6539
"""Format an OpenAI compatible content block.
@@ -86,7 +60,7 @@ def format_request_message_content(cls, content: ContentBlock) -> dict[str, Any]
8660

8761
if "image" in content:
8862
mime_type = mimetypes.types_map.get(f".{content['image']['format']}", "application/octet-stream")
89-
image_data = OpenAIModel.b64encode(content["image"]["source"]["bytes"]).decode("utf-8")
63+
image_data = base64.b64encode(content["image"]["source"]["bytes"]).decode("utf-8")
9064

9165
return {
9266
"image_url": {

tests/strands/types/models/test_openai.py

Lines changed: 0 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
import base64
21
import unittest.mock
32

43
import pytest
@@ -96,23 +95,6 @@ def system_prompt():
9695
"type": "image_url",
9796
},
9897
),
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=",
112-
},
113-
"type": "image_url",
114-
},
115-
),
11698
# Text
11799
(
118100
{"text": "hello"},
@@ -367,15 +349,3 @@ def test_format_chunk_unknown_type(model):
367349

368350
with pytest.raises(RuntimeError, match="chunk_type=<unknown> | unknown type"):
369351
model.format_chunk(event)
370-
371-
372-
@pytest.mark.parametrize(
373-
("data", "exp_result"),
374-
[
375-
(b"image", b"aW1hZ2U="),
376-
(b"aW1hZ2U=", b"aW1hZ2U="),
377-
],
378-
)
379-
def test_b64encode(data, exp_result):
380-
tru_result = SAOpenAIModel.b64encode(data)
381-
assert tru_result == exp_result

0 commit comments

Comments
 (0)