From 2ac9b2442bc070e2dd476734513e13f58a6cb10e Mon Sep 17 00:00:00 2001 From: Miguel Neves Date: Fri, 2 May 2025 15:02:58 +0100 Subject: [PATCH] chore: updated b64 converse errors and naming for clarity. added make integration-tests command. --- Makefile | 3 +++ .../providers/bedrock_converse.py | 17 +++++++++++------ 2 files changed, 14 insertions(+), 6 deletions(-) diff --git a/Makefile b/Makefile index 6c957607..1099e2bb 100644 --- a/Makefile +++ b/Makefile @@ -3,3 +3,6 @@ format: unit-tests: pytest libs/core/tests/unit_tests + +integration-tests: + pytest libs/llmstudio/tests/integration_tests \ No newline at end of file diff --git a/libs/core/llmstudio_core/providers/bedrock_converse.py b/libs/core/llmstudio_core/providers/bedrock_converse.py index abc990f7..6afeffcf 100644 --- a/libs/core/llmstudio_core/providers/bedrock_converse.py +++ b/libs/core/llmstudio_core/providers/bedrock_converse.py @@ -335,17 +335,22 @@ def _process_messages( return messages, system_prompt @staticmethod - def _base64_to_bytes(image_url: str) -> bytes: + def _b64_data_url_to_bytes(b64_data_url: str) -> bytes: """ - Extracts and decodes Base64 image data from a 'data:image/...;base64,...' URL. + Extracts and decodes Base64 image data from a 'data:image/...;base64,...' data URL. Returns the raw image bytes. """ - if not image_url.startswith("data:image/"): + if not b64_data_url.startswith("data:image/"): raise ValueError("Invalid Base64 image URL") - base64_data = re.sub(r"^data:image/[^;]+;base64,", "", image_url) + base64_data = re.sub(r"^data:image/[^;]+;base64,", "", b64_data_url) - return base64.b64decode(base64_data) + try: + return base64.b64decode(base64_data) + except Exception as e: + raise ValueError( + f"Failed to decode Base64: {e} ; For Base64 Data Url: {b64_data_url}" + ) @staticmethod def _get_img_format_from_bytes(image_bytes: bytes) -> str: @@ -377,7 +382,7 @@ def _get_image_bytes(image_url: str) -> bytes: - If it's a normal URL, downloads and encodes the image in Base64. """ if image_url.startswith("data:image/"): - return BedrockConverseProvider._base64_to_bytes(image_url) + return BedrockConverseProvider._b64_data_url_to_bytes(image_url) elif image_url.startswith(("http://", "https://")): response = requests.get(image_url)