@@ -335,17 +335,22 @@ def _process_messages(
335
335
return messages , system_prompt
336
336
337
337
@staticmethod
338
- def _base64_to_bytes ( image_url : str ) -> bytes :
338
+ def _b64_data_url_to_bytes ( b64_data_url : str ) -> bytes :
339
339
"""
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.
341
341
Returns the raw image bytes.
342
342
"""
343
- if not image_url .startswith ("data:image/" ):
343
+ if not b64_data_url .startswith ("data:image/" ):
344
344
raise ValueError ("Invalid Base64 image URL" )
345
345
346
- base64_data = re .sub (r"^data:image/[^;]+;base64," , "" , image_url )
346
+ base64_data = re .sub (r"^data:image/[^;]+;base64," , "" , b64_data_url )
347
347
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
+ )
349
354
350
355
@staticmethod
351
356
def _get_img_format_from_bytes (image_bytes : bytes ) -> str :
@@ -377,7 +382,7 @@ def _get_image_bytes(image_url: str) -> bytes:
377
382
- If it's a normal URL, downloads and encodes the image in Base64.
378
383
"""
379
384
if image_url .startswith ("data:image/" ):
380
- return BedrockConverseProvider ._base64_to_bytes (image_url )
385
+ return BedrockConverseProvider ._b64_data_url_to_bytes (image_url )
381
386
382
387
elif image_url .startswith (("http://" , "https://" )):
383
388
response = requests .get (image_url )
0 commit comments