Skip to content

test(logging): regression coverage for streaming /v1/messages OpenAI Responses spend logs - #31388

Merged
yucheng-berri merged 3 commits into
litellm_internal_stagingfrom
litellm_lit4018
Jun 26, 2026
Merged

yucheng-berri merged 3 commits into
litellm_internal_stagingfrom
litellm_lit4018

Conversation

@yucheng-berri

@yucheng-berri yucheng-berri commented Jun 26, 2026 •

Copy link
Copy Markdown
Contributor

Relevant issues

Adds the missing regression coverage for #28595 (and the related #28943)

Linear ticket

Resolves #4018

Pre-Submission checklist

  • I have added meaningful tests
  • My PR passes all unit tests on make test-unit
  • My PR's scope is as isolated as possible; it only solves 1 specific problem
  • I have requested a Greptile review by commenting @greptileai and received a Confidence Score of at least 4/5 before requesting a maintainer review

Type

✅ 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_logging directly. Nothing exercises the streaming path that actually regressed: a streaming POST /v1/messages cross-routed to an OpenAI Responses backend returned 200 with a full body, but the success handler took the no-op async_log_stream_event branch, so _PROXY_track_cost_callback never fired and no LiteLLM_SpendLogs row landed. A refactor of _get_assembled_streaming_response or the success-handler routing could reintroduce that drop while every existing test stays green

This 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 terminal response.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 one content_block_delta surfaced, that async_log_success_event fired exactly once, and that the recorded payload has a non-zero response_cost with call_type == "anthropic_messages". Checking cost and call_type (not just "a row exists") keeps the guard strong against a mutation that bills the row as aresponses or with zero cost. litellm.callbacks is set through the monkeypatch fixture 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 for tests/test_litellm

Screenshots / 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_event fires and no payload is recorded, so the test fails

1.89.4 -> chunks streamed: 3 | stream_event(no-op) calls: 1 | success_payload set: False
FAILED ... test_streaming_anthropic_messages_openai_bridge_fires_success_logging
  AssertionError: async_log_success_event never fired for streaming /v1/messages -> openai Responses bridge

Post-fix (current staging), the deterministic test plus the existing #28595 unit block pass

$ pytest test_litellm_logging.py -k "anthropic_messages_response_logging or anthropic_messages_openai_bridge"
.......  7 passed, 97 deselected

The live behavior was also verified by hand against a real proxy hitting OpenAI on /v1/messages with stream: true: pre-fix the streaming call returned 200 with a full body but wrote zero LiteLLM_SpendLogs rows, post-fix the same call wrote a row with non-zero cost

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-apps

greptile-apps Bot commented Jun 26, 2026 •

Copy link
Copy Markdown
Contributor

Greptile Summary

Adds an end-to-end regression test for the streaming POST /v1/messages → OpenAI Responses backend path that previously regressed (#28595/#28943). The fix shipped in v1.90.0 but lacked a test exercising the streaming wiring; this PR closes that gap. Issues flagged in earlier review rounds (a live-network test and bare litellm.callbacks assignment) are both resolved here: the diff contains only the mocked test, and monkeypatch.setattr restores global state after each test run.

  • A new _SuccessCapturingLogger fixture class records whether async_log_success_event or only the no-op async_log_stream_event fires, making the guard sensitive to the exact regression vector.
  • _fake_streaming_responses_http_response replaces the upstream SSE at the HTTP transport layer so no real network calls are made; the test is fully isolated for CI.
  • Post-stream assertions check delta chunks were forwarded, success logging fired exactly once, cost is non-zero, and call_type is "anthropic_messages" — strong enough to catch mutations that bill under a wrong call type or with zero cost.

Confidence Score: 5/5

Test-only change with no production code touched; the mock is fully self-contained and monkeypatch ensures no global state leaks between test runs.

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.

Important Files Changed

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

codecov Bot commented Jun 26, 2026

Copy link
Copy Markdown

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.
@yucheng-berri

Copy link
Copy Markdown
Contributor Author

@greptileai

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.
@yucheng-berri

Copy link
Copy Markdown
Contributor Author

@greptileai

@yucheng-berri
yucheng-berri requested a review from tin-berri June 26, 2026 02:36
@yucheng-berri
yucheng-berri merged commit 7680ced into litellm_internal_staging Jun 26, 2026
74 checks passed
@yucheng-berri
yucheng-berri deleted the litellm_lit4018 branch June 26, 2026 03:30
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[Bug]: Authentication Error, No module named 'nacl'.

2 participants