diff --git a/litellm/litellm_core_utils/prompt_templates/common_utils.py b/litellm/litellm_core_utils/prompt_templates/common_utils.py index 3706fed449d..24fb1e53bbd 100644 --- a/litellm/litellm_core_utils/prompt_templates/common_utils.py +++ b/litellm/litellm_core_utils/prompt_templates/common_utils.py @@ -1567,7 +1567,10 @@ def _extract_ollama_image_data(image_url: str) -> str: convert_url_to_base64, ) - image_url = convert_url_to_base64(image_url) + try: + return _extract_base64_data(convert_url_to_base64(image_url)) + except litellm.ImageFetchError: + return image_url return _extract_base64_data(image_url) diff --git a/tests/test_litellm/litellm_core_utils/test_extract_base64_image.py b/tests/test_litellm/litellm_core_utils/test_extract_base64_image.py index 6958cf1f21a..063bbf32c84 100644 --- a/tests/test_litellm/litellm_core_utils/test_extract_base64_image.py +++ b/tests/test_litellm/litellm_core_utils/test_extract_base64_image.py @@ -117,6 +117,34 @@ class TestExtractImagesFromMessage: mock_convert.assert_called_once_with("https://example.com/image.png") assert result == ["ZmV0Y2hlZA=="] + def test_extract_from_message_falls_back_to_url_when_download_fails(self): + """If the remote download is disabled or fails, fall back to the URL. + + This preserves the prior behavior (forwarding the URL) instead of introducing a new + hard failure for users who set ``MAX_IMAGE_URL_DOWNLOAD_SIZE_MB=0`` or whose image is + unreachable. + """ + import litellm + + message = { + "role": "user", + "content": [ + { + "type": "image_url", + "image_url": {"url": "https://example.com/image.png"}, + } + ], + } + with patch( + "litellm.litellm_core_utils.prompt_templates.image_handling.convert_url_to_base64", + side_effect=litellm.ImageFetchError( + message="boom", model="ollama", llm_provider="ollama" + ), + ): + result = extract_images_from_message(message) + + assert result == ["https://example.com/image.png"] + def test_extract_multiple_images(self): """Test extracting multiple images from a single message""" message = {