-
-
Notifications
You must be signed in to change notification settings - Fork 3.4k
Fix: Responses API now respects global request_timeout #12211
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
colesmcintosh
wants to merge
1
commit into
BerriAI:main
Choose a base branch
from
colesmcintosh:lit-272-responses-api-not-respecting-global-request-timeout
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+212
−3
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,205 @@ | ||
""" | ||
Test to verify that request_timeout from litellm_settings is properly used in responses API | ||
""" | ||
import os | ||
import tempfile | ||
import pytest | ||
from unittest.mock import Mock, patch, AsyncMock | ||
|
||
|
||
@pytest.mark.asyncio | ||
async def test_responses_api_timeout_from_config(): | ||
""" | ||
Test that request_timeout set in litellm_settings is properly propagated to user_request_timeout | ||
""" | ||
# Create a temporary config file | ||
config_content = """ | ||
model_list: | ||
- model_name: "*" | ||
litellm_params: | ||
model: openai/* | ||
api_key: test-key | ||
|
||
litellm_settings: | ||
request_timeout: 300 # 5 minutes | ||
drop_params: True | ||
""" | ||
|
||
with tempfile.NamedTemporaryFile(mode="w", suffix=".yaml", delete=False) as f: | ||
f.write(config_content) | ||
config_file = f.name | ||
|
||
try: | ||
# Import after tempfile creation | ||
import litellm | ||
import litellm.proxy.proxy_server | ||
from litellm.proxy.proxy_server import ProxyConfig | ||
|
||
# Save original values | ||
original_request_timeout = getattr(litellm, "request_timeout", None) | ||
original_user_request_timeout = litellm.proxy.proxy_server.user_request_timeout | ||
|
||
# Reset user_request_timeout to None to simulate fresh start | ||
litellm.proxy.proxy_server.user_request_timeout = None | ||
|
||
# Create a new ProxyConfig instance and load config (simulating proxy startup) | ||
config_loader = ProxyConfig() | ||
_, _, _ = await config_loader.load_config( | ||
router=None, config_file_path=config_file | ||
) | ||
|
||
# Check that litellm.request_timeout was set from config | ||
assert ( | ||
litellm.request_timeout == 300 | ||
), f"Expected litellm.request_timeout to be 300, got {litellm.request_timeout}" | ||
|
||
# Check that user_request_timeout was also set | ||
assert ( | ||
litellm.proxy.proxy_server.user_request_timeout == 300 | ||
), f"Expected user_request_timeout to be 300, got {litellm.proxy.proxy_server.user_request_timeout}" | ||
|
||
finally: | ||
# Cleanup | ||
os.unlink(config_file) | ||
if original_request_timeout is not None: | ||
litellm.request_timeout = original_request_timeout | ||
litellm.proxy.proxy_server.user_request_timeout = original_user_request_timeout | ||
|
||
|
||
@pytest.mark.asyncio | ||
async def test_responses_api_timeout_not_overwritten(): | ||
""" | ||
Test that user_request_timeout is not overwritten if already set | ||
""" | ||
# Create a temporary config file | ||
config_content = """ | ||
model_list: | ||
- model_name: "*" | ||
litellm_params: | ||
model: openai/* | ||
api_key: test-key | ||
|
||
litellm_settings: | ||
request_timeout: 400 | ||
""" | ||
|
||
with tempfile.NamedTemporaryFile(mode="w", suffix=".yaml", delete=False) as f: | ||
f.write(config_content) | ||
config_file = f.name | ||
|
||
try: | ||
# Import after tempfile creation | ||
import litellm | ||
import litellm.proxy.proxy_server | ||
from litellm.proxy.proxy_server import ProxyConfig | ||
|
||
# Save original values | ||
original_request_timeout = getattr(litellm, "request_timeout", None) | ||
original_user_request_timeout = litellm.proxy.proxy_server.user_request_timeout | ||
|
||
# Set user_request_timeout to a specific value | ||
litellm.proxy.proxy_server.user_request_timeout = 500 | ||
|
||
# Create a new ProxyConfig instance and load config | ||
config_loader = ProxyConfig() | ||
_, _, _ = await config_loader.load_config( | ||
router=None, config_file_path=config_file | ||
) | ||
|
||
# user_request_timeout should NOT be updated since it's already set | ||
assert ( | ||
litellm.proxy.proxy_server.user_request_timeout == 500 | ||
), f"Expected user_request_timeout to remain 500, got {litellm.proxy.proxy_server.user_request_timeout}" | ||
|
||
finally: | ||
# Cleanup | ||
os.unlink(config_file) | ||
if original_request_timeout is not None: | ||
litellm.request_timeout = original_request_timeout | ||
litellm.proxy.proxy_server.user_request_timeout = original_user_request_timeout | ||
|
||
|
||
@pytest.mark.asyncio | ||
async def test_common_request_processing_uses_litellm_timeout(): | ||
""" | ||
Test that ProxyBaseLLMRequestProcessing uses litellm.request_timeout when user_request_timeout is None | ||
""" | ||
import litellm | ||
from litellm.proxy.common_request_processing import ProxyBaseLLMRequestProcessing | ||
|
||
# Save original values | ||
original_request_timeout = getattr(litellm, "request_timeout", None) | ||
|
||
try: | ||
# Set litellm.request_timeout to a specific value | ||
litellm.request_timeout = 600 # 10 minutes | ||
|
||
# Create processor with sample data | ||
processor = ProxyBaseLLMRequestProcessing(data={"model": "gpt-4"}) | ||
|
||
# Mock the base_process_llm_request method to test internal logic | ||
# The actual logic happens inside base_process_llm_request | ||
# Simulate the condition where user_request_timeout is None | ||
user_request_timeout = None | ||
|
||
# This simulates the logic in base_process_llm_request | ||
if user_request_timeout: | ||
processor.data["request_timeout"] = user_request_timeout | ||
elif ( | ||
user_request_timeout is None | ||
and hasattr(litellm, "request_timeout") | ||
and litellm.request_timeout is not None | ||
): | ||
processor.data["request_timeout"] = litellm.request_timeout | ||
|
||
# Verify the timeout was set correctly | ||
assert ( | ||
processor.data.get("request_timeout") == 600 | ||
), f"Expected request_timeout to be 600, got {processor.data.get('request_timeout')}" | ||
|
||
finally: | ||
# Restore original value | ||
if original_request_timeout is not None: | ||
litellm.request_timeout = original_request_timeout | ||
|
||
|
||
@pytest.mark.asyncio | ||
async def test_common_request_processing_explicit_timeout(): | ||
""" | ||
Test that explicit user_request_timeout takes precedence | ||
""" | ||
import litellm | ||
from litellm.proxy.common_request_processing import ProxyBaseLLMRequestProcessing | ||
|
||
# Save original values | ||
original_request_timeout = getattr(litellm, "request_timeout", None) | ||
|
||
try: | ||
# Set litellm.request_timeout to a specific value | ||
litellm.request_timeout = 600 # 10 minutes | ||
|
||
# Create processor with sample data | ||
processor = ProxyBaseLLMRequestProcessing(data={"model": "gpt-4"}) | ||
|
||
# Simulate explicit user_request_timeout | ||
user_request_timeout = 300 | ||
|
||
# This simulates the logic in base_process_llm_request | ||
if user_request_timeout: | ||
processor.data["request_timeout"] = user_request_timeout | ||
elif ( | ||
user_request_timeout is None | ||
and hasattr(litellm, "request_timeout") | ||
and litellm.request_timeout is not None | ||
): | ||
processor.data["request_timeout"] = litellm.request_timeout | ||
|
||
# Verify that explicit timeout takes precedence | ||
assert ( | ||
processor.data.get("request_timeout") == 300 | ||
), f"Expected request_timeout to be 300, got {processor.data.get('request_timeout')}" | ||
|
||
finally: | ||
# Restore original value | ||
if original_request_timeout is not None: | ||
litellm.request_timeout = original_request_timeout |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
this is the wrong place. It should be on responses/main.py