litellm/tests/unit/rag/ingestion/test_gemini_ingestion.py
Mateo Wang d80f8c28ca
refactor(types): replace Any with proven types in 137 files (#44478)
* refactor(types): prove runtime types at harness, search, rag and client boundaries

Replace Any with adapter-validated types in the litellm.agent() harness, the
search provider transformations, RAG ingestion and query, the vector store
pre-call hook and registry, the galileo and opik logging integrations and the
proxy client CLI. Each boundary gets unit tests for well-formed and malformed
payloads.

* chore(typing): prove types at more provider boundaries and restore search transformations

Second pass over a2a, embedding, rerank, image, audio and small provider
modules. Search transformations go back to their previous form because
validating their response bodies would change the proxy status for
malformed upstream bodies from 400 to 500.

* refactor(types): prove types at logging, files, rerank, image, audio and management boundaries

Replace Any with validated or annotation-only types in 37 more files: logging
integrations, token counters, provider files/rerank/image generation/audio
transcription transformations, pass-through logging handlers and management
endpoints. No proxy HTTP status or error type changes.

* refactor(types): prove types at repository, spend, files and router boundaries

Replace Any with repository table accessors, validated mappings and
annotation-only types in 33 more files: Prisma repositories, the enterprise
batch and responses cost checkers, budget reservation, files endpoints,
management endpoints, the policy registry, the adaptive and complexity routers
and the secret managers. No proxy HTTP status or error type changes.

* test(types): run the aiohttp transformation test in-process and cover repository row conversion

The aiohttp chat transformation test no longer starts a server. It feeds the
transformation a response whose json() returns the body under test.

The proxy unit shards now exercise stored model rows whose params are JSON
strings and the object permission create and update paths.
2026-10-05 11:01:45 -07:00

111 lines
4 KiB
Python

import json
from collections.abc import Mapping
from types import MappingProxyType
from typing import Final
import httpx
import pytest
import respx
from pydantic import ValidationError
import litellm
from litellm.rag.ingestion.gemini_ingestion import GeminiRAGIngestion
_API_BASE: Final = "https://gemini.example"
_STORE: Final = "fileSearchStores/docs-1"
_START_UPLOAD_URL: Final = f"{_API_BASE}/upload/v1beta/{_STORE}:uploadToFileSearchStore"
_UPLOAD_SESSION_URL: Final = "https://gemini.example/upload/session-1"
_DOCUMENT: Final = "fileSearchStores/docs-1/documents/notes-1"
def _ingestion(chunking_strategy: object) -> GeminiRAGIngestion:
return GeminiRAGIngestion(
ingest_options={
"chunking_strategy": chunking_strategy,
"vector_store": {
"custom_llm_provider": "gemini",
"vector_store_id": _STORE,
"api_key": "test-key",
"api_base": _API_BASE,
},
}
)
async def _store_notes(ingestion: GeminiRAGIngestion) -> tuple[str | None, str | None]:
return await ingestion.store(
file_content=b"first note",
filename="notes.txt",
content_type="text/plain",
chunks=[],
embeddings=None,
)
@pytest.fixture
def start_upload(monkeypatch: pytest.MonkeyPatch, respx_mock: respx.MockRouter) -> respx.Route:
monkeypatch.setattr(litellm, "disable_aiohttp_transport", True)
litellm.in_memory_llm_clients_cache.flush_cache()
respx_mock.put(_UPLOAD_SESSION_URL).mock(return_value=httpx.Response(200, json={"name": _DOCUMENT}))
return respx_mock.post(_START_UPLOAD_URL).mock(
return_value=httpx.Response(200, headers={"x-goog-upload-url": _UPLOAD_SESSION_URL})
)
@pytest.mark.asyncio
@pytest.mark.parametrize(
("white_space_config", "expected"),
[
(
{"max_tokens_per_chunk": 200, "max_overlap_tokens": 20},
{"maxTokensPerChunk": 200, "maxOverlapTokens": 20},
),
({"max_tokens_per_chunk": 200}, {"maxTokensPerChunk": 200, "maxOverlapTokens": 400}),
({"unrelated": True}, {"maxTokensPerChunk": 800, "maxOverlapTokens": 400}),
(
{"max_tokens_per_chunk": "200", "max_overlap_tokens": None},
{"maxTokensPerChunk": "200", "maxOverlapTokens": None},
),
({7: "not a field", "max_overlap_tokens": 1.5}, {"maxTokensPerChunk": 800, "maxOverlapTokens": 1.5}),
(MappingProxyType({"max_overlap_tokens": 0}), {"maxTokensPerChunk": 800, "maxOverlapTokens": 0}),
],
)
async def test_white_space_config_is_sent_as_the_chunking_config_of_the_upload(
start_upload: respx.Route, white_space_config: Mapping[object, object], expected: Mapping[str, object]
):
stored: Final = await _store_notes(_ingestion({"white_space_config": white_space_config}))
assert stored == (_STORE, _DOCUMENT)
assert json.loads(start_upload.calls.last.request.content) == {
"displayName": "notes.txt",
"chunkingConfig": {"whiteSpaceConfig": expected},
}
@pytest.mark.asyncio
@pytest.mark.parametrize(
"chunking_strategy",
[None, {"type": "auto"}, {"white_space_config": None}, {"white_space_config": {}}, {"white_space_config": 0}],
)
async def test_upload_without_a_white_space_config_sends_no_chunking_config(
start_upload: respx.Route, chunking_strategy: object
):
stored: Final = await _store_notes(_ingestion(chunking_strategy))
assert stored == (_STORE, _DOCUMENT)
assert json.loads(start_upload.calls.last.request.content) == {"displayName": "notes.txt"}
@pytest.mark.asyncio
@pytest.mark.parametrize(
"white_space_config",
["private-setting", [800, 400], [("max_tokens_per_chunk", 200)], 800, True, 1.5],
)
async def test_a_white_space_config_that_is_not_a_mapping_is_rejected_before_any_upload(
start_upload: respx.Route, white_space_config: object
):
with pytest.raises(ValidationError) as raised:
await _store_notes(_ingestion({"white_space_config": white_space_config}))
assert "private-setting" not in str(raised.value)
assert not start_upload.called