@@ -34,6 +34,32 @@ class OpenAIModel(Model, abc.ABC):
34
34
35
35
config : dict [str , Any ]
36
36
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
+
37
63
@classmethod
38
64
def format_request_message_content (cls , content : ContentBlock ) -> dict [str , Any ]:
39
65
"""Format an OpenAI compatible content block.
@@ -60,17 +86,8 @@ def format_request_message_content(cls, content: ContentBlock) -> dict[str, Any]
60
86
61
87
if "image" in content :
62
88
mime_type = mimetypes .types_map .get (f".{ content ['image' ]['format' ]} " , "application/octet-stream" )
63
- image_bytes = content ["image" ]["source" ]["bytes" ]
64
- try :
65
- base64 .b64decode (image_bytes , validate = True )
66
- logger .warning (
67
- "issue=<%s> | base64 encoded images will not be accepted in a future version" ,
68
- "https://github.com/strands-agents/sdk-python/issues/252" ,
69
- )
70
- except ValueError :
71
- image_bytes = base64 .b64encode (image_bytes )
72
-
73
- image_data = image_bytes .decode ("utf-8" )
89
+ image_data = OpenAIModel .b64encode (content ["image" ]["source" ]["bytes" ]).decode ("utf-8" )
90
+
74
91
return {
75
92
"image_url" : {
76
93
"detail" : "auto" ,
0 commit comments