From 407dd73ae9a9e5316b95a6a6ece8758155986798 Mon Sep 17 00:00:00 2001 From: Patrick Gray Date: Mon, 7 Jul 2025 20:16:45 +0000 Subject: [PATCH] models - openai - do not accept b64 images --- src/strands/types/models/openai.py | 28 +-------------------- tests/strands/types/models/test_openai.py | 30 ----------------------- 2 files changed, 1 insertion(+), 57 deletions(-) diff --git a/src/strands/types/models/openai.py b/src/strands/types/models/openai.py index 30971c2ba..09d24bd80 100644 --- a/src/strands/types/models/openai.py +++ b/src/strands/types/models/openai.py @@ -34,32 +34,6 @@ 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. @@ -86,7 +60,7 @@ 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 = OpenAIModel.b64encode(content["image"]["source"]["bytes"]).decode("utf-8") + image_data = base64.b64encode(content["image"]["source"]["bytes"]).decode("utf-8") return { "image_url": { diff --git a/tests/strands/types/models/test_openai.py b/tests/strands/types/models/test_openai.py index dc43b3fcd..5baa7e709 100644 --- a/tests/strands/types/models/test_openai.py +++ b/tests/strands/types/models/test_openai.py @@ -1,4 +1,3 @@ -import base64 import unittest.mock import pytest @@ -96,23 +95,6 @@ def system_prompt(): "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", - }, - ), # Text ( {"text": "hello"}, @@ -367,15 +349,3 @@ def test_format_chunk_unknown_type(model): with pytest.raises(RuntimeError, match="chunk_type= | 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