Skip to content

fix(frontend): observe completions metrics before dropping empty chunks - #18811

Open
spa5k wants to merge 4 commits into
ai-dynamo:mainfrom
spa5k:feat/completions-empty-chunk-metrics
Open

spa5k wants to merge 4 commits into
ai-dynamo:mainfrom
spa5k:feat/completions-empty-chunk-metrics

Conversation

@spa5k

@spa5k spa5k commented Nov 15, 2026 •

Copy link
Copy Markdown
Contributor

Overview

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.

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.

Case Before After
1 prompt: TTFT while the gate is closed Not recorded. The test times out after 5 s. Recorded
1 prompt: output tokens 1 2
2-prompt batch: TTFT while the gate is closed Not recorded. The test times out after 5 s. Recorded
2-prompt batch: output tokens 2 4
Client stream No empty chunks No change
  • test_streaming_completions_observes_empty_chunk_metrics covers completions_single.
  • test_streaming_completions_batch_observes_empty_chunk_metrics covers completions_batch.
  • Both tests fail on main and 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, base b20fe6fe94. I did not run the Linux CI matrix or the default features.

Issues and Ongoing Work

Pull Request Checklist
  • I used Dynamo's /pr-checklist skill.

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>
Signed-off-by: spa5k <79936503+spa5k@users.noreply.github.com>
@copy-pr-bot

copy-pr-bot Bot commented Nov 15, 2026

Copy link
Copy Markdown

This pull request requires additional validation before any workflows can run on NVIDIA's runners.

Pull request vetters can view their responsibilities here.

Contributors can view more details about this message here.

@spa5k
spa5k temporarily deployed to external_collaborator November 15, 2026 16:11 — with GitHub Actions Inactive
@spa5k
spa5k temporarily deployed to external_collaborator November 15, 2026 16:11 — with GitHub Actions Inactive
@github-actions github-actions Bot added the fix label Nov 15, 2026
@github-actions

Copy link
Copy Markdown
Contributor

👋 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.

🚀

@github-actions github-actions Bot added external-contribution Pull request is from an external contributor frontend `python -m dynamo.frontend` and `dynamo-run in=http|text|grpc` labels Nov 15, 2026
@spa5k
spa5k marked this pull request as ready for review November 15, 2026 16:22
@spa5k
spa5k requested review from a team as code owners November 15, 2026 16:22
@coderabbitai

coderabbitai Bot commented Nov 15, 2026 •

Copy link
Copy Markdown
Contributor

Explain Change →

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.

Estimated code review effort: 3 (Moderate) | ~20 minutes

Severity of issue fixed: Medium

Merge Risk: Minimal · up to d9e47

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: Passed 4

Check name Status Explanation
Title check Passed 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 Passed 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 Passed 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 Passed 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
Recent review info

Run configuration

Commits

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.

Comment thread lib/llm/tests/http_metrics.rs Resolved
Signed-off-by: spa5k <79936503+spa5k@users.noreply.github.com>
@spa5k
spa5k temporarily deployed to external_collaborator November 15, 2026 17:12 — with GitHub Actions Inactive
Comment thread lib/llm/tests/http_metrics.rs Resolved
Comment thread lib/llm/tests/http_metrics.rs Resolved
@spa5k
spa5k temporarily deployed to external_collaborator November 15, 2026 19:29 — with GitHub Actions Inactive

This branch was previously deployed

1 inactive deployment
external_collaborator — 89b76153 Deployed Nov 15, 2026 by spa5k via ok-to-test #51666
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

external-contribution Pull request is from an external contributor fix frontend `python -m dynamo.frontend` and `dynamo-run in=http|text|grpc` size/L

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[BUG]: Streaming /v1/completions undercounts output tokens when a chunk is empty

1 participant