From 36d46e0ecfda3fa9002e83b956cfe01407d72bd0 Mon Sep 17 00:00:00 2001 From: Ishaan Jaff Date: Tue, 29 Jul 2025 18:19:38 -0700 Subject: [PATCH] use _safe_get_request_query_params --- litellm/proxy/auth/user_api_key_auth.py | 7 +++++-- litellm/proxy/common_utils/http_parsing_utils.py | 12 ++++++++++++ 2 files changed, 17 insertions(+), 2 deletions(-) diff --git a/litellm/proxy/auth/user_api_key_auth.py b/litellm/proxy/auth/user_api_key_auth.py index 70f9636adf..fb70426bd5 100644 --- a/litellm/proxy/auth/user_api_key_auth.py +++ b/litellm/proxy/auth/user_api_key_auth.py @@ -256,6 +256,9 @@ def get_api_key( Tuple[Optional[str], Optional[str]]: Tuple of the api_key and the passed_in_key """ from litellm.proxy.auth.route_checks import RouteChecks + from litellm.proxy.common_utils.http_parsing_utils import ( + _safe_get_request_query_params, + ) api_key = api_key passed_in_key: Optional[str] = None if isinstance(custom_litellm_key_header, str): @@ -276,8 +279,8 @@ def get_api_key( elif isinstance(azure_apim_header, str): passed_in_key = azure_apim_header api_key = azure_apim_header - elif RouteChecks.is_generate_content_route(route=route) and request is not None and request.query_params.get("key"): - google_auth_key: str = request.query_params.get("key") or "" + elif RouteChecks.is_generate_content_route(route=route) and request is not None and _safe_get_request_query_params(request).get("key"): + google_auth_key: str = _safe_get_request_query_params(request).get("key") or "" passed_in_key = google_auth_key api_key = google_auth_key elif pass_through_endpoints is not None: diff --git a/litellm/proxy/common_utils/http_parsing_utils.py b/litellm/proxy/common_utils/http_parsing_utils.py index 875d69437c..ee12f8814e 100644 --- a/litellm/proxy/common_utils/http_parsing_utils.py +++ b/litellm/proxy/common_utils/http_parsing_utils.py @@ -102,6 +102,18 @@ def _safe_get_request_parsed_body(request: Optional[Request]) -> Optional[dict]: return {key: parsed_body[key] for key in accepted_keys} return None +def _safe_get_request_query_params(request: Optional[Request]) -> Dict: + if request is None: + return {} + try: + if hasattr(request, "query_params"): + return dict(request.query_params) + return {} + except Exception as e: + verbose_proxy_logger.debug( + "Unexpected error reading request query params - {}".format(e) + ) + return {} def _safe_set_request_parsed_body( request: Optional[Request],