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 errors when callbacks are invoked for file delete operations:
Ephrim Stanleybac6d1127c15
diff --git a/litellm/integrations/s3_v2.py b/litellm/integrations/s3_v2.pyindex 534b85e4752..e847ce27476 100644--- a/litellm/integrations/s3_v2.py+++ b/litellm/integrations/s3_v2.py@@ -248,7 +248,11 @@ class S3Logger(CustomBatchLogger, BaseAWSLLM):)if s3_batch_logging_element is None:- raise ValueError("s3_batch_logging_element is None")+ verbose_logger.debug(+ "s3 Logging - skipping event, no standard_logging_object for call_type=%s",+ kwargs.get("call_type", "unknown"),+ )+ returnverbose_logger.debug("\ns3 Logger - Logging payload = %s", s3_batch_logging_elementdiff --git a/litellm/proxy/hooks/proxy_track_cost_callback.py b/litellm/proxy/hooks/proxy_track_cost_callback.pyindex 37b79e6d065..ffc9dbd2372 100644--- a/litellm/proxy/hooks/proxy_track_cost_callback.py+++ b/litellm/proxy/hooks/proxy_track_cost_callback.py@@ -202,6 +202,12 @@ class _ProxyDBLogger(CustomLogger):max_budget=end_user_max_budget,)else:+ if sl_object is None and kwargs.get("model") is None:+ verbose_proxy_logger.warning(+ "Cost tracking - skipping, no standard_logging_object and no model for call_type=%s",+ kwargs.get("call_type", "unknown"),+ )+ returnif kwargs.get("stream") is not True or (kwargs.get("stream") is True and "complete_streaming_response" in kwargs):diff --git a/tests/test_litellm/integrations/test_s3_v2.py b/tests/test_litellm/integrations/test_s3_v2.pyindex 0a3523699a9..51fec288f6e 100644--- a/tests/test_litellm/integrations/test_s3_v2.py+++ b/tests/test_litellm/integrations/test_s3_v2.py@@ -157,6 +157,51 @@ class TestS3V2UnitTests:assert result == {"downloaded": "data"}+async def test_async_log_event_skips_when_standard_logging_object_missing():+ """+ Reproduces the bug where _async_log_event_base raises ValueError when+ kwargs has no standard_logging_object (e.g. call_type=afile_delete).++ The S3 logger should skip gracefully, not raise.+ """+ logger = S3Logger(+ s3_bucket_name="test-bucket",+ s3_region_name="us-east-1",+ s3_aws_access_key_id="fake",+ s3_aws_secret_access_key="fake",+ )++ kwargs_without_slo = {+ "call_type": "afile_delete",+ "model": None,+ "litellm_call_id": "test-call-id",+ }++ start_time = datetime.utcnow()+ end_time = datetime.utcnow()++ # Spy on handle_callback_failure — should NOT be called if we skip gracefully.+ # Without the fix, the ValueError is caught by the except block which calls+ # handle_callback_failure. With the fix, we return early and never hit except.+ with patch.object(logger, "handle_callback_failure") as mock_failure:+ await logger._async_log_event_base(+ kwargs=kwargs_without_slo,+ response_obj=None,+ start_time=start_time,+ end_time=end_time,+ )++ assert not mock_failure.called, (+ "handle_callback_failure should not be called — "+ "missing standard_logging_object should be a graceful skip, not an error"+ )++ # Nothing should have been queued (catches the case where code falls+ # through without returning and appends None to the queue)+ assert len(logger.log_queue) == 0, "log_queue should be empty when standard_logging_object is missing"++@pytest.mark.asyncioasync def test_strip_base64_removes_file_and_nontext_entries():logger = S3Logger(s3_strip_base64_files=True)diff --git a/tests/test_litellm/proxy/hooks/test_proxy_track_cost_callback.py b/tests/test_litellm/proxy/hooks/test_proxy_track_cost_callback.pyindex cb6d90103f7..46da6a89cb2 100644--- a/tests/test_litellm/proxy/hooks/test_proxy_track_cost_callback.py+++ b/tests/test_litellm/proxy/hooks/test_proxy_track_cost_callback.py@@ -126,3 +126,44 @@ async def test_async_post_call_failure_hook_non_llm_route():# Assert that update_database was NOT called for non-LLM routesmock_update_database.assert_not_called()+++async def test_track_cost_callback_skips_when_no_standard_logging_object():+ """+ Reproduces the bug where _PROXY_track_cost_callback raises+ 'Cost tracking failed for model=None' when kwargs has no+ standard_logging_object (e.g. call_type=afile_delete).++ File operations have no model and no standard_logging_object.+ The callback should skip gracefully instead of raising.+ """+ logger = _ProxyDBLogger()++ kwargs = {+ "call_type": "afile_delete",+ "model": None,+ "litellm_call_id": "test-call-id",+ "litellm_params": {},+ "stream": False,+ }++ with patch(+ "litellm.proxy.proxy_server.proxy_logging_obj",+ ) as mock_proxy_logging:+ mock_proxy_logging.failed_tracking_alert = AsyncMock()+ mock_proxy_logging.db_spend_update_writer = MagicMock()+ mock_proxy_logging.db_spend_update_writer.update_database = AsyncMock()++ await logger._PROXY_track_cost_callback(+ kwargs=kwargs,+ completion_response=None,+ start_time=datetime.now(),+ end_time=datetime.now(),+ )++ # update_database should NOT be called — nothing to track+ mock_proxy_logging.db_spend_update_writer.update_database.assert_not_called()++ # failed_tracking_alert should NOT be called — this is not an error+ mock_proxy_logging.failed_tracking_alert.assert_not_called()