@@ -335,17 +335,22 @@ def _process_messages(
335335 return messages , system_prompt
336336
337337 @staticmethod
338- def _base64_to_bytes ( image_url : str ) -> bytes :
338+ def _b64_data_url_to_bytes ( b64_data_url : str ) -> bytes :
339339 """
340- Extracts and decodes Base64 image data from a 'data:image/...;base64,...' URL.
340+ Extracts and decodes Base64 image data from a 'data:image/...;base64,...' data URL.
341341 Returns the raw image bytes.
342342 """
343- if not image_url .startswith ("data:image/" ):
343+ if not b64_data_url .startswith ("data:image/" ):
344344 raise ValueError ("Invalid Base64 image URL" )
345345
346- base64_data = re .sub (r"^data:image/[^;]+;base64," , "" , image_url )
346+ base64_data = re .sub (r"^data:image/[^;]+;base64," , "" , b64_data_url )
347347
348- return base64 .b64decode (base64_data )
348+ try :
349+ return base64 .b64decode (base64_data )
350+ except Exception as e :
351+ raise ValueError (
352+ f"Failed to decode Base64: { e } ; For Base64 Data Url: { b64_data_url } "
353+ )
349354
350355 @staticmethod
351356 def _get_img_format_from_bytes (image_bytes : bytes ) -> str :
@@ -377,7 +382,7 @@ def _get_image_bytes(image_url: str) -> bytes:
377382 - If it's a normal URL, downloads and encodes the image in Base64.
378383 """
379384 if image_url .startswith ("data:image/" ):
380- return BedrockConverseProvider ._base64_to_bytes (image_url )
385+ return BedrockConverseProvider ._b64_data_url_to_bytes (image_url )
381386
382387 elif image_url .startswith (("http://" , "https://" )):
383388 response = requests .get (image_url )
0 commit comments