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: add async call to avoid server pauses

Harshit Jain1792b3c8e5b6
diff --git a/litellm/proxy/management_endpoints/ui_sso.py b/litellm/proxy/management_endpoints/ui_sso.py
index a2d7510c344..2760197781b 100644
--- a/litellm/proxy/management_endpoints/ui_sso.py
+++ b/litellm/proxy/management_endpoints/ui_sso.py
@@ -695,7 +695,7 @@ async def get_generic_sso_response(
try:
result = await generic_sso.verify_and_process(
request,
- params=SSOAuthenticationHandler.prepare_token_exchange_parameters(
+ params=await SSOAuthenticationHandler.prepare_token_exchange_parameters(
request=request,
generic_include_client_id=generic_include_client_id,
),
@@ -1721,13 +1721,13 @@ class SSOAuthenticationHandler:
# so callbacks landing on another pod can retrieve it (multi-pod SSO).
cache_key = f"pkce_verifier:{redirect_params['state']}"
if redis_usage_cache is not None:
- redis_usage_cache.set_cache(
+ await redis_usage_cache.async_set_cache(
key=cache_key,
value=json.dumps(code_verifier),
ttl=600,
)
else:
- user_api_key_cache.set_cache(
+ await user_api_key_cache.async_set_cache(
key=cache_key,
value=code_verifier,
ttl=600,
@@ -2328,7 +2328,7 @@ class SSOAuthenticationHandler:
return redirect_response
@staticmethod
- def prepare_token_exchange_parameters(
+ async def prepare_token_exchange_parameters(
request: Request,
generic_include_client_id: bool,
) -> dict:
@@ -2354,9 +2354,9 @@ class SSOAuthenticationHandler:
cache_key = f"pkce_verifier:{state}"
if redis_usage_cache is not None:
- code_verifier = redis_usage_cache.get_cache(key=cache_key)
+ code_verifier = await redis_usage_cache.async_get_cache(key=cache_key)
else:
- code_verifier = user_api_key_cache.get_cache(key=cache_key)
+ code_verifier = await user_api_key_cache.async_get_cache(key=cache_key)
if code_verifier:
# Add code_verifier to token exchange parameters (Redis returns decoded string)
@@ -2371,9 +2371,9 @@ class SSOAuthenticationHandler:
# Clean up the cache entry (single-use verifier)
if redis_usage_cache is not None:
- redis_usage_cache.delete_cache(key=cache_key)
+ await redis_usage_cache.async_delete_cache(key=cache_key)
else:
- user_api_key_cache.delete_cache(key=cache_key)
+ await user_api_key_cache.async_delete_cache(key=cache_key)
return token_params
@staticmethod
diff --git a/tests/test_litellm/proxy/management_endpoints/test_ui_sso.py b/tests/test_litellm/proxy/management_endpoints/test_ui_sso.py
index 3a5505cdbc5..1a67f7f6b6f 100644
--- a/tests/test_litellm/proxy/management_endpoints/test_ui_sso.py
+++ b/tests/test_litellm/proxy/management_endpoints/test_ui_sso.py
@@ -3073,14 +3073,15 @@ class TestPKCEFunctionality:
test_state = "test_oauth_state_123"
mock_request.query_params = {"state": test_state}
- # Mock cache
+ # Mock cache with async methods
mock_cache = MagicMock()
test_code_verifier = "test_code_verifier_abc123xyz"
- mock_cache.get_cache.return_value = test_code_verifier
+ mock_cache.async_get_cache = AsyncMock(return_value=test_code_verifier)
+ mock_cache.async_delete_cache = AsyncMock()
with patch("litellm.proxy.proxy_server.user_api_key_cache", mock_cache):
# Act
- token_params = SSOAuthenticationHandler.prepare_token_exchange_parameters(
+ token_params = await SSOAuthenticationHandler.prepare_token_exchange_parameters(
request=mock_request, generic_include_client_id=False
)
@@ -3089,10 +3090,10 @@ class TestPKCEFunctionality:
assert token_params["code_verifier"] == test_code_verifier
# Verify cache was accessed and deleted
- mock_cache.get_cache.assert_called_once_with(
+ mock_cache.async_get_cache.assert_called_once_with(
key=f"pkce_verifier:{test_state}"
)
- mock_cache.delete_cache.assert_called_once_with(
+ mock_cache.async_delete_cache.assert_called_once_with(
key=f"pkce_verifier:{test_state}"
)
@@ -3117,6 +3118,8 @@ class TestPKCEFunctionality:
test_state = "test456"
mock_cache = MagicMock()
+ mock_cache.async_set_cache = AsyncMock()
+
with patch.dict(os.environ, {"GENERIC_CLIENT_USE_PKCE": "true"}):
with patch("litellm.proxy.proxy_server.user_api_key_cache", mock_cache):
# Act
@@ -3127,9 +3130,9 @@ class TestPKCEFunctionality:
)
# Assert
- # Verify cache was called to store code_verifier
- mock_cache.set_cache.assert_called_once()
- cache_call = mock_cache.set_cache.call_args
+ # Verify async cache was called to store code_verifier
+ mock_cache.async_set_cache.assert_called_once()
+ cache_call = mock_cache.async_set_cache.call_args
assert cache_call.kwargs["key"] == f"pkce_verifier:{test_state}"
assert cache_call.kwargs["ttl"] == 600
assert len(cache_call.kwargs["value"]) == 43