@@ -34,6 +34,32 @@ 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+
3763 @classmethod
3864 def format_request_message_content (cls , content : ContentBlock ) -> dict [str , Any ]:
3965 """Format an OpenAI compatible content block.
@@ -60,17 +86,8 @@ def format_request_message_content(cls, content: ContentBlock) -> dict[str, Any]
6086
6187 if "image" in content :
6288 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+
7491 return {
7592 "image_url" : {
7693 "detail" : "auto" ,
0 commit comments