Skip to content

The fastest, litest AI Gateway. Rust core with Python SDK. Call 100+ LLM APIs in OpenAI (or native) format with cost tracking, guardrails, load balancing, and logging [Bedrock, Azure, OpenAI, Anthropic, OpenAI, VertexAI, vLLM, Nvidia NIM]

test(rust): add native vs Python parity harness for responses (#46567)

Nate Armstrong4995d319ed54
diff --git a/tests/rust-python-harness/AGENTS.md b/tests/rust-python-harness/AGENTS.md
index 3064960d66e..98a20fa1c38 100644
--- a/tests/rust-python-harness/AGENTS.md
+++ b/tests/rust-python-harness/AGENTS.md
@@ -13,7 +13,7 @@ tests/rust-python-harness/
│ │ ├── __init__.py
│ │ ├── reporting.py
│ │ ├── sdk/
-│ │ │ └── ocr/
+│ │ │ └── responses/
│ │
│ ├── trace_parity/
│ │ ├── __init__.py
@@ -53,6 +53,7 @@ tests/rust-python-harness/
- Examples: `run e2e_parity --surface sdk --function ocr`, `run unit_tests_parity --function ocr --pytest-arg=-x`, or `run all --function ocr`
- `cli/catalog.py` discovers strategies, validates their Python definitions, and orders them. `cli/__init__.py` builds the Click command tree. `cli/commands.py` runs selected cases
- `e2e_parity/` compares SDK objects, exceptions, callbacks, and streams, or gateway HTTP responses
+- `e2e_parity/sdk/<route>/` holds the pytest parity files that send one public SDK call through the Python implementation and through the native route against the same recorded provider response, then compare dispatch, the provider request, the response and the success log. Every known gap is a strict xfail naming the gap. Run them with `uv run pytest -o consider_namespace_packages=true tests/rust-python-harness/strategies/e2e_parity/sdk/responses -q`. They reuse the recording server and isolation helpers from `tests/test_litellm_rust/support`, and they never belong in `tests/test_litellm_rust`, which is native only
- `trace_parity/` profiles the Python call stack and prints every collected Python call under `litellm/`. It never collects Rust spans and never rebuilds the native extension
- E2E and trace strategies load their registered module cases and run surface-specific execution from their folders
- `shared/unit_runners/contracts.py` owns the typed per-function unit contracts consumed by `unit_tests_parity` and `unit_tests_rust`
diff --git a/tests/rust-python-harness/strategies/e2e_parity/sdk/responses/conftest.py b/tests/rust-python-harness/strategies/e2e_parity/sdk/responses/conftest.py
new file mode 100644
index 00000000000..4bb20713c59
--- /dev/null
+++ b/tests/rust-python-harness/strategies/e2e_parity/sdk/responses/conftest.py
@@ -0,0 +1,34 @@
+from collections.abc import AsyncIterator, Generator
+from importlib import import_module
+from pathlib import Path
+from typing import Final
+
+import pytest
+import pytest_asyncio
+
+from tests.test_litellm_rust.support.isolation import isolated_test_state
+from tests.test_litellm_rust.support.recording_server import RecordingServer, recording_service
+
+PARITY_ROOT: Final = Path(__file__).parent
+
+
+@pytest_asyncio.fixture(autouse=True, loop_scope="function")
+async def isolate_test_state() -> AsyncIterator[None]:
+ async with isolated_test_state():
+ yield
+
+
+def recording_server() -> Generator[RecordingServer]:
+ with recording_service() as server:
+ yield server
+
+
+def pytest_collection_modifyitems(items: list[pytest.Item]) -> None:
+ try:
+ import_module("litellm.rust_bridge._native")
+ except ImportError:
+ skip: Final = pytest.mark.skip(reason="requires a compiled litellm.rust_bridge._native extension")
+ for item in items:
+ if PARITY_ROOT in item.path.parents:
+ item.add_marker(skip)
diff --git a/tests/rust-python-harness/strategies/e2e_parity/sdk/responses/test_native_parity.py b/tests/rust-python-harness/strategies/e2e_parity/sdk/responses/test_native_parity.py
new file mode 100644
index 00000000000..0c8efcd36c1
--- /dev/null
+++ b/tests/rust-python-harness/strategies/e2e_parity/sdk/responses/test_native_parity.py
@@ -0,0 +1,320 @@
+"""Native Responses calls must be indistinguishable from the Python implementation.
+
+Each case sends one public `litellm.aresponses` call through Python and through the native route against the same
+recorded OpenAI response, then compares what reached the provider, what the caller got back, and what the success
+callback logged. The native leg fails instead of silently falling back to Python
+"""
+
+from collections.abc import Awaitable, Callable, Iterator, Mapping
+from dataclasses import dataclass, field
+from types import MappingProxyType
+from typing import Final, cast
+
+import pytest
+from pydantic import JsonValue, TypeAdapter
+
+import litellm
+from litellm.responses import dispatch
+from litellm.responses import main as python_responses
+from litellm.responses.utils import ResponsesAPIRequestUtils
+from litellm.rust_bridge import catalog
+from litellm.rust_bridge.catalog import Route, RouteRule
+from litellm.rust_bridge.configuration import Rollout
+from litellm.types.llms.openai import ResponsesAPIResponse
+from tests.test_litellm_rust.support.callback_recorder import RecordingLogger, drain_logging
+from tests.test_litellm_rust.support.isolation import isolated_callback_registries, rebound
+from tests.test_litellm_rust.support.recording_server import RecordingServer, ResponseSpec
+
+pytestmark = pytest.mark.requires_rust_extension
+
+MODEL: Final = "openai/gpt-6.1-sol"
+_OBJECT: Final = TypeAdapter(dict[str, JsonValue])
+_LOGGED: Final = TypeAdapter(dict[str, object])
+_COMPARED_HEADERS: Final = ("authorization", "content-type", "openai-organization", "openai-project")
+_COMPARED_LOG_FIELDS: Final = (
+ "call_type",
+ "custom_llm_provider",
+ "model",
+ "model_group",
+ "model_id",
+ "prompt_tokens",
+ "completion_tokens",
+ "total_tokens",
+ "response_cost",
+ "status",
+)
+
+TEXT_RESPONSE: Final[Mapping[str, JsonValue]] = MappingProxyType(
+ {
+ "id": "resp_0a1b2c3d4e5f",
+ "object": "response",
+ "created_at": 1760486400,
+ "status": "completed",
+ "error": None,
+ "incomplete_details": None,
+ "instructions": None,
+ "max_output_tokens": None,
+ "model": "gpt-6.1-sol-2026-09-30",
+ "output": [
+ {
+ "id": "rs_0a1b2c3d4e5f",
+ "type": "reasoning",
+ "summary": [],
+ },
+ {
+ "id": "msg_0a1b2c3d4e5f",
+ "type": "message",
+ "status": "completed",
+ "role": "assistant",
+ "content": [{"type": "output_text", "annotations": [], "logprobs": [], "text": "Paris"}],
+ },
+ ],
+ "parallel_tool_calls": True,
+ "previous_response_id": None,
+ "reasoning": {"effort": "medium", "summary": None},
+ "store": True,
+ "temperature": 1.0,
+ "text": {"format": {"type": "text"}, "verbosity": "medium"},
+ "tool_choice": "auto",
+ "tools": [],
+ "top_p": 1.0,
+ "truncation": "disabled",
+ "usage": {
+ "input_tokens": 12,
+ "input_tokens_details": {"cached_tokens": 0},
+ "output_tokens": 70,
+ "output_tokens_details": {"reasoning_tokens": 64},
+ "total_tokens": 82,
+ },
+ "user": None,
+ "metadata": {},
+ }
+)
+
+FUNCTION_CALL_RESPONSE: Final[Mapping[str, JsonValue]] = MappingProxyType(
+ {
+ **TEXT_RESPONSE,
+ "id": "resp_6f5e4d3c2b1a",
+ "output": [
+ {
+ "id": "fc_6f5e4d3c2b1a",
+ "type": "function_call",
+ "status": "completed",
+ "arguments": '{"city":"Paris"}',
+ "call_id": "call_6f5e4d3c2b1a",
+ "name": "get_weather",
+ }
+ ],
+ }
+)
+
+WEATHER_TOOL: Final[Mapping[str, JsonValue]] = MappingProxyType(
+ {
+ "type": "function",
+ "name": "get_weather",
+ "description": "Current weather for a city",
+ "parameters": {
+ "type": "object",
+ "properties": {"city": {"type": "string"}},
+ "required": ["city"],
+ },
+ }
+)
+
+
+REASONING_REPLAY_INPUT: Final[list[JsonValue]] = [
+ {"type": "reasoning", "id": "rs_0a1b2c3d4e5f", "summary": [], "status": None},
+ {"role": "user", "content": "And Germany?"},
+]
+
+_PYTHON_ARESPONSES: Final = cast(Callable[..., Awaitable[object]], python_responses.aresponses) # noqa: TID251 # the Python baseline under comparison
+_PUBLIC_ARESPONSES: Final = cast(Callable[..., Awaitable[object]], litellm.aresponses)
+
+
+@dataclass(frozen=True, slots=True)
+class Leg:
+ request_body: Mapping[str, JsonValue]
+ request_headers: Mapping[str, str]
+ response: Mapping[str, JsonValue]
+ logged: Mapping[str, object]
+ fell_back: bool
+
+
+ASPECTS: Final = ("dispatch", "request", "response", "logged")
+
+
+@dataclass(frozen=True, slots=True)
+class Case:
+ name: str
+ kwargs: Mapping[str, object]
+ upstream: Mapping[str, JsonValue] = TEXT_RESPONSE
+ gaps: Mapping[str, str] = field(default_factory=lambda: MappingProxyType({}))
+
+ def params(self) -> Iterator[object]:
+ for aspect in ASPECTS:
+ gap = self.gaps.get(aspect)
+ marks = () if gap is None else (pytest.mark.xfail(reason=gap, strict=True),)
+ yield pytest.param(self, aspect, id=f"{self.name}-{aspect}", marks=marks)
+
+
+ID_ENCODING_GAP: Final = MappingProxyType(
+ {"response": "native responses return the provider's raw response id instead of LiteLLM's encoded resp_ id"}
+)
+
+
+def _gaps(**request_gaps: str) -> Mapping[str, str]:
+ return MappingProxyType({**ID_ENCODING_GAP, **request_gaps})
+
+
+CASES: Final = (
+ Case("plain_text", {"input": "What is the capital of France?"}, gaps=_gaps()),
+ Case(
+ "instructions_and_input_items",
+ {
+ "instructions": "Answer in one word",
+ "input": [{"role": "user", "content": [{"type": "input_text", "text": "Capital of France?"}]}],
+ },
+ gaps=_gaps(),
+ ),
+ Case(
+ "function_tool",
+ {"input": "Weather in Paris?", "tools": [dict(WEATHER_TOOL)], "tool_choice": "auto"},
+ upstream=FUNCTION_CALL_RESPONSE,
+ gaps=_gaps(),
+ ),
+ Case(
+ "encoded_previous_response_id",
+ {
+ "input": [{"type": "function_call_output", "call_id": "call_6f5e4d3c2b1a", "output": "18C and sunny"}],
+ "previous_response_id": ResponsesAPIRequestUtils._build_responses_api_response_id( # pyright: ignore[reportPrivateUsage] # the public encoder needs a response object
+ custom_llm_provider="openai", model_id=None, response_id="resp_6f5e4d3c2b1a"
+ ),
+ },
+ gaps=_gaps(),
+ ),
+ Case(
+ "warm_up_probe_below_minimum_max_output_tokens",
+ {"input": "hi", "max_output_tokens": 1},
+ gaps=_gaps(request="native requests send max_output_tokens below OpenAI's minimum of 16"),
+ ),
+ Case(
+ "gpt_5_family_drops_temperature_while_reasoning",
+ {"input": "hi", "temperature": 0.2, "reasoning": {"effort": "high"}, "drop_params": True},
+ gaps=MappingProxyType(
+ {"dispatch": "native responses decline drop_params instead of applying the GPT-5 sampling rules"}
+ ),
+ ),
+ Case(
+ "anthropic_cache_control_is_stripped",
+ {
+ "input": [
+ {
+ "role": "user",
+ "content": [{"type": "input_text", "text": "hi", "cache_control": {"type": "ephemeral"}}],
+ }
+ ],
+ "tools": [{**WEATHER_TOOL, "cache_control": {"type": "ephemeral"}}],
+ },
+ gaps=_gaps(),
+ ),
+ Case(
+ "reasoning_replay_without_status",
+ {
+ "input": REASONING_REPLAY_INPUT,
+ },
+ gaps=_gaps(request="native requests forward reasoning items with status null"),
+ ),
+ Case(
+ "tool_parameters_sent_as_json_string",
+ {
+ "input": "Weather in Paris?",
+ "tools": [{**WEATHER_TOOL, "parameters": '{"type":"object","properties":{"city":{"type":"string"}}}'}],
+ },
+ gaps=_gaps(request="native requests forward tool parameters encoded as a JSON string"),
+ ),
+)
+
+
+@dataclass(slots=True)
+class FallbackProbe:
+ calls: int = 0
+
+
+def native_responses() -> Iterator[FallbackProbe]:
+ probe: Final = FallbackProbe()
+ python_aresponses: Final = dispatch._PYTHON_ARESPONSES # pyright: ignore[reportPrivateUsage] # the fallback under observation
+
+ async def python_fallback(*args: object, **kwargs: object) -> object:
+ probe.calls += 1
+ return await python_aresponses(*args, **kwargs)
+
+ with (
+ rebound(catalog, "RULES", (RouteRule(Route.RESPONSES, Rollout.RUST_OPT_OUT), *catalog.RULES)),
+ rebound(dispatch, "_PYTHON_ARESPONSES", python_fallback),
+ ):
+ yield probe
+
+
+def _kwargs(case: Case, server: RecordingServer, recorder: RecordingLogger) -> dict[str, object]:
+ return {"model": MODEL, "api_key": "sk-test", "api_base": server.base_url, "callbacks": [recorder], **case.kwargs}
+
+
+def _response(result: object) -> Mapping[str, JsonValue]:
+ assert isinstance(result, ResponsesAPIResponse)
+ return _OBJECT.validate_python(result.model_dump(mode="json", exclude_none=True))
+
+
+async def _leg(case: Case, server: RecordingServer, probe: FallbackProbe | None) -> Leg:
+ with isolated_callback_registries():
+ return await _isolated_leg(case, server, probe)
+
+
+async def _isolated_leg(case: Case, server: RecordingServer, probe: FallbackProbe | None) -> Leg:
+ recorder: Final = RecordingLogger()
+ server.enqueue(ResponseSpec(body=dict(case.upstream)))
+ request_count: Final = len(server.requests)
+ kwargs: Final = _kwargs(case, server, recorder)
+ fallbacks_before: Final = 0 if probe is None else probe.calls
+ if probe is None:
+ result = await _PYTHON_ARESPONSES(**kwargs)
+ else:
+ result = await _PUBLIC_ARESPONSES(**kwargs)
+ await drain_logging()
+ success: Final = await recorder.wait_for_async("async_log_success_event")
+ assert len(server.requests) == request_count + 1
+ sent: Final = server.requests[-1]
+ standard: Final = _LOGGED.validate_python(_LOGGED.validate_python(success[0].kwargs)["standard_logging_object"])
+ return Leg(
+ request_body=_OBJECT.validate_python(sent.body),
+ request_headers=MappingProxyType(
+ {name: value for name, value in sent.headers.items() if name in _COMPARED_HEADERS}
+ ),
+ response=_response(result),
+ logged=MappingProxyType({name: standard.get(name) for name in _COMPARED_LOG_FIELDS}),
+ fell_back=probe is not None and probe.calls > fallbacks_before,
+ )
+
+
+def parity_server(recording_server: RecordingServer) -> RecordingServer:
+ recording_server.expected_requests = 2
+ return recording_server
+
+
[email protected](("case", "aspect"), tuple(param for case in CASES for param in case.params()))
+async def test_native_responses_match_python(
+ case: Case, aspect: str, parity_server: RecordingServer, native_responses: FallbackProbe
+) -> None:
+ python: Final = await _leg(case, parity_server, probe=None)
+ native: Final = await _leg(case, parity_server, probe=native_responses)
+ match aspect:
+ case "dispatch":
+ assert not native.fell_back
+ case "request":
+ assert (native.request_body, native.request_headers) == (python.request_body, python.request_headers)
+ case "response":
+ assert native.response == python.response
+ case _:
+ assert native.logged == python.logged
diff --git a/tests/test_litellm_rust/conftest.py b/tests/test_litellm_rust/conftest.py
index c1d452e838e..06fbcf58591 100644
--- a/tests/test_litellm_rust/conftest.py
+++ b/tests/test_litellm_rust/conftest.py
@@ -1,7 +1,4 @@
-import asyncio
from collections.abc import AsyncIterator, Generator
-from concurrent.futures import ThreadPoolExecutor
-from contextlib import ExitStack
from importlib import import_module
from typing import Final
@@ -9,34 +6,15 @@ import pytest
import pytest_asyncio
import litellm
-from litellm import utils
-from litellm.litellm_core_utils import litellm_logging, thread_pool_executor
-from litellm.litellm_core_utils.logging_worker import GLOBAL_LOGGING_WORKER
-from litellm.rust_bridge.configuration import _CONFIGURATION
-from tests.test_litellm_rust.support.callback_recorder import drain_logging
from tests.test_litellm_rust.support.clickhouse import clickhouse_url as clickhouse_url
-from tests.test_litellm_rust.support.isolation import isolated_callback_registries, rebound
+from tests.test_litellm_rust.support.isolation import isolated_test_state
from tests.test_litellm_rust.support.recording_server import RecordingServer, recording_service
@pytest_asyncio.fixture(autouse=True, loop_scope="function")
async def isolate_test_state() -> AsyncIterator[None]:
- with ExitStack() as stack:
- stack.enter_context(isolated_callback_registries())
- stack.enter_context(rebound(litellm, "cache", None)) # test-quality-ok: isolate process-global cache
- stack.enter_context(rebound(_CONFIGURATION, "override", None))
- executor: Final = ThreadPoolExecutor(thread_name_prefix="rust-test-logging")
- stack.enter_context(rebound(litellm_logging, "executor", executor))
- stack.enter_context(rebound(utils, "executor", executor))
- stack.enter_context(rebound(thread_pool_executor, "executor", executor))
- try:
- yield
- finally:
- try:
- await drain_logging()
- finally:
- await asyncio.to_thread(executor.shutdown, wait=True)
- await GLOBAL_LOGGING_WORKER.stop()
+ async with isolated_test_state():
+ yield
@pytest.fixture
diff --git a/tests/test_litellm_rust/support/isolation.py b/tests/test_litellm_rust/support/isolation.py
index 4464ee4bb51..58dc0d9016d 100644
--- a/tests/test_litellm_rust/support/isolation.py
+++ b/tests/test_litellm_rust/support/isolation.py
@@ -1,11 +1,16 @@
-from collections.abc import Generator
-from contextlib import ExitStack, contextmanager
+import asyncio
+from collections.abc import AsyncGenerator, Generator
+from concurrent.futures import ThreadPoolExecutor
+from contextlib import ExitStack, asynccontextmanager, contextmanager
from types import ModuleType
from typing import Final, cast
import litellm
from litellm import utils
-from litellm.litellm_core_utils import litellm_logging
+from litellm.litellm_core_utils import litellm_logging, thread_pool_executor
+from litellm.litellm_core_utils.logging_worker import GLOBAL_LOGGING_WORKER
+from litellm.rust_bridge.configuration import _CONFIGURATION
+from tests.test_litellm_rust.support.callback_recorder import drain_logging
CALLBACK_ATTRIBUTES: Final = (
"callbacks",
@@ -56,3 +61,23 @@ def isolated_callback_registries() -> Generator[None]:
stack.enter_context(_isolated_list(litellm_logging, "_in_memory_loggers"))
stack.enter_context(rebound(utils, "callback_list", []))
yield
+
+
+@asynccontextmanager
+async def isolated_test_state() -> AsyncGenerator[None]:
+ with ExitStack() as stack:
+ stack.enter_context(isolated_callback_registries())
+ stack.enter_context(rebound(litellm, "cache", None)) # test-quality-ok: isolate process-global cache
+ stack.enter_context(rebound(_CONFIGURATION, "override", None))
+ executor: Final = ThreadPoolExecutor(thread_name_prefix="rust-test-logging")
+ stack.enter_context(rebound(litellm_logging, "executor", executor))
+ stack.enter_context(rebound(utils, "executor", executor))
+ stack.enter_context(rebound(thread_pool_executor, "executor", executor))
+ try:
+ yield
+ finally:
+ try:
+ await drain_logging()
+ finally:
+ await asyncio.to_thread(executor.shutdown, wait=True)
+ await GLOBAL_LOGGING_WORKER.stop()