litellm/tests/unit/rag/ingestion/test_openai_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

101 lines
3.1 KiB
Python

import json
from typing import Final
import httpx
import pytest
import respx
import litellm
from litellm.rag.ingestion.openai_ingestion import OpenAIRAGIngestion
_API_BASE: Final = "https://openai.example/v1"
_STATIC_CHUNKING: Final = {"type": "static", "static": {"max_chunk_size_tokens": 800, "chunk_overlap_tokens": 400}}
_ATTACHED_FILE: Final = {
"id": "file-1",
"object": "vector_store.file",
"created_at": 1767323045,
"vector_store_id": "vs_1",
"status": "completed",
"usage_bytes": 10,
}
_UPLOADED_FILE: Final = {
"id": "file-1",
"object": "file",
"bytes": 10,
"created_at": 1767323045,
"filename": "notes.txt",
"purpose": "assistants",
"status": "processed",
}
def _ingestion(chunking_strategy: object) -> OpenAIRAGIngestion:
return OpenAIRAGIngestion(
ingest_options={
"chunking_strategy": chunking_strategy,
"vector_store": {
"custom_llm_provider": "openai",
"vector_store_id": "vs_1",
"api_key": "sk-test",
"api_base": _API_BASE,
},
}
)
@pytest.fixture
def attach_file(monkeypatch: pytest.MonkeyPatch, respx_mock: respx.MockRouter) -> respx.Route:
monkeypatch.setattr(litellm, "disable_aiohttp_transport", True)
litellm.in_memory_llm_clients_cache.flush_cache()
return respx_mock.post(f"{_API_BASE}/vector_stores/vs_1/files").mock(
return_value=httpx.Response(200, json=_ATTACHED_FILE)
)
@pytest.mark.asyncio
@pytest.mark.parametrize(
("chunking_strategy", "sent_chunking_strategy"),
[(None, {"type": "auto"}), ({"type": "auto"}, {"type": "auto"}), (_STATIC_CHUNKING, _STATIC_CHUNKING)],
)
async def test_an_uploaded_file_is_attached_to_the_vector_store_with_the_chunking_strategy(
attach_file: respx.Route, respx_mock: respx.MockRouter, chunking_strategy: object, sent_chunking_strategy: object
):
respx_mock.post(f"{_API_BASE}/files").mock(return_value=httpx.Response(200, json=_UPLOADED_FILE))
stored: Final = await _ingestion(chunking_strategy).store(
file_content=b"first note",
filename="notes.txt",
content_type="text/plain",
chunks=[],
embeddings=None,
)
assert stored == ("vs_1", "file-1")
assert json.loads(attach_file.calls.last.request.content) == {
"file_id": "file-1",
"chunking_strategy": sent_chunking_strategy,
}
@pytest.mark.asyncio
@pytest.mark.parametrize(
("chunking_strategy", "sent_chunking_strategy"),
[(None, {"type": "auto"}), (_STATIC_CHUNKING, _STATIC_CHUNKING)],
)
async def test_an_existing_file_is_attached_to_the_vector_store_with_the_chunking_strategy(
attach_file: respx.Route, chunking_strategy: object, sent_chunking_strategy: object
):
stored: Final = await _ingestion(chunking_strategy).store(
file_content=None,
filename=None,
content_type=None,
chunks=[],
embeddings=None,
existing_file_id="file-9",
)
assert stored == ("vs_1", "file-9")
assert json.loads(attach_file.calls.last.request.content) == {
"file_id": "file-9",
"chunking_strategy": sent_chunking_strategy,
}