From 12169cd47b6c1154d7fe61d301bbc34bed7beac1 Mon Sep 17 00:00:00 2001 From: Patrick Gray Date: Thu, 19 Jun 2025 11:59:58 +0000 Subject: [PATCH 1/3] models - openai - images - b64 validate --- src/strands/types/models/openai.py | 10 +++++++++- tests/strands/types/models/test_openai.py | 20 +++++++++++++++++++- 2 files changed, 28 insertions(+), 2 deletions(-) diff --git a/src/strands/types/models/openai.py b/src/strands/types/models/openai.py index 96f758d5..683ec15d 100644 --- a/src/strands/types/models/openai.py +++ b/src/strands/types/models/openai.py @@ -57,7 +57,15 @@ 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: + # TODO: Checking if base64 encoded for backwards compatability. In 1.0 release, we should only + # accept raw bytes and base64 encode on behalf of customers. + base64.b64decode(image_bytes, validate=True) + except ValueError: + image_bytes = base64.b64encode(image_bytes) + + image_data = image_bytes.decode("utf-8") return { "image_url": { "detail": "auto", diff --git a/tests/strands/types/models/test_openai.py b/tests/strands/types/models/test_openai.py index 9db08bc9..2827969d 100644 --- a/tests/strands/types/models/test_openai.py +++ b/tests/strands/types/models/test_openai.py @@ -1,3 +1,4 @@ +import base64 import unittest.mock import pytest @@ -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", }, From 515e921fdaaa78b56c4adc60018e88992e350519 Mon Sep 17 00:00:00 2001 From: Patrick Gray Date: Thu, 19 Jun 2025 09:01:00 -0400 Subject: [PATCH 2/3] log warning --- src/strands/types/models/openai.py | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/src/strands/types/models/openai.py b/src/strands/types/models/openai.py index 683ec15d..075ab957 100644 --- a/src/strands/types/models/openai.py +++ b/src/strands/types/models/openai.py @@ -59,9 +59,11 @@ def format_request_message_content(cls, content: ContentBlock) -> dict[str, Any] mime_type = mimetypes.types_map.get(f".{content['image']['format']}", "application/octet-stream") image_bytes = content["image"]["source"]["bytes"] try: - # TODO: Checking if base64 encoded for backwards compatability. In 1.0 release, we should only - # accept raw bytes and base64 encode on behalf of customers. base64.b64decode(image_bytes, validate=True) + logger.warning( + "issue=<%s> | base64 encoded images will not be accepted in version 0.3.0", + "https://github.com/strands-agents/sdk-python/issues/252" + ) except ValueError: image_bytes = base64.b64encode(image_bytes) From a2fef8d0a0c93595ecaf17d63405702103a969b3 Mon Sep 17 00:00:00 2001 From: Patrick Gray Date: Thu, 19 Jun 2025 09:09:43 -0400 Subject: [PATCH 3/3] warnimg for future version Co-authored-by: Mackenzie Zastrow <3211021+zastrowm@users.noreply.github.com> --- src/strands/types/models/openai.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/strands/types/models/openai.py b/src/strands/types/models/openai.py index 075ab957..0df1dda5 100644 --- a/src/strands/types/models/openai.py +++ b/src/strands/types/models/openai.py @@ -61,7 +61,7 @@ def format_request_message_content(cls, content: ContentBlock) -> dict[str, Any] try: base64.b64decode(image_bytes, validate=True) logger.warning( - "issue=<%s> | base64 encoded images will not be accepted in version 0.3.0", + "issue=<%s> | base64 encoded images will not be accepted in a future version", "https://github.com/strands-agents/sdk-python/issues/252" ) except ValueError: