mirror of
https://github.com/BerriAI/litellm.git
synced 2026-09-27 01:22:18 +00:00
* ci: run the unit_selection.sh shard files on every event instead of only fork pull requests Co-Authored-By: Devin AI <158243242+devin-ai-integration[bot]@users.noreply.github.com> * ci: rename fork-flag to unit-flag now that it applies on every event * test: move tests/test_litellm root and small trees into tests/unit Pure renames, no content changes. Follow-up commits in this PR fix references, merge the three files that already existed in tests/unit, keep live-provider tests in tests/test_litellm and wire CI. * test: carry tests/test_litellm conftest isolation into tests/unit Callback lists, routing fallbacks, cached HTTP clients, logger state, AWS, proxy-URL and keychain env, and session-end client cleanup now reset for unit tests too. The environment isolation owns its MonkeyPatch so a test's own monkeypatch is undone before the model-cost teardown runs. * test: merge, split and prune the moved root and small-tree tests Merge batches/test_batch_utils.py and the chat_completions and messages dispatch tests into the files that already existed in tests/unit. Keep the live Gemini interactions tests, the async image-fetch format test and the OpenAI embedding scorer test in tests/test_litellm since they need real network or keys. Put test_router.py under tests/unit/test_router so the existing package no longer shadows it. Delete eight tests the audit found superseded by stronger ones kept in this move. * ci: run the moved root and small-tree tests under their legacy flags Add the misc and responses-caching-types flags to unit_selection.sh and CircleCI, extend enterprise-routing and mcp-integration, and point the legacy GHA shards, Makefile, redis-compat workflow, merge smoke manifest and change classifier at the new paths. * test: make the new tests/unit directories packages tests/unit/test_package_layout.py requires every directory to carry an __init__.py, and without one the moved and retained test_litellm_responses_bridge.py modules collide on import. * test: scope the unit socket block to tests/unit in shared sessions The GHA shards collect the legacy test-path and the unit selection in one pytest session. The unit conftest's loopback-only block leaked into legacy modules that reach the network at import. The legacy conftest now lifts the restriction at collect and setup time, and the unit conftest re-applies it when collecting its own modules. * test: move tests/test_litellm/llms into tests/unit/llms Rename-only. Moves the provider tests and the fine-tuning fixtures they load, mirroring the old paths. Follow-up commits merge, split and wire them. * test: merge, split and prune the moved llms tests Merges the Databricks chat transformation tests into the existing unit file, keeps the tests that need real keys or the network in tests/test_litellm, deletes the audited tests a stronger unit test already covers, and points imports at tests.unit.llms. * ci: run the moved llms tests under their legacy flags The Vertex AI and All Other Providers shards keep their legacy test-path for the retained files and add the llm-vertex-ai and llm-other-providers unit selections. CircleCI gets matching unit jobs. * test: make the tests/unit/llms directories packages Adds __init__.py to the moved dirs and drops the legacy ones whose directories no longer hold tests. * test: drop script runners and path hacks the llms split left dangling The __main__ runners in the split openai_like files and the Databricks e2e runner called tests that now live in the other half of the split or were deleted. The retained legacy halves also no longer need sys.path edits. * test: give the shard-script tests their own GITHUB_OUTPUT They only passed where the runner set it. The CircleCI unit job's env allowlist drops it, so the script's redirect failed there. * test: point the router and module-deletion checks at tests/unit router_code_coverage and code_qa_check_tests only searched tests/test_litellm, so the moved router tests no longer counted. The two silent-experiment tests the audit deleted were the only direct callers of those methods; they are replaced with tests that assert the forwarded shadow request and the recursion guard. * test: keep the Databricks manual e2e runner and fix the SageMaker Nova run path The Databricks e2e file is a manual script whose main() calls the tests that were pruned, so pruning them broke the documented run. It is back to its main version. The SageMaker Nova docstring now points at the file's real location in tests/local_testing. * test: keep the job's UNIT_FLAG out of the shard-script tests --------- Co-authored-by: Devin AI <158243242+devin-ai-integration[bot]@users.noreply.github.com>
433 lines
17 KiB
Python
433 lines
17 KiB
Python
"""
|
|
Tests for handling malformed or invalid 'file' content blocks (missing or null
|
|
`file` sub-field, HTTP file_id URLs for Google AI Studio).
|
|
|
|
Regression tests for:
|
|
- litellm/llms/vertex_ai/gemini/transformation.py
|
|
- litellm/llms/gemini/chat/transformation.py
|
|
- litellm/litellm_core_utils/prompt_templates/common_utils.py
|
|
(migrate_file_to_image_url raises on missing `file`; file-id helpers skip non-OpenAI shapes)
|
|
- litellm/litellm_core_utils/prompt_templates/factory.py (Bedrock + Anthropic)
|
|
- litellm/llms/openai/chat/gpt_transformation.py
|
|
"""
|
|
|
|
import asyncio
|
|
import copy
|
|
from typing import List, cast
|
|
|
|
import pytest
|
|
|
|
import litellm
|
|
from litellm.litellm_core_utils.prompt_templates.common_utils import (
|
|
get_file_ids_from_messages,
|
|
migrate_file_to_image_url,
|
|
update_messages_with_model_file_ids,
|
|
)
|
|
from litellm.litellm_core_utils.prompt_templates.factory import (
|
|
BedrockConverseMessagesProcessor,
|
|
anthropic_process_openai_file_message,
|
|
)
|
|
from litellm.llms.gemini.chat.transformation import GoogleAIStudioGeminiConfig
|
|
from litellm.llms.openai.chat.gpt_transformation import OpenAIGPTConfig
|
|
from litellm.llms.vertex_ai.gemini.transformation import (
|
|
_gemini_convert_messages_with_history,
|
|
)
|
|
from litellm.types.llms.openai import (
|
|
AllMessageValues,
|
|
ChatCompletionFileObject,
|
|
OpenAIMessageContentListBlock,
|
|
)
|
|
|
|
_MALFORMED_MESSAGES_RAW = [
|
|
{
|
|
"role": "user",
|
|
"content": [
|
|
{"type": "text", "text": "hello"},
|
|
{"type": "file"}, # Missing required "file" sub-field
|
|
],
|
|
}
|
|
]
|
|
|
|
_WELL_FORMED_MESSAGES_RAW = [
|
|
{
|
|
"role": "user",
|
|
"content": [
|
|
{"type": "text", "text": "hello"},
|
|
{
|
|
"type": "file",
|
|
"file": {"file_id": "file-abc123", "format": "pdf"},
|
|
},
|
|
],
|
|
}
|
|
]
|
|
|
|
MALFORMED_FILE_OBJECT: ChatCompletionFileObject = cast(
|
|
ChatCompletionFileObject, {"type": "file"}
|
|
)
|
|
|
|
EXPLICIT_NULL_FILE_OBJECT: ChatCompletionFileObject = cast(
|
|
ChatCompletionFileObject,
|
|
{"type": "file", "file": None},
|
|
)
|
|
|
|
|
|
def _malformed() -> List[AllMessageValues]:
|
|
return copy.deepcopy(cast(List[AllMessageValues], _MALFORMED_MESSAGES_RAW))
|
|
|
|
|
|
def _well_formed() -> List[AllMessageValues]:
|
|
return copy.deepcopy(cast(List[AllMessageValues], _WELL_FORMED_MESSAGES_RAW))
|
|
|
|
|
|
def _explicit_null_file_in_content() -> List[AllMessageValues]:
|
|
return copy.deepcopy(
|
|
cast(
|
|
List[AllMessageValues],
|
|
[
|
|
{
|
|
"role": "user",
|
|
"content": [
|
|
{"type": "text", "text": "hello"},
|
|
{"type": "file", "file": None},
|
|
],
|
|
}
|
|
],
|
|
)
|
|
)
|
|
|
|
|
|
# ---------------------------------------------------------------------------
|
|
# vertex_ai/gemini/transformation.py
|
|
# ---------------------------------------------------------------------------
|
|
|
|
|
|
def test_gemini_convert_messages_malformed_file_raises_bad_request():
|
|
"""_gemini_convert_messages_with_history should raise BadRequestError (not KeyError)
|
|
when a content block has type='file' but no 'file' sub-field."""
|
|
with pytest.raises(litellm.BadRequestError, match="missing the required 'file' field"):
|
|
_gemini_convert_messages_with_history(
|
|
messages=_malformed(),
|
|
model="gemini-2.0-flash",
|
|
)
|
|
|
|
|
|
def test_gemini_convert_messages_explicit_null_file_field_raises_bad_request():
|
|
"""Explicit JSON null for `file` must be rejected like a missing `file` key."""
|
|
with pytest.raises(litellm.BadRequestError, match="missing the required 'file' field"):
|
|
_gemini_convert_messages_with_history(
|
|
messages=_explicit_null_file_in_content(),
|
|
model="gemini-2.0-flash",
|
|
)
|
|
|
|
|
|
# ---------------------------------------------------------------------------
|
|
# gemini/chat/transformation.py - GoogleAIStudioGeminiConfig
|
|
# ---------------------------------------------------------------------------
|
|
|
|
|
|
def test_google_ai_studio_transform_messages_malformed_file_raises_bad_request():
|
|
"""GoogleAIStudioGeminiConfig._transform_messages should raise BadRequestError
|
|
when a content block has type='file' but no 'file' sub-field."""
|
|
config = GoogleAIStudioGeminiConfig()
|
|
with pytest.raises(litellm.BadRequestError, match="missing the required 'file' field"):
|
|
config._transform_messages(messages=_malformed(), model="gemini-2.0-flash")
|
|
|
|
|
|
def test_google_ai_studio_transform_messages_explicit_null_file_field_raises_bad_request():
|
|
"""Explicit JSON null for `file` must be rejected like a missing `file` key."""
|
|
config = GoogleAIStudioGeminiConfig()
|
|
with pytest.raises(litellm.BadRequestError, match="missing the required 'file' field"):
|
|
config._transform_messages(
|
|
messages=_explicit_null_file_in_content(), model="gemini-2.0-flash"
|
|
)
|
|
|
|
|
|
def test_google_ai_studio_transform_messages_http_file_id_converts_to_base64(monkeypatch):
|
|
"""Google AI Studio rejects raw HTTP(S) file URLs; _transform_messages should
|
|
fetch and replace them with base64 `file_data` before conversion."""
|
|
# Data URL shape so downstream Gemini media parsing accepts the inlined bytes
|
|
# (mirrors real `convert_url_to_base64` output from `_process_image_response`).
|
|
fake_file_data = "data:application/pdf;base64,aGVsbG8="
|
|
|
|
def _fake_convert_url_to_base64(url: str) -> str:
|
|
assert url == "https://example.com/doc.pdf"
|
|
return fake_file_data
|
|
|
|
monkeypatch.setattr(
|
|
"litellm.llms.gemini.chat.transformation.convert_url_to_base64",
|
|
_fake_convert_url_to_base64,
|
|
)
|
|
messages = cast(
|
|
List[AllMessageValues],
|
|
[
|
|
{
|
|
"role": "user",
|
|
"content": [
|
|
{"type": "text", "text": "hello"},
|
|
{
|
|
"type": "file",
|
|
"file": {
|
|
"file_id": "https://example.com/doc.pdf",
|
|
"format": "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_data") == fake_file_data
|
|
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
|
|
# ---------------------------------------------------------------------------
|
|
|
|
|
|
def test_update_messages_with_model_file_ids_malformed_skips_non_openai_file_block():
|
|
"""Non-OpenAI file blocks (e.g. missing nested `file` dict) are skipped so callers
|
|
relying on LangChain v1 / provider-native shapes are not rejected here."""
|
|
messages = _malformed()
|
|
result = update_messages_with_model_file_ids(
|
|
messages=messages,
|
|
model_id="some-model",
|
|
model_file_id_mapping={},
|
|
)
|
|
assert result == messages
|
|
content = result[0].get("content")
|
|
assert isinstance(content, list)
|
|
file_block = next(c for c in content if isinstance(c, dict) and c.get("type") == "file")
|
|
assert "file" not in file_block
|
|
|
|
|
|
def test_update_messages_with_model_file_ids_well_formed_updates():
|
|
"""update_messages_with_model_file_ids should update file_id for well-formed blocks."""
|
|
mapping = {"file-abc123": {"some-model": "provider-file-xyz"}}
|
|
result = update_messages_with_model_file_ids(
|
|
messages=_well_formed(),
|
|
model_id="some-model",
|
|
model_file_id_mapping=mapping,
|
|
)
|
|
content = result[0].get("content")
|
|
assert isinstance(content, list)
|
|
file_block = next(c for c in content if c.get("type") == "file")
|
|
assert file_block.get("file", {}).get("file_id") == "provider-file-xyz"
|
|
|
|
|
|
# ---------------------------------------------------------------------------
|
|
# common_utils.py - get_file_ids_from_messages
|
|
# ---------------------------------------------------------------------------
|
|
|
|
|
|
def test_get_file_ids_from_messages_malformed_skips_non_openai_file_block():
|
|
"""Blocks with type='file' but no OpenAI `file` sub-dict yield no extracted ids."""
|
|
assert get_file_ids_from_messages(messages=_malformed()) == []
|
|
|
|
|
|
def test_get_file_ids_from_messages_well_formed_returns_ids():
|
|
"""get_file_ids_from_messages should extract file_id from well-formed blocks."""
|
|
messages: List[AllMessageValues] = cast(
|
|
List[AllMessageValues],
|
|
[
|
|
{
|
|
"role": "user",
|
|
"content": [
|
|
{"type": "text", "text": "hello"},
|
|
{"type": "file", "file": {"file_id": "file-abc123", "format": "pdf"}},
|
|
],
|
|
}
|
|
],
|
|
)
|
|
result = get_file_ids_from_messages(messages=messages)
|
|
assert result == ["file-abc123"]
|
|
|
|
|
|
# ---------------------------------------------------------------------------
|
|
# factory.py - BedrockConverseMessagesProcessor (sync + async)
|
|
# ---------------------------------------------------------------------------
|
|
|
|
|
|
def test_bedrock_process_file_message_malformed_raises_bad_request():
|
|
"""_process_file_message should raise BadRequestError (not KeyError)
|
|
when the file object is missing the 'file' sub-field."""
|
|
with pytest.raises(litellm.BadRequestError, match="missing the required 'file' field"):
|
|
BedrockConverseMessagesProcessor._process_file_message(MALFORMED_FILE_OBJECT)
|
|
|
|
|
|
def test_bedrock_process_file_message_explicit_null_file_field_raises_bad_request():
|
|
with pytest.raises(litellm.BadRequestError, match="missing the required 'file' field"):
|
|
BedrockConverseMessagesProcessor._process_file_message(EXPLICIT_NULL_FILE_OBJECT)
|
|
|
|
|
|
def test_bedrock_async_process_file_message_malformed_raises_bad_request():
|
|
"""_async_process_file_message should raise BadRequestError (not KeyError)
|
|
when the file object is missing the 'file' sub-field."""
|
|
|
|
async def _run() -> None:
|
|
with pytest.raises(
|
|
litellm.BadRequestError, match="missing the required 'file' field"
|
|
):
|
|
await BedrockConverseMessagesProcessor._async_process_file_message(
|
|
MALFORMED_FILE_OBJECT
|
|
)
|
|
|
|
asyncio.run(_run())
|
|
|
|
|
|
def test_bedrock_async_process_file_message_explicit_null_file_field_raises_bad_request():
|
|
async def _run() -> None:
|
|
with pytest.raises(
|
|
litellm.BadRequestError, match="missing the required 'file' field"
|
|
):
|
|
await BedrockConverseMessagesProcessor._async_process_file_message(
|
|
EXPLICIT_NULL_FILE_OBJECT
|
|
)
|
|
|
|
asyncio.run(_run())
|
|
|
|
|
|
# ---------------------------------------------------------------------------
|
|
# openai/chat/gpt_transformation.py
|
|
# ---------------------------------------------------------------------------
|
|
|
|
|
|
def test_openai_apply_common_transform_malformed_file_raises_bad_request():
|
|
"""_apply_common_transform_content_item should raise BadRequestError (not KeyError)
|
|
when a content block has type='file' but no 'file' sub-field."""
|
|
config = OpenAIGPTConfig()
|
|
malformed_block: OpenAIMessageContentListBlock = cast(
|
|
OpenAIMessageContentListBlock, {"type": "file"}
|
|
)
|
|
with pytest.raises(litellm.BadRequestError, match="missing the required 'file' field"):
|
|
config._apply_common_transform_content_item(malformed_block)
|
|
|
|
|
|
def test_openai_apply_common_transform_explicit_null_file_field_raises_bad_request():
|
|
config = OpenAIGPTConfig()
|
|
explicit_null_block: OpenAIMessageContentListBlock = cast(
|
|
OpenAIMessageContentListBlock,
|
|
{"type": "file", "file": None},
|
|
)
|
|
with pytest.raises(litellm.BadRequestError, match="missing the required 'file' field"):
|
|
config._apply_common_transform_content_item(explicit_null_block)
|
|
|
|
|
|
def test_openai_apply_common_transform_well_formed_file_does_not_raise():
|
|
"""_apply_common_transform_content_item should not raise for well-formed file blocks."""
|
|
config = OpenAIGPTConfig()
|
|
well_formed_block: OpenAIMessageContentListBlock = cast(
|
|
OpenAIMessageContentListBlock,
|
|
{"type": "file", "file": {"file_id": "file-abc123"}},
|
|
)
|
|
result = config._apply_common_transform_content_item(well_formed_block)
|
|
assert result.get("type") == "file"
|
|
file_field = cast(ChatCompletionFileObject, result).get("file", {})
|
|
assert file_field.get("file_id") == "file-abc123"
|
|
|
|
|
|
# ---------------------------------------------------------------------------
|
|
# factory.py - anthropic_process_openai_file_message
|
|
# ---------------------------------------------------------------------------
|
|
|
|
|
|
def test_anthropic_process_openai_file_message_malformed_raises_bad_request():
|
|
"""anthropic_process_openai_file_message should raise BadRequestError (not KeyError)
|
|
when the file object is missing the 'file' sub-field."""
|
|
with pytest.raises(litellm.BadRequestError, match="missing the required 'file' field"):
|
|
anthropic_process_openai_file_message(MALFORMED_FILE_OBJECT)
|
|
|
|
|
|
def test_anthropic_process_openai_file_message_explicit_null_file_field_raises_bad_request():
|
|
with pytest.raises(litellm.BadRequestError, match="missing the required 'file' field"):
|
|
anthropic_process_openai_file_message(EXPLICIT_NULL_FILE_OBJECT)
|
|
|
|
|
|
def test_anthropic_process_openai_file_message_well_formed_file_id_does_not_raise():
|
|
"""anthropic_process_openai_file_message should not raise for a well-formed file_id block."""
|
|
well_formed: ChatCompletionFileObject = cast(
|
|
ChatCompletionFileObject,
|
|
{"type": "file", "file": {"file_id": "file-abc123"}},
|
|
)
|
|
result = anthropic_process_openai_file_message(well_formed)
|
|
assert result.get("type") in ("document", "image", "container_upload")
|
|
|
|
|
|
# ---------------------------------------------------------------------------
|
|
# common_utils.py - migrate_file_to_image_url
|
|
# ---------------------------------------------------------------------------
|
|
|
|
|
|
def test_migrate_file_to_image_url_malformed_raises_bad_request():
|
|
"""migrate_file_to_image_url should raise BadRequestError (not KeyError)
|
|
when the file object is missing the 'file' sub-field."""
|
|
with pytest.raises(litellm.BadRequestError, match="missing the required 'file' field"):
|
|
migrate_file_to_image_url(MALFORMED_FILE_OBJECT)
|
|
|
|
|
|
def test_migrate_file_to_image_url_explicit_null_file_field_raises_bad_request():
|
|
with pytest.raises(litellm.BadRequestError, match="missing the required 'file' field"):
|
|
migrate_file_to_image_url(EXPLICIT_NULL_FILE_OBJECT)
|
|
|
|
|
|
def test_migrate_file_to_image_url_well_formed_returns_image_url():
|
|
"""migrate_file_to_image_url should return an image_url block for a well-formed file."""
|
|
well_formed: ChatCompletionFileObject = cast(
|
|
ChatCompletionFileObject,
|
|
{"type": "file", "file": {"file_id": "file-abc123", "format": "png"}},
|
|
)
|
|
result = migrate_file_to_image_url(well_formed)
|
|
assert result.get("type") == "image_url"
|
|
image_url = result.get("image_url", {})
|
|
assert isinstance(image_url, dict)
|
|
assert image_url.get("url") == "file-abc123"
|