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]

tests(vcr): patch vcrpy aiohttp record path instead of forcing httpx transport

mateo-berri687ff32616d5
diff --git a/tests/_vcr_redis_persister.py b/tests/_vcr_redis_persister.py
index 6cbaff84413..d8cda930983 100644
--- a/tests/_vcr_redis_persister.py
+++ b/tests/_vcr_redis_persister.py
@@ -85,3 +85,46 @@ def filter_non_2xx_response(response):
if not isinstance(code, int):
return response
return response if 200 <= code < 300 else None
+
+
+_PATCHED_AIOHTTP_RECORD = False
+
+
+def patch_vcrpy_aiohttp_record_path() -> None:
+ """Make vcrpy's aiohttp record path leave the response body re-readable.
+
+ vcrpy.stubs.aiohttp_stubs.record_response calls ``await response.read()``
+ to capture the body for the cassette, which drains aiohttp's StreamReader.
+ Downstream consumers of the same ClientResponse (e.g.
+ ``litellm.llms.custom_httpx.aiohttp_transport.AiohttpResponseStream``,
+ which iterates ``response.content.iter_chunked``) then see an empty body
+ and surface as ``Expecting value: line 1 column 1 (char 0)`` JSON errors.
+
+ Re-feed the captured bytes back into the StreamReader via ``unread_data``
+ so the body remains available to whoever holds the ClientResponse next.
+ Idempotent; safe to call from multiple conftests.
+ """
+ global _PATCHED_AIOHTTP_RECORD
+ if _PATCHED_AIOHTTP_RECORD:
+ return
+ try:
+ import vcr.stubs.aiohttp_stubs as _aiohttp_stubs
+ except ImportError: # pragma: no cover - aiohttp not installed in env
+ return
+
+ _orig_record_response = _aiohttp_stubs.record_response
+
+ async def _record_response_preserving_body(cassette, vcr_request, response):
+ await _orig_record_response(cassette, vcr_request, response)
+ body = getattr(response, "_body", None) or b""
+ if body:
+ try:
+ response.content.unread_data(body)
+ except Exception:
+ # If aiohttp removes unread_data in a future release we want
+ # the test to fail loudly via the original empty-body
+ # symptom rather than mask the regression here.
+ pass
+
+ _aiohttp_stubs.record_response = _record_response_preserving_body
+ _PATCHED_AIOHTTP_RECORD = True
diff --git a/tests/llm_responses_api_testing/conftest.py b/tests/llm_responses_api_testing/conftest.py
index 948debbf95c..02cc88ee0d9 100644
--- a/tests/llm_responses_api_testing/conftest.py
+++ b/tests/llm_responses_api_testing/conftest.py
@@ -16,6 +16,7 @@ import litellm # noqa: E402
from tests._vcr_redis_persister import ( # noqa: E402
filter_non_2xx_response,
make_redis_persister,
+ patch_vcrpy_aiohttp_record_path,
)
@@ -97,11 +98,10 @@ def pytest_recording_configure(config, vcr):
if _vcr_disabled():
return
vcr.register_persister(make_redis_persister())
- # vcrpy patches httpx's transport; litellm's default AiohttpTransport
- # routes around httpx and produces empty responses under the patched
- # transport. Force pure-httpx transport so vcrpy can record/replay.
- litellm.disable_aiohttp_transport = True
- os.environ["DISABLE_AIOHTTP_TRANSPORT"] = "True"
+ # vcrpy's aiohttp record path drains the response stream via
+ # ``await response.read()``; without the patch, downstream consumers
+ # (litellm's AiohttpResponseStream) see an empty body on first record.
+ patch_vcrpy_aiohttp_record_path()
@pytest.fixture(scope="session")
diff --git a/tests/llm_translation/conftest.py b/tests/llm_translation/conftest.py
index 2215b1c24b4..2d8623b8366 100644
--- a/tests/llm_translation/conftest.py
+++ b/tests/llm_translation/conftest.py
@@ -21,6 +21,7 @@ import litellm # noqa: E402
from tests._vcr_redis_persister import ( # noqa: E402
filter_non_2xx_response,
make_redis_persister,
+ patch_vcrpy_aiohttp_record_path,
)
@@ -121,11 +122,10 @@ def pytest_recording_configure(config, vcr):
if _vcr_disabled():
return
vcr.register_persister(make_redis_persister())
- # vcrpy patches httpx's transport; litellm's default AiohttpTransport
- # routes around httpx and produces empty responses under the patched
- # transport. Force pure-httpx transport so vcrpy can record/replay.
- litellm.disable_aiohttp_transport = True
- os.environ["DISABLE_AIOHTTP_TRANSPORT"] = "True"
+ # vcrpy's aiohttp record path drains the response stream via
+ # ``await response.read()``; without the patch, downstream consumers
+ # (litellm's AiohttpResponseStream) see an empty body on first record.
+ patch_vcrpy_aiohttp_record_path()
# ---------------------------------------------------------------------------