You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
fix(frontend): observe completions metrics before dropping empty chunks - #18811
Streaming /v1/completions drops empty chunks before it records their metrics. An empty chunk occurs, for example, when a token is only part of a multi-byte character. Each empty chunk carries the metrics of one generated token. Thus, the output-token count is too low, and TTFT is recorded too late.
This PR records the metrics first. Then it drops the chunk. The chat handler already uses this order (#11251).
Key Changes
Core logic: 24 lines added and 14 removed, in one file.
completions_single: remove the filter that ran before the metrics. Record the metrics of an empty chunk, then drop the chunk.
The client stream does not change. The client still gets no empty chunks. The empty check runs before the echo fold. A chunk with echo is never empty, so echo does not change.
These metrics can change for output with many empty chunks, for example CJK text or emoji:
dynamo_frontend_output_tokens_total increases. It now counts all generated tokens.
TTFT can decrease. It is now measured at the first generated token. This also applies to dynamo_frontend_worker_last_time_to_first_token_seconds.
ITL gets more samples, and its mean decreases. This also applies to dynamo_frontend_worker_last_inter_token_latency_seconds.
A request leaves dynamo_frontend_queued_requests at the first generated token.
A request leaves the awaiting-first-token state at the first generated token. This changes dynamo_frontend_awaiting_first_token_requests, dynamo_frontend_inflight_input_tokens{state} and the Pool Relay serving-load frame at the same time.
No metric name, type, unit or label changes. The values now match the catalog descriptions. Dashboards for streaming completions can show a step change.
Validation
Cause. The filter removed an empty chunk before the code recorded its metrics. The non-streaming path and the KServe gRPC path record every chunk. Thus, the same request gave different counts in streaming and non-streaming mode.
Tests. The test engine sends 1 empty chunk (1 token). Then it holds the next chunk (1 token) until the test opens a gate.
Streaming /v1/completions dropped empty chunks before it observed their
metrics, so output tokens were undercounted and TTFT moved to the first
non-empty chunk. Observe first, then drop, as chat does.
Signed-off-by: spa5k <79936503+spa5k@users.noreply.github.com>
👋 Hi spa5k! Thank you for contributing to ai-dynamo/dynamo.
Just a reminder: The NVIDIA Test Github Validation CI runs an essential subset of the testing framework to quickly catch errors.Your PR reviewers may elect to test the changes comprehensively before approving your changes.
No actionable comments were generated in the recent review.
Walkthrough
Single and batched /v1/completions streaming paths now observe response metrics for empty chunks before omitting them from client output. Tests verify TTFT, output-token totals, and streamed text for single and batched requests.
Changes
Completion streaming metrics
Layer / File(s)
Summary
Observe metrics before filtering empty chunks lib/llm/src/http/service/openai.rs, lib/llm/tests/http_metrics.rs
Both streaming paths observe metrics before omitting empty chunks. Tests check TTFT while the first empty chunk is gated, confirm output-token totals, and verify clients receive only the nonempty text chunk.
Streaming completions now count tokens and record first-token timing for empty chunks, and clients still receive no empty chunks. Tests cover single and batched requests. No concrete merge-blocking risk remains.
Pre-merge checks: 4
Check name
Status
Explanation
Title check
The title "fix(frontend): observe completions metrics before dropping empty chunks" names the main change. The PR moves metrics recording for streaming /v1/completions ahead of the empty-chunk filter …
Linked Issues check
Issue #18814 requires that streaming /v1/completions count tokens carried by empty chunks, record TTFT at the first empty chunk, and keep the client stream unchanged. In lib/llm/src/http/service/opena…
Out of Scope Changes check
The whole-PR diff against merge base b20fe6f changes only lib/llm/src/http/service/openai.rs and lib/llm/tests/http_metrics.rs. The openai.rs hunks move the empty-chunk handling in completions_sing…
Description check
The description follows the repository template closely. It has Overview, Key Changes, Validation, and Issues and Ongoing Work sections, and it links issue #18814 with a 'Closes' line. It also ticks t…
Full details: Title check
Explanation
The title "fix(frontend): observe completions metrics before dropping empty chunks" names the main change. The PR moves metrics recording for streaming /v1/completions ahead of the empty-chunk filter in completions_single and completions_batch, which the raw summary and objectives confirm. The title is specific and matches the changeset.
Full details: Linked Issues check
Explanation
Issue #18814 requires that streaming /v1/completions count tokens carried by empty chunks, record TTFT at the first empty chunk, and keep the client stream unchanged. In lib/llm/src/http/service/openai.rs, both completions_single and completions_batch now test for an empty chunk inside the map step. An empty chunk calls process_response_and_observe_metrics before returning Ok(None). This replaces the earlier pre-metrics filter, so token counts and TTFT now include empty chunks. The client stream still omits empty chunks. The empty check runs before the echo handling, so echo output is unchanged. The new tests in lib/llm/tests/http_metrics.rs cover the single-prompt case (expected 2 output tokens) and the 2-prompt batch case (expected 4 output tokens). They also assert that TTFT is observed while the gate is closed and that the client receives only non-empty text. The tests were reported to fail on main and pass with this PR. The PR author ran them on macOS arm64 only. Coding requirements are met by the diff. I did not run the tests, and I did not inspect the downstream filter that consumes Ok(None), so that wiring rests on the diff and the author's test report.
Full details: Out of Scope Changes check
Explanation
The whole-PR diff against merge base b20fe6f changes only lib/llm/src/http/service/openai.rs and lib/llm/tests/http_metrics.rs. The openai.rs hunks move the empty-chunk handling in completions_single and completions_batch so metrics are recorded before the drop. The test hunks add a mock engine, a metric-polling helper, and two tests that cover the same behavior. No unrelated code changes appear. The PR description's follow-up JSON-roundtrip work is not part of this diff.
Full details: Description check
Explanation
The description follows the repository template closely. It has Overview, Key Changes, Validation, and Issues and Ongoing Work sections, and it links issue #18814 with a 'Closes' line. It also ticks the pr-checklist box. It explains the cause, the affected metrics, the test cases with before and after results, and the platforms tested. Gaps: the 'Closes' line is in a different form from the template's advisory issue-link format but still names an issue, and the Linux CI matrix and default features were not run, which the author states openly.
Autofix · Keep fixing CodeRabbit findings and required CI, and resolving merge conflicts
Reviewing files that changed from the base of the PR and between b20fe6f and d9e47e3.
Files selected for processing: 2
lib/llm/src/http/service/openai.rs
lib/llm/tests/http_metrics.rs
Included review availability: This review used your included allowance. Your plan provides up to 12 included reviews per hour; 11 remain after this review.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Overview
Streaming
/v1/completionsdrops empty chunks before it records their metrics. An empty chunk occurs, for example, when a token is only part of a multi-byte character. Each empty chunk carries the metrics of one generated token. Thus, the output-token count is too low, and TTFT is recorded too late.This PR records the metrics first. Then it drops the chunk. The chat handler already uses this order (#11251).
Key Changes
Core logic: 24 lines added and 14 removed, in one file.
completions_single: remove the filter that ran before the metrics. Record the metrics of an empty chunk, then drop the chunk.completions_batch: the same change for prompt batches.tests/http_metrics.rs: 2 tests through the real HTTP handler.The client stream does not change. The client still gets no empty chunks. The empty check runs before the
echofold. A chunk withechois never empty, soechodoes not change.These metrics can change for output with many empty chunks, for example CJK text or emoji:
dynamo_frontend_output_tokens_totalincreases. It now counts all generated tokens.dynamo_frontend_worker_last_time_to_first_token_seconds.dynamo_frontend_worker_last_inter_token_latency_seconds.dynamo_frontend_queued_requestsat the first generated token.dynamo_frontend_awaiting_first_token_requests,dynamo_frontend_inflight_input_tokens{state}and the Pool Relay serving-load frame at the same time.No metric name, type, unit or label changes. The values now match the catalog descriptions. Dashboards for streaming completions can show a step change.
Validation
Cause. The filter removed an empty chunk before the code recorded its metrics. The non-streaming path and the KServe gRPC path record every chunk. Thus, the same request gave different counts in streaming and non-streaming mode.
Tests. The test engine sends 1 empty chunk (1 token). Then it holds the next chunk (1 token) until the test opens a gate.
test_streaming_completions_observes_empty_chunk_metricscoverscompletions_single.test_streaming_completions_batch_observes_empty_chunk_metricscoverscompletions_batch.mainand pass with this PR. The 2 tests take less than 1 s together. Each wait has a timeout.I ran the tests on macOS arm64 with
-p dynamo-llm --no-default-features, baseb20fe6fe94. I did not run the Linux CI matrix or the default features.Issues and Ongoing Work
Pull Request Checklist
/pr-checklistskill.