From 1a3a9aa34cf70eb0d30924c8ca2e64d065d00ce5 Mon Sep 17 00:00:00 2001 From: michelligabriele Date: Fri, 6 Mar 2026 18:36:56 +0100 Subject: [PATCH] =?UTF-8?q?fix(proxy):=20address=20review=20feedback=20?= =?UTF-8?q?=E2=80=94=20safer=20backwards=20compat=20and=20None=20guards?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - Replace try/except TypeError with inspect.signature() check for litellm_call_info backwards compatibility. This avoids masking real TypeErrors inside callback implementations and prevents double invocation with inconsistent parameters. - Use (data.get("key") or {}) instead of data.get("key", {}) to guard against keys that exist with an explicit None value, which would cause AttributeError on the subsequent .get() call. --- litellm/proxy/common_request_processing.py | 2 +- litellm/proxy/utils.py | 10 ++++++---- 2 files changed, 7 insertions(+), 5 deletions(-) diff --git a/litellm/proxy/common_request_processing.py b/litellm/proxy/common_request_processing.py index 745ba52cebb..ffc05d00c58 100644 --- a/litellm/proxy/common_request_processing.py +++ b/litellm/proxy/common_request_processing.py @@ -1212,7 +1212,7 @@ class ProxyBaseLLMRequestProcessing: data=self.data, user_api_key_dict=user_api_key_dict, response=None, - request_headers=self.data.get("proxy_server_request", {}).get("headers", {}), + request_headers=(self.data.get("proxy_server_request") or {}).get("headers", {}), ) if callback_headers: headers.update(callback_headers) diff --git a/litellm/proxy/utils.py b/litellm/proxy/utils.py index 51941f6f9db..b6066ccf984 100644 --- a/litellm/proxy/utils.py +++ b/litellm/proxy/utils.py @@ -1,6 +1,7 @@ import asyncio import copy import hashlib +import inspect import json import os import smtplib @@ -1988,7 +1989,8 @@ class ProxyLogging: _callback = callback # type: ignore if _callback is not None and isinstance(_callback, CustomLogger): - try: + sig = inspect.signature(_callback.async_post_call_response_headers_hook) + if "litellm_call_info" in sig.parameters: result = await _callback.async_post_call_response_headers_hook( data=data, user_api_key_dict=user_api_key_dict, @@ -1996,7 +1998,7 @@ class ProxyLogging: request_headers=request_headers, litellm_call_info=litellm_call_info, ) - except TypeError: + else: # Backwards compat: callback doesn't accept litellm_call_info result = await _callback.async_post_call_response_headers_hook( data=data, @@ -2024,8 +2026,8 @@ class ProxyLogging: # model_info: check both metadata keys (chat uses "metadata", responses uses "litellm_metadata") model_info = ( - data.get("metadata", {}).get("model_info") - or data.get("litellm_metadata", {}).get("model_info") + (data.get("metadata") or {}).get("model_info") + or (data.get("litellm_metadata") or {}).get("model_info") or {} )