litellm/tests/unit/responses
norkelbyte bda8fafe87
fix(proxy): refund a Responses WebSocket's key TPM reservation when the socket closes (#51086)
* fix(proxy): settle a Responses WebSocket's key TPM admission reservation when the socket closes

A Responses WebSocket reserves key TPM once at admission, but each turn logs under its own call id, so the
limiter's success and failure callbacks never find the connection's stash and the reservation stayed counted
until the window rolled over. Closing the socket only freed the parallel slot, so a few short sockets could
lock a key out of its TPM limit

On close the proxy now releases the slot and refunds the whole reservation through the v3 limiter, sharing
the settle logic with the post-call failure hook. A lock on the stash makes the refund happen once even when
the failure row and the close settle at the same time

* test(proxy): a Responses WebSocket close whose refund fails still frees its slot

* fix(proxy): drop a Responses WebSocket close refund whose rate-limit window already rolled over

* fix(proxy): keep the Responses WebSocket release method name and the failure hook's log line

ProxyLogging.arelease_responses_websocket_slot shipped in v1.107.0, so it keeps its name. The failure hook logs
its old "Error releasing TPM reservation on post-call failure" line again, and a failure hook whose refund fails
still frees its slot without raising

* fix(proxy): settle a closed Responses WebSocket's reservations by how it ended

A socket closed mid-turn keeps every reservation, since a cut turn charges nothing. A socket whose turns finished refunds its TPM but keeps the team PTU estimate, since turns never charge PTU. A socket a later pre-call hook refused at open frees its slot and every reservation

* fix(responses): report a managed socket turn the session limit cancels as unfinished

* fix(proxy): settle a closed socket's TPM after its turns log and keep HTTP refunds unguarded

A socket whose turns all finished now waits up to 5 s for those turns' logging before it refunds its key, team and
user TPM, so the key never counts less than it used. The slot is freed before the wait.

HTTP settles go back to main's plain refund, so a failed Redis window check no longer drops an HTTP request's
refund. Pass-through routes with no turn classifier leave the reservation to their session row, as on main, and
arelease_responses_websocket_slot accepts one argument again.

* fix(proxy): close a socket's client before its release waits on turn logging

When the upstream or the session limit ends a Responses WebSocket, the client's
close now goes out before the release waits for the turns' logging, so the client
never waits on logging. A socket closed with a turn still open marks its
reservation kept before its first await, so its failure row cannot refund it first

* fix(proxy): leave a Responses WebSocket's reservation to its close and send no close frame main never sent

The native socket's route no longer closes the client itself when the relay ends, so the client
sees what it saw on main, and a finished socket's refund waits for turn logging in the background.
A socket's failure row now frees only its slot, and a relay that fails before it returns closes as
refused at open, so it refunds everything

* fix(proxy): free a legacy limiter's slot when a Responses WebSocket relay fails

* test(proxy): wait for a closed socket's refund before opening the next socket

* fix(proxy): keep a pass-through socket's reservation when its handler is cancelled mid-turn

A cancelled pass-through handler skipped the drain that marks a turn as cut, so its close refunded the reservation of a turn that was never billed. The close now also counts any turn still owed at cleanup as cut

The limiter tests that check a finished socket's refund now wait for the background refund, since before Python 3.12 that task only starts once the caller yields

* refactor(proxy): spell each socket close kind explicitly in the Rust limiter

* fix(proxy): keep a closed socket's TPM reservation when its turns are still logging after the wait

---------

Co-authored-by: mateo-berri <277851410+mateo-berri@users.noreply.github.com>
2026-11-23 23:05:26 -08:00
..
litellm_completion_transformation fix(openai): relay provider error codes, types, and Responses params from OpenAI-compatible backends (#51809) 2026-11-24 02:23:00 +00:00
mcp feat(responses): store each bridged turn's path to the root so a fork replays only its ancestors (#49519) 2026-11-23 12:43:11 -08:00
__init__.py
test_additional_tools.py feat(proxy): mixed Codex model picker and LiteLLM routing on native codex routes (#49408) 2026-11-06 11:14:47 -08:00
test_custom_tool_call.py
test_input_items_query.py fix(proxy): forward limit, order, after, before and include on GET /v1/responses/{id}/input_items (#51259) 2026-11-20 07:54:26 -08:00
test_instructions_length.py fix(responses): send bridged system prompts too long for instructions as input (#52964) 2026-11-20 11:56:47 -08:00
test_metadata_codex_callback.py
test_no_duplicate_spend_logs.py
test_null_test_fix.py
test_responses_api_bridge_flag.py fix(openai)!: apply drop_params to reasoning objects on openai/ chat, openai/responses/ and /v1/messages (#53865) 2026-11-24 04:16:41 +00:00
test_responses_api_lifecycle.py fix(responses): log the resolved provider on Responses API calls (#52622) 2026-11-18 22:34:16 +00:00
test_responses_api_request_body.py fix(responses): log the resolved provider on Responses API calls (#52622) 2026-11-18 22:34:16 +00:00
test_responses_prompt_management.py fix(sdk): decide MCP gateway routing without importing the proxy handler (#45961) 2026-10-20 01:44:54 +00:00
test_responses_router_cooldown.py fix(openai): relay provider error codes, types, and Responses params from OpenAI-compatible backends (#51809) 2026-11-24 02:23:00 +00:00
test_responses_streaming_iterator.py fix(responses): resume background response streams with stream and starting_after (#48819) 2026-10-29 02:31:22 -07:00
test_responses_supported_endpoints_passthrough.py
test_responses_utils.py fix(openai): forward Responses background_options (#53991) 2026-11-23 17:56:11 -08:00
test_responses_websocket_all_providers.py fix(proxy): refund a Responses WebSocket's key TPM reservation when the socket closes (#51086) 2026-11-23 23:05:26 -08:00
test_rust_bridge_websocket.py refactor(rust-bridge): require the native extension (#48852) 2026-10-28 13:16:56 -07:00
test_sse_output_recovery.py
test_streaming_iterator.py fix(responses): always stream tool argument fields as strings for every Responses provider (#53953) 2026-11-24 03:45:17 +00:00
test_streaming_iterator_error_events.py fix(openai): relay provider error codes, types, and Responses params from OpenAI-compatible backends (#51809) 2026-11-24 02:23:00 +00:00
test_text_format_conversion.py refactor: expose core private helpers under public names (#44871) 2026-10-07 00:05:27 +00:00