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(proxy): handle ownership-recording failures after upstream create
user6c37e9d8d571
diff --git a/litellm/proxy/container_endpoints/endpoints.py b/litellm/proxy/container_endpoints/endpoints.pyindex 089f22a23a5..7e097802632 100644--- a/litellm/proxy/container_endpoints/endpoints.py+++ b/litellm/proxy/container_endpoints/endpoints.py@@ -2,9 +2,10 @@from typing import Any, Dict-from fastapi import APIRouter, Depends, Request, Response+from fastapi import APIRouter, Depends, HTTPException, Request, Responsefrom fastapi.responses import ORJSONResponse+from litellm._logging import verbose_proxy_loggerfrom litellm.proxy._types import *from litellm.proxy.auth.user_api_key_auth import UserAPIKeyAuth, user_api_key_authfrom litellm.proxy.common_request_processing import ProxyBaseLLMRequestProcessing@@ -129,11 +130,31 @@ async def create_container(proxy_logging_obj=proxy_logging_obj,version=version,)- return await record_container_owner(- response=response,- user_api_key_dict=user_api_key_dict,- custom_llm_provider=custom_llm_provider,- )++ # Ownership recording sits between the upstream create and the response+ # to the caller. The recorder swallows DB errors internally (falling+ # back to in-memory tracking) and only surfaces HTTPException on auth+ # conflicts; we let those propagate verbatim so the client sees the+ # real status code rather than a generic LLM error wrapper.+ try:+ return await record_container_owner(+ response=response,+ user_api_key_dict=user_api_key_dict,+ custom_llm_provider=custom_llm_provider,+ )+ except HTTPException:+ raise+ except Exception as e:+ # Unexpected (non-HTTPException) failure after upstream create.+ # The container exists upstream but is now untracked — log loudly+ # so an operator can reconcile, and return the response so the+ # caller does not get charged for a resource they cannot use.+ verbose_proxy_logger.exception(+ "Container ownership recording failed after upstream create; "+ "returning response with untracked ownership: %s",+ e,+ )+ return response@router.get(diff --git a/tests/test_litellm/containers/test_container_proxy_ownership.py b/tests/test_litellm/containers/test_container_proxy_ownership.pyindex b696f8fc48f..13216ad7c59 100644--- a/tests/test_litellm/containers/test_container_proxy_ownership.py+++ b/tests/test_litellm/containers/test_container_proxy_ownership.py@@ -933,6 +933,65 @@ async def test_should_not_route_owner_record_errors_through_llm_error_handler(assert exc.value.status_code == 403+async def test_should_return_response_when_owner_recording_raises_unexpected(+ monkeypatch,+):+ """If record_container_owner raises a non-HTTPException after upstream create,+ the upstream container exists but is untracked. The caller still gets the+ response (not a 500) so they aren't billed for an unusable resource — an+ operator reconciles via logs.+ """+ from litellm.proxy.container_endpoints import endpoints++ proxy_server_stub = SimpleNamespace(+ general_settings={},+ llm_router=None,+ proxy_config=None,+ proxy_logging_obj=None,+ select_data_generator=None,+ user_api_base=None,+ user_max_tokens=None,+ user_model=None,+ user_request_timeout=None,+ user_temperature=None,+ version="test",+ )+ monkeypatch.setitem(sys.modules, "litellm.proxy.proxy_server", proxy_server_stub)++ created = _container("cntr_provider")++ class FakeProcessor:+ def __init__(self, data):+ pass++ async def base_process_llm_request(self, **kwargs):+ return created++ async def _handle_llm_api_exception(self, **kwargs):+ raise AssertionError("upstream-create errors only")++ monkeypatch.setattr(endpoints, "ProxyBaseLLMRequestProcessing", FakeProcessor)+ monkeypatch.setattr(+ endpoints,+ "record_container_owner",+ AsyncMock(side_effect=RuntimeError("transient db blip")),+ )++ response = await endpoints.create_container(+ request=SimpleNamespace(+ query_params={},+ headers={},+ json=AsyncMock(return_value={}),+ body=AsyncMock(return_value=b"{}"),+ ),+ fastapi_response=SimpleNamespace(),+ user_api_key_dict=UserAPIKeyAuth(user_id="user-1"),+ )++ assert response is created++@pytest.mark.asyncioasync def test_should_filter_container_list_inside_list_endpoint(monkeypatch):from litellm.proxy.container_endpoints import endpoints