Skip to content

models - openai - b64encode method #260

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 1 commit into from
Jun 20, 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
39 changes: 28 additions & 11 deletions src/strands/types/models/openai.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,32 @@ class OpenAIModel(Model, abc.ABC):

config: dict[str, Any]

@staticmethod
def b64encode(data: bytes) -> bytes:
"""Base64 encode the provided data.
If the data is already base64 encoded, we do nothing.
Note, this is a temporary method used to provide a warning to users who pass in base64 encoded data. In future
versions, images and documents will be base64 encoded on behalf of customers for consistency with the other
providers and general convenience.
Args:
data: Data to encode.
Returns:
Base64 encoded data.
"""
try:
base64.b64decode(data, validate=True)
logger.warning(
"issue=<%s> | base64 encoded images and documents will not be accepted in future versions",
"https://github.com/strands-agents/sdk-python/issues/252",
)
except ValueError:
data = base64.b64encode(data)

return data

@classmethod
def format_request_message_content(cls, content: ContentBlock) -> dict[str, Any]:
"""Format an OpenAI compatible content block.
Expand All @@ -60,17 +86,8 @@ 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_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")
image_data = OpenAIModel.b64encode(content["image"]["source"]["bytes"]).decode("utf-8")

return {
"image_url": {
"detail": "auto",
Expand Down
12 changes: 12 additions & 0 deletions tests/strands/types/models/test_openai.py
Original file line number Diff line number Diff line change
Expand Up @@ -362,3 +362,15 @@ def test_format_chunk_unknown_type(model):

with pytest.raises(RuntimeError, match="chunk_type=<unknown> | unknown type"):
model.format_chunk(event)


@pytest.mark.parametrize(
("data", "exp_result"),
[
(b"image", b"aW1hZ2U="),
(b"aW1hZ2U=", b"aW1hZ2U="),
],
)
def test_b64encode(data, exp_result):
tru_result = SAOpenAIModel.b64encode(data)
assert tru_result == exp_result