From a88e23eb5d14abd29f1466dbd57aae2932a47456 Mon Sep 17 00:00:00 2001 From: SIDDARTHA REDDY <75976672+SIDDARTHAREDDY8@users.noreply.github.com> Date: Sat, 17 Oct 2026 14:04:30 -0400 Subject: [PATCH] fix(proxy): validate general_settings into a typed dict for the basedpyright gate Signed-off-by: SIDDARTHA REDDY <75976672+SIDDARTHAREDDY8@users.noreply.github.com> --- litellm/proxy/db/exception_handler.py | 16 +++++++++++++--- 1 file changed, 13 insertions(+), 3 deletions(-) diff --git a/litellm/proxy/db/exception_handler.py b/litellm/proxy/db/exception_handler.py index 0a2bf2aa279..378afea0d30 100644 --- a/litellm/proxy/db/exception_handler.py +++ b/litellm/proxy/db/exception_handler.py @@ -25,6 +25,7 @@ _TRANSIENT_DB_UNAVAILABLE_MESSAGE: Final = ( ) _DATABASE_ERROR_META: Final = TypeAdapter(dict[str, object]) +_GENERAL_SETTINGS_ADAPTER: Final = TypeAdapter(dict[str, object]) _BATCH_POSTGRES_ERROR_CODE: Final = re.compile(r'PostgresError \{ code: "([0-9A-Z]{5})"') _CONNECTION_CAPACITY_PHRASES: Final = ( "too many clients already", @@ -111,10 +112,19 @@ class PrismaDBExceptionHandler: # Read ``general_settings`` through ``sys.modules``: importing # ``proxy_server`` here could fail or recurse while it is still # being imported, and a proxy server that has not been imported - # yet has not configured a database through it either. + # yet has not configured a database through it either. The raw + # value is ``object`` until validated into a typed dict, because + # ``general_settings`` is declared as a bare ``dict`` and reading + # it directly surfaces as partially unknown to basedpyright. proxy_server_module: Final = sys.modules.get("litellm.proxy.proxy_server") - general_settings: Final = getattr(proxy_server_module, "general_settings", None) - return isinstance(general_settings, dict) and bool(general_settings.get("database_url")) + raw_settings: Final[object] = getattr(proxy_server_module, "general_settings", None) + if not isinstance(raw_settings, dict): + return False + try: + settings: Final = _GENERAL_SETTINGS_ADAPTER.validate_python(raw_settings) + except ValidationError: + return False + return bool(settings.get("database_url")) @staticmethod def is_database_connection_error(e: Exception) -> bool: