From 93d7e304aee2fee5781bb1d05d2460c0aafc62f3 Mon Sep 17 00:00:00 2001 From: S0ngRu1 <1922909737@qq.com> Date: Wed, 13 May 2026 17:11:12 +0800 Subject: [PATCH] test: cover Google AI Studio HTTP file_id convert failure path Co-authored-by: Cursor --- .../llms/test_file_content_block.py | 44 +++++++++++++++++++ 1 file changed, 44 insertions(+) diff --git a/tests/test_litellm/llms/test_file_content_block.py b/tests/test_litellm/llms/test_file_content_block.py index d2401d912fc..5552c1a4d68 100644 --- a/tests/test_litellm/llms/test_file_content_block.py +++ b/tests/test_litellm/llms/test_file_content_block.py @@ -186,6 +186,50 @@ def test_google_ai_studio_transform_messages_http_file_id_converts_to_base64(mon assert "file_id" not in file_field +def test_google_ai_studio_transform_messages_http_file_id_convert_failure_leaves_file_unchanged( + monkeypatch, +): + """If convert_url_to_base64 fails, the Studio prep step must not mutate the block + (see try/except in GoogleAIStudioGeminiConfig._transform_messages).""" + https_id = "https://example.com/missing.pdf" + + def _raise(_url: str) -> str: + raise litellm.ImageFetchError("simulated fetch failure") + + monkeypatch.setattr( + "litellm.llms.gemini.chat.transformation.convert_url_to_base64", + _raise, + ) + messages = cast( + List[AllMessageValues], + [ + { + "role": "user", + "content": [ + {"type": "text", "text": "hello"}, + { + "type": "file", + "file": { + "file_id": https_id, + "format": "application/pdf", + }, + }, + ], + } + ], + ) + config = GoogleAIStudioGeminiConfig() + config._transform_messages(messages=messages, model="gemini-2.0-flash") + content = messages[0].get("content") + assert isinstance(content, list) + file_block = next(c for c in content if isinstance(c, dict) and c.get("type") == "file") + file_field = file_block.get("file") + assert isinstance(file_field, dict) + assert file_field.get("file_id") == https_id + assert file_field.get("format") == "application/pdf" + assert "file_data" not in file_field + + # --------------------------------------------------------------------------- # common_utils.py - update_messages_with_model_file_ids # ---------------------------------------------------------------------------