Repository navigation
test(logging): regression coverage for streaming /v1/messages OpenAI Responses spend logs - #31388
Conversation
The #28595 fix added unit tests that call _handle_anthropic_messages_response_logging directly, but nothing exercises the streaming wiring that actually regressed: a streaming /v1/messages call cross-routed to the OpenAI Responses backend whose success handler took the no-op async_log_stream_event path and dropped the SpendLogs row. Add an end-to-end test that drives litellm.anthropic_messages(stream=True) with a mocked upstream Responses SSE and asserts async_log_success_event fires with non-zero cost and call_type anthropic_messages, plus a key-gated live counterpart.
Greptile SummaryAdds an end-to-end regression test for the streaming
Confidence Score: 5/5Test-only change with no production code touched; the mock is fully self-contained and The change adds a single, well-structured regression test with a fully mocked HTTP layer. It addresses the exact wiring gap the PR describes, resolves the two issues raised in earlier review rounds, and does not touch any production code paths. No files require special attention.
|
| Filename | Overview |
|---|---|
| tests/test_litellm/litellm_core_utils/test_litellm_logging.py | Adds a regression test (test_streaming_anthropic_messages_openai_bridge_fires_success_logging) that drives the streaming anthropic_messages → OpenAI Responses bridge end-to-end with the HTTP layer fully mocked, asserting delta chunks are forwarded and async_log_success_event fires exactly once with a non-zero cost and correct call type. Previous thread issues (live network call, global state) are resolved: no live test exists in this diff, and monkeypatch.setattr properly restores litellm.callbacks. |
Reviews (3): Last reviewed commit: "test(logging): drop live network test fr..." | Re-trigger Greptile
Codecov Report✅ All modified and coverable lines are covered by tests. 📢 Thoughts on this report? Let us know! |
Address review on the streaming bridge regression test: emit output_item.added plus text deltas before response.completed so it covers mid-stream delta handling rather than only end-of-stream success logging, assert at least one content_block_delta surfaces, restore litellm.callbacks via monkeypatch instead of leaking global state, and assert async_log_success_event fires exactly once.
Greptile flagged that tests/test_litellm only permits mock tests; network calls belong in tests/e2e. Remove the key-gated live counterpart and keep the deterministic mocked test as the regression guard. The live verification stays in the PR description as the proof of fix.
Relevant issues
Adds the missing regression coverage for #28595 (and the related #28943)
Linear ticket
Resolves #4018
Pre-Submission checklist
make test-unit@greptileaiand received a Confidence Score of at least 4/5 before requesting a maintainer reviewType
✅ Test
Changes
The fix for #28595 (translate the Responses bridge result to a ModelResponse for spend logs) shipped in v1.90.0 and is correct, but the tests it added all call
_handle_anthropic_messages_response_loggingdirectly. Nothing exercises the streaming path that actually regressed: a streamingPOST /v1/messagescross-routed to an OpenAI Responses backend returned 200 with a full body, but the success handler took the no-opasync_log_stream_eventbranch, so_PROXY_track_cost_callbacknever fired and noLiteLLM_SpendLogsrow landed. A refactor of_get_assembled_streaming_responseor the success-handler routing could reintroduce that drop while every existing test stays greenThis adds a deterministic end-to-end guard in the mapped test file. It drives
litellm.anthropic_messages(model="openai/gpt-4o", stream=True)with the upstream Responses SSE mocked at the HTTP layer. The mocked stream emits an opened message item, two text deltas, then the terminalresponse.completed, so the test covers mid-stream delta forwarding as well as end-of-stream success logging. After draining the stream it asserts at least onecontent_block_deltasurfaced, thatasync_log_success_eventfired exactly once, and that the recorded payload has a non-zeroresponse_costwithcall_type == "anthropic_messages". Checking cost and call_type (not just "a row exists") keeps the guard strong against a mutation that bills the row asaresponsesor with zero cost.litellm.callbacksis set through themonkeypatchfixture so global logger state is restored after the test. The test is fully mocked and makes no network calls, so it stays within the isolation rules fortests/test_litellmScreenshots / Proof of Fix
The deterministic test was run against both the pre-fix and post-fix code to confirm it is a true fail-before / pass-after guard, not a tautology
Pre-fix (v1.89.4): the stream runs to completion (3 chunks) but only the no-op
async_log_stream_eventfires and no payload is recorded, so the test failsPost-fix (current staging), the deterministic test plus the existing #28595 unit block pass
The live behavior was also verified by hand against a real proxy hitting OpenAI on
/v1/messageswithstream: true: pre-fix the streaming call returned 200 with a full body but wrote zeroLiteLLM_SpendLogsrows, post-fix the same call wrote a row with non-zero cost