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]

fix(test): add cleanup for disable_aiohttp_transport in test_extra_body_with_fallback

Julio Quinteros Proa2a3d144438e
diff --git a/tests/test_litellm/test_main.py b/tests/test_litellm/test_main.py
index 70664827253..9d76f41d59a 100644
--- a/tests/test_litellm/test_main.py
+++ b/tests/test_litellm/test_main.py
@@ -423,64 +423,71 @@ async def test_extra_body_with_fallback(
This was perhaps a wider issue with the acompletion function not passing kwargs such as extra_body correctly when fallbacks are specified.
"""
- # since this uses respx, we need to set use_aiohttp_transport to False
- litellm.disable_aiohttp_transport = True
- # Set up test parameters
- model = "openrouter/deepseek/deepseek-chat"
- messages = [{"role": "user", "content": "Hello, world!"}]
- extra_body = {
- "provider": {
- "order": ["DeepSeek"],
- "allow_fallbacks": False,
- "require_parameters": True,
- }
- }
- fallbacks = [{"model": "openrouter/google/gemini-flash-1.5-8b"}]
+ # Save original state to restore after test
+ original_disable_aiohttp = litellm.disable_aiohttp_transport
- respx_mock.post("https://openrouter.ai/api/v1/chat/completions").respond(
- json={
- "id": "chatcmpl-123",
- "object": "chat.completion",
- "created": 1677652288,
- "model": model,
- "choices": [
- {
- "index": 0,
- "message": {
- "role": "assistant",
- "content": "Hello from mocked response!",
- },
- "finish_reason": "stop",
- }
- ],
- "usage": {"prompt_tokens": 9, "completion_tokens": 12, "total_tokens": 21},
+ try:
+ # since this uses respx, we need to set use_aiohttp_transport to False
+ litellm.disable_aiohttp_transport = True
+ # Set up test parameters
+ model = "openrouter/deepseek/deepseek-chat"
+ messages = [{"role": "user", "content": "Hello, world!"}]
+ extra_body = {
+ "provider": {
+ "order": ["DeepSeek"],
+ "allow_fallbacks": False,
+ "require_parameters": True,
+ }
}
- )
+ fallbacks = [{"model": "openrouter/google/gemini-flash-1.5-8b"}]
+
+ respx_mock.post("https://openrouter.ai/api/v1/chat/completions").respond(
+ json={
+ "id": "chatcmpl-123",
+ "object": "chat.completion",
+ "created": 1677652288,
+ "model": model,
+ "choices": [
+ {
+ "index": 0,
+ "message": {
+ "role": "assistant",
+ "content": "Hello from mocked response!",
+ },
+ "finish_reason": "stop",
+ }
+ ],
+ "usage": {"prompt_tokens": 9, "completion_tokens": 12, "total_tokens": 21},
+ }
+ )
- response = await litellm.acompletion(
- model=model,
- messages=messages,
- extra_body=extra_body,
- fallbacks=fallbacks,
- api_key="fake-openrouter-api-key",
- )
+ response = await litellm.acompletion(
+ model=model,
+ messages=messages,
+ extra_body=extra_body,
+ fallbacks=fallbacks,
+ api_key="fake-openrouter-api-key",
+ )
- # Get the request from the mock
- request: httpx.Request = respx_mock.calls[0].request
- request_body = request.read()
- request_body = json.loads(request_body)
+ # Get the request from the mock
+ request: httpx.Request = respx_mock.calls[0].request
+ request_body = request.read()
+ request_body = json.loads(request_body)
- # Verify basic parameters
- assert request_body["model"] == "deepseek/deepseek-chat"
- assert request_body["messages"] == messages
+ # Verify basic parameters
+ assert request_body["model"] == "deepseek/deepseek-chat"
+ assert request_body["messages"] == messages
- # Verify the extra_body parameters remain under the provider key
- assert request_body["provider"]["order"] == ["DeepSeek"]
- assert request_body["provider"]["allow_fallbacks"] is False
- assert request_body["provider"]["require_parameters"] is True
+ # Verify the extra_body parameters remain under the provider key
+ assert request_body["provider"]["order"] == ["DeepSeek"]
+ assert request_body["provider"]["allow_fallbacks"] is False
+ assert request_body["provider"]["require_parameters"] is True
- # Verify the response
- assert response is not None
+ # Verify the response
+ assert response is not None
+ finally:
+ # Restore original state to prevent test pollution
+ litellm.disable_aiohttp_transport = original_disable_aiohttp
assert response.choices[0].message.content == "Hello from mocked response!"