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(passthrough): bill streamed Responses calls that end failed
mateo-berri5e14649c5420
diff --git a/litellm/llms/openai/responses/transformation.py b/litellm/llms/openai/responses/transformation.pyindex 568beed8664..b2a69564908 100644--- a/litellm/llms/openai/responses/transformation.py+++ b/litellm/llms/openai/responses/transformation.py@@ -356,7 +356,7 @@ class OpenAIResponsesAPIConfig(BaseResponsesAPIConfig):@staticmethoddef parse_terminal_response_from_stream_chunks(all_chunks: list[str]) -> ResponsesAPIResponse | None:for chunk_str in reversed(all_chunks):- for event_model in (ResponseCompletedEvent, ResponseIncompleteEvent):+ for event_model in (ResponseCompletedEvent, ResponseIncompleteEvent, ResponseFailedEvent):try:return event_model.model_validate_json(chunk_str.removeprefix("data: ")).responseexcept ValueError:diff --git a/tests/test_litellm/proxy/pass_through_endpoints/llm_provider_handlers/test_openai_passthrough_logging_handler.py b/tests/test_litellm/proxy/pass_through_endpoints/llm_provider_handlers/test_openai_passthrough_logging_handler.pyindex b0576960355..1735308bda0 100644--- a/tests/test_litellm/proxy/pass_through_endpoints/llm_provider_handlers/test_openai_passthrough_logging_handler.py+++ b/tests/test_litellm/proxy/pass_through_endpoints/llm_provider_handlers/test_openai_passthrough_logging_handler.py@@ -664,6 +664,63 @@ class TestOpenAIPassthroughLoggingHandler:call_type="responses",)+ @patch(f"{OpenAIPassthroughLoggingHandler.__module__}.get_standard_logging_object_payload")+ @patch("litellm.completion_cost", return_value=1.4e-06)+ def test_streaming_responses_failed_event_is_billed(self, mock_completion_cost, mock_get_standard_logging):+ response_id = "resp_FAILEDSENTINEL0123456789abcd"+ failed_event = {+ "type": "response.failed",+ "sequence_number": 4,+ "response": {+ "id": response_id,+ "object": "response",+ "created_at": 1786374786,+ "status": "failed",+ "model": "gpt-4o-mini-2024-07-18",+ "output": [],+ "usage": {+ "input_tokens": 14,+ "input_tokens_details": {"cached_tokens": 0},+ "output_tokens": 7,+ "output_tokens_details": {"reasoning_tokens": 0},+ "total_tokens": 21,+ },+ "error": {"code": "server_error", "message": "The model failed to generate a response"},+ "incomplete_details": None,+ "instructions": None,+ "metadata": {},+ "parallel_tool_calls": True,+ "temperature": 1.0,+ "tool_choice": "auto",+ "tools": [],+ "top_p": 1.0,+ },+ }+ logging_obj = self._create_mock_logging_obj()++ result = OpenAIPassthroughLoggingHandler._handle_logging_openai_collected_chunks(+ litellm_logging_obj=logging_obj,+ passthrough_success_handler_obj=MagicMock(),+ url_route="https://api.openai.com/v1/responses",+ request_body={"model": "gpt-4o-mini", "stream": True},+ endpoint_type=MagicMock(),+ start_time=self.start_time,+ all_chunks=[f"data: {json.dumps(failed_event)}"],+ end_time=self.end_time,+ )++ response = result["result"]+ assert response.id == response_id+ assert response.status == "failed"+ assert response.usage.total_tokens == 21+ assert result["kwargs"]["response_cost"] == 1.4e-06+ mock_completion_cost.assert_called_once_with(+ completion_response=response,+ model="gpt-4o-mini",+ custom_llm_provider="openai",+ call_type="responses",+ )+@patch(f"{OpenAIPassthroughLoggingHandler.__module__}.get_standard_logging_object_payload")@patch("litellm.completion_cost")def test_streaming_responses_without_completed_event_returns_none(