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]
Python ai-gatewayanthropicazure-openaibedrockgatewaylangchainlitellmllmllm-gatewayllmopsmcp-gatewayopenaiopenai-proxyrustrust-aivertex-ai
fix(litellm_logging.py): always log the api base
Krrish Dholakia6a0cf3db5062
diff --git a/litellm/litellm_core_utils/litellm_logging.py b/litellm/litellm_core_utils/litellm_logging.pyindex 67511968e21..3565c4468c5 100644--- a/litellm/litellm_core_utils/litellm_logging.py+++ b/litellm/litellm_core_utils/litellm_logging.py@@ -518,6 +518,16 @@ class Logging(LiteLLMLoggingBaseClass):}return data+ def _get_masked_api_base(self, api_base: str) -> str:+ if "key=" in api_base:+ # Find the position of "key=" in the string+ key_index = api_base.find("key=") + 4+ # Mask the last 5 characters after "key="+ masked_api_base = api_base[:key_index] + "*" * 5 + api_base[-4:]+ else:+ masked_api_base = api_base+ return str(masked_api_base)+def _pre_call(self, input, api_key, model=None, additional_args={}):"""Common helper function across the sync + async pre-call function@@ -531,6 +541,9 @@ class Logging(LiteLLMLoggingBaseClass):model): # if model name was changes pre-call, overwrite the initial model call name with the new oneself.model_call_details["model"] = model+ self.model_call_details["litellm_params"]["api_base"] = (+ self._get_masked_api_base(additional_args.get("api_base", ""))+ )def pre_call(self, input, api_key, model=None, additional_args={}): # noqa: PLR0915@@ -714,15 +727,6 @@ class Logging(LiteLLMLoggingBaseClass):headers = {}data = additional_args.get("complete_input_dict", {})api_base = str(additional_args.get("api_base", ""))- if "key=" in api_base:- # Find the position of "key=" in the string- key_index = api_base.find("key=") + 4- # Mask the last 5 characters after "key="- masked_api_base = api_base[:key_index] + "*" * 5 + api_base[-4:]- else:- masked_api_base = api_base- self.model_call_details["litellm_params"]["api_base"] = masked_api_base-curl_command = self._get_request_curl_command(api_base=api_base,headers=headers,@@ -737,11 +741,12 @@ class Logging(LiteLLMLoggingBaseClass):def _get_request_curl_command(self, api_base: str, headers: Optional[dict], additional_args: dict, data: dict) -> str:+ masked_api_base = self._get_masked_api_base(api_base)if headers is None:headers = {}curl_command = "\n\nPOST Request Sent from LiteLLM:\n"curl_command += "curl -X POST \\\n"- curl_command += f"{api_base} \\\n"+ curl_command += f"{masked_api_base} \\\n"masked_headers = self._get_masked_headers(headers)formatted_headers = " ".join([f"-H '{k}: {v}'" for k, v in masked_headers.items()]diff --git a/litellm/litellm_core_utils/model_param_helper.py b/litellm/litellm_core_utils/model_param_helper.pyindex d792ede2822..b7d8fc19d14 100644--- a/litellm/litellm_core_utils/model_param_helper.py+++ b/litellm/litellm_core_utils/model_param_helper.py@@ -138,13 +138,22 @@ class ModelParamHelper:TranscriptionCreateParamsNonStreaming,TranscriptionCreateParamsStreaming,)- non_streaming_kwargs = set(getattr(TranscriptionCreateParamsNonStreaming, "__annotations__", {}).keys())- streaming_kwargs = set(getattr(TranscriptionCreateParamsStreaming, "__annotations__", {}).keys())++ non_streaming_kwargs = set(+ getattr(+ TranscriptionCreateParamsNonStreaming, "__annotations__", {}+ ).keys()+ )+ streaming_kwargs = set(+ getattr(+ TranscriptionCreateParamsStreaming, "__annotations__", {}+ ).keys()+ )all_transcription_kwargs = non_streaming_kwargs.union(streaming_kwargs)return all_transcription_kwargsexcept Exception as e:- verbose_logger.warning("Error getting transcription kwargs %s", str(e))+ verbose_logger.debug("Error getting transcription kwargs %s", str(e))return set()@staticmethoddiff --git a/litellm/proxy/_new_secret_config.yaml b/litellm/proxy/_new_secret_config.yamlindex cd49647464b..fbe3471663c 100644--- a/litellm/proxy/_new_secret_config.yaml+++ b/litellm/proxy/_new_secret_config.yaml@@ -5,7 +5,10 @@ model_list:api_key: os.environ/AZURE_API_KEYapi_base: http://0.0.0.0:8090rpm: 3-+ - model_name: "gpt-4o-mini-openai"+ litellm_params:+ model: gpt-4o-mini+ api_key: os.environ/OPENAI_API_KEYlitellm_settings:num_retries: 0diff --git a/tests/litellm/proxy/spend_tracking/test_spend_management_endpoints.py b/tests/litellm/proxy/spend_tracking/test_spend_management_endpoints.pyindex 415acdecee7..0a55991215a 100644--- a/tests/litellm/proxy/spend_tracking/test_spend_management_endpoints.py+++ b/tests/litellm/proxy/spend_tracking/test_spend_management_endpoints.py@@ -1,3 +1,4 @@+import asyncioimport datetimeimport jsonimport os@@ -11,6 +12,10 @@ sys.path.insert(0, os.path.abspath("../../../..")) # Adds the parent directory to the system path+from unittest.mock import patch++import litellm+from litellm.proxy.hooks.proxy_track_cost_callback import _ProxyDBLoggerfrom litellm.proxy.proxy_server import app, prisma_client@@ -400,3 +405,28 @@ async def test_ui_view_spend_logs_unauthorized(client):headers={"Authorization": "Bearer invalid-token"},)assert response.status_code == 401 or response.status_code == 403+++class TestSpendLogsPayload:+ @pytest.mark.asyncio+ async def test_spend_logs_payload_api_base_e2e(self):+ litellm.callbacks = [_ProxyDBLogger(message_logging=False)]+ # litellm._turn_on_debug()++ with patch.object(+ litellm.proxy.proxy_server, "_set_spend_logs_payload"+ ) as mock_client:+ response = await litellm.acompletion(+ model="gpt-4o",+ messages=[{"role": "user", "content": "Hello, world!"}],+ mock_response="Hello, world!",+ )++ assert response.choices[0].message.content == "Hello, world!"++ await asyncio.sleep(1)++ mock_client.assert_called_once()++ def test_spend_logs_payload_with_prompts_enabled(self):+ pass