test(gemini): de-flake test_gemini_image_size_limit_exceeded

Mock the image fetch instead of downloading a 50MB+ image from
upload.wikimedia.org. The runner was intermittently rate-limited
(HTTP 429), so the code raised "Unable to fetch image ... Status
code: 429" and the size-limit assertions failed even though
pytest.raises(litellm.ImageFetchError) still matched.

Mirror the established LargeImageClient pattern in
tests/test_litellm/litellm_core_utils/test_image_handling.py: stub
litellm.module_level_client with a response whose Content-Length
exceeds the 50MB limit and bypass SSRF validation, so the
size-limit rejection path is exercised deterministically with no
external network dependency.
This commit is contained in:
Yuneng Jiang
2026-05-15 22:34:57 -07:00
parent c706184444
commit 1cf2d7f286
+35 -2
View File
@@ -1594,13 +1594,46 @@ def test_gemini_31_flash_lite_reasoning_effort_minimal():
), "gemini-3.1-flash-lite-preview should use thinkingLevel, not thinkingBudget"
def test_gemini_image_size_limit_exceeded():
def test_gemini_image_size_limit_exceeded(monkeypatch):
"""
Test that large images exceeding MAX_IMAGE_URL_DOWNLOAD_SIZE_MB are rejected.
This validates that the 50MB default limit prevents downloading very large images
that could cause memory issues and pod crashes.
The image fetch is mocked (mirroring the LargeImageClient pattern in
tests/test_litellm/litellm_core_utils/test_image_handling.py) so the test
deterministically exercises the size-limit rejection path without any
external network dependency.
"""
from httpx import Request, Response
from litellm.litellm_core_utils.prompt_templates import image_handling
class LargeImageClient:
"""Returns a response whose Content-Length exceeds the 50MB limit."""
def get(self, url, follow_redirects=True):
size_bytes = int(100 * 1024 * 1024) # 100MB > 50MB default limit
return Response(
status_code=200,
headers={
"Content-Type": "image/jpeg",
"Content-Length": str(size_bytes),
},
content=b"x" * size_bytes,
request=Request("GET", url),
)
# Bypass SSRF validation (which would resolve DNS / hit the network) and
# route straight to our mocked client.
monkeypatch.setattr(
image_handling,
"safe_get",
lambda client, url, **kw: client.get(url, follow_redirects=True),
)
monkeypatch.setattr(litellm, "module_level_client", LargeImageClient())
messages = [
{
"role": "user",
@@ -1608,7 +1641,7 @@ def test_gemini_image_size_limit_exceeded():
{"type": "text", "text": "What is in this image?"},
{
"type": "image_url",
"image_url": "https://upload.wikimedia.org/wikipedia/commons/5/51/Blue_Marble_2002.jpg",
"image_url": "https://example.com/large-image.jpg",
},
],
}