Skip to content

fix(capture): keep unknown usage counts for streamed /v1/responses calls - #3317

Merged
cmunley1 merged 5 commits into
NVIDIA-NeMo:mainfrom
jiwasawa:jiwasawa/streaming-usage-details
Oct 26, 2026
Merged

cmunley1 merged 5 commits into
NVIDIA-NeMo:mainfrom
jiwasawa:jiwasawa/streaming-usage-details

Conversation

@jiwasawa

@jiwasawa jiwasawa commented Sep 11, 2026 •

Copy link
Copy Markdown
Contributor

What does this PR do?

Refs #3313 (the wire-side fix landed upstream in #3968 ).

#3968 reports an unknown cached_tokens / reasoning_tokens as 0 in the synthesized Responses SSE stream.
Model-call capture rebuilds a streamed response from that stream, so streamed calls with unknown counts were recorded as cached_tokens=0 / cache_hit=False, while non-streaming calls record null (#2583 ).

The streaming dispatch now passes its own usage, before the SSE zeroing, to the capture middleware via request.state, and capture records it for streamed calls.
The SSE stream sent to clients is unchanged.
The usage is passed only after the stream is built, so a call that fails during finalization is not recorded with the successful response's usage.

Tests

  • New: TestResponsesDispatchRoute::test_streaming_capture_keeps_unknown_usage_counts. The SSE stream carries 0, and the captured record carries null.
  • pytest tests/unit_tests/test_responses_api_model_streaming.py tests/unit_tests/test_base_responses_api_model.py responses_api_models/vllm_model_with_compaction/tests: 313 passed.

Checklist

  • I have read the contributing guidelines.
  • The change is focused; unrelated "drive-by" edits are tracked as separate issues/PRs.
  • Tests added or updated and pass locally, or N/A for docs-only / non-code changes (so CI unit/server checks pass when applicable).
  • Pre-commit checks pass locally (pre-commit run --all-files) (so CI lint/format/copyright pass).
  • All commits have DCO sign-off (git commit -s) (so the DCO check passes).

@copy-pr-bot

copy-pr-bot Bot commented Sep 11, 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.

@jiwasawa
jiwasawa marked this pull request as ready for review September 11, 2026 06:45
@yaoyu-33 yaoyu-33 added area:core Shared APIs, servers, telemetry, health, and registries bug Something isn't working needs-review PR is ready for code review and waiting on a reviewer labels Sep 11, 2026
Comment thread nemo_gym/responses_streaming.py Outdated
("output_tokens_details", "reasoning_tokens"),
):
if (usage.get(details_key) or {}).get(counter_key) is None:
usage.pop(details_key, None)

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[P1] Keep the required usage detail objects on the wire

Could we normalize an unknown counter to 0 in the synthesized SSE copy instead of popping its detail object? In pinned openai==2.44.0, ResponseUsage requires both input_tokens_details and output_tokens_details; I verified the emitted usage here fails ResponseUsage.model_validate() with “input_tokens_details: Field required.” Codex happens to tolerate the absence, but other strict Responses clients may not. Setting only cached_tokens/reasoning_tokens to 0 at this wire boundary keeps the SSE schema-valid while the internal and non-streaming values remain None, preserving #2583’s unknown semantics inside Gym.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done, thanks. The helper now sets a missing or null cached_tokens / reasoning_tokens to 0 in the SSE copy and keeps both detail objects. I reproduced your check with openai==2.44.0: the omitted object fails ResponseUsage.model_validate() with "Field required", and the zeroed one passes. The test now runs that validation on the emitted usage and asserts the caller's response still carries null.

One thing to flag. Model-call capture rebuilds streamed responses from the wire (_reconstruct_responses_sse), so streamed calls with an unknown cached count will now be recorded as cached_tokens=0 / cache_hit=False, which #2583 avoided for non-streaming. Non-streaming capture and everything internal still see null. If the unknown marker should be preserved for streamed calls too, the dispatch could publish its pre-normalization usage to the capture middleware and have capture prefer it over the wire value for that field. We need a ~20-line change in base_responses_api_model.py to do this.

@yaoyu-33 yaoyu-33 added waiting-on-customer Waiting on the original author to respond and removed needs-review PR is ready for code review and waiting on a reviewer labels Sep 11, 2026
@jiwasawa jiwasawa changed the title fix(responses): omit unknown usage detail counters from the synthesized SSE stream fix(responses): report unknown usage detail counters as 0 in the synthesized SSE stream Sep 14, 2026
@jiwasawa
jiwasawa requested a review from yaoyu-33 September 15, 2026 03:38
@yaoyu-33 yaoyu-33 added the complexity:low Localized change in one scope with a small, straightforward review surface label Sep 15, 2026
@github-actions github-actions Bot added the sla:review-overdue Review response is over the one-business-day SLA label Sep 16, 2026

@cmunley1 cmunley1 left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you resolve the conflicts and also add the preserve unknown fix you suggest either here or in a 2nd stacked PR?

@jiwasawa jiwasawa changed the title fix(responses): report unknown usage detail counters as 0 in the synthesized SSE stream fix(capture): keep unknown usage counts for streamed /v1/responses calls Oct 21, 2026
@jiwasawa

Copy link
Copy Markdown
Contributor Author

Thanks! Resolved the conflicts by merging main. #3968 had already landed the same SSE zeroing (_wire_usage), so I dropped my version and its test in favor of upstream's.

I added the preserve-unknown fix here rather than in a stacked PR, since that is now all this PR does. The streaming dispatch passes its usage, before the SSE zeroing, to the capture middleware, so streamed calls with unknown counts are recorded as null instead of cached_tokens=0 / cache_hit=False, the same as non-streaming. I updated the title and description to match. #3313 can be closed now that #3968 is in.

@yaoyu-33

Copy link
Copy Markdown
Contributor

/ok to test 1a5417a

@yaoyu-33 yaoyu-33 left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM at 1a5417a.

@yaoyu-33 yaoyu-33 added ready-to-merge PR is approved, current, and waiting for final CI or merge and removed waiting-on-customer Waiting on the original author to respond labels Oct 23, 2026
Signed-off-by: jiwasawa <[email protected]>
@jiwasawa

Copy link
Copy Markdown
Contributor Author

Merged main to pick up #4966. The required check failed only on test_exited_shell_recovery_discards_pending_input_and_reports_lost_state[interrupt] in terminus_2_sandboxed_agent, which #4966 fixes for the Ubuntu 26.04 runners. The PR's own diff against main is unchanged.

@yaoyu-33 could you run /ok to test bd8f39e7589b66f8df6c83949611811335027801?
@cmunley1 both of your requests are addressed (see my comment from Oct 21). Could you take another look?

@marcromeyn

Copy link
Copy Markdown
Contributor

/ok to test bd8f39e

@cmunley1
cmunley1 added this pull request to the merge queue Oct 26, 2026
@github-actions

Copy link
Copy Markdown
Contributor

🔄 Merge queue validation started! You can track the progress here: https://github.com/NVIDIA-NeMo/Gym/actions/runs/40384930291

@github-actions github-actions Bot removed the sla:review-overdue Review response is over the one-business-day SLA label Oct 26, 2026
@github-actions

Copy link
Copy Markdown
Contributor

🔄 Merge queue validation started! You can track the progress here: https://github.com/NVIDIA-NeMo/Gym/actions/runs/40389313403

Merged via the queue into NVIDIA-NeMo:main with commit 9a76458 Oct 26, 2026
76 of 86 checks passed
@jiwasawa
jiwasawa deleted the jiwasawa/streaming-usage-details branch October 28, 2026 03:03

This branch was previously deployed

1 inactive deployment
public — bd8f39e7 Deployed Oct 26, 2026 by copy-pr-bot[bot] via release / finalize / notify #7293
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

area:core Shared APIs, servers, telemetry, health, and registries bug Something isn't working complexity:low Localized change in one scope with a small, straightforward review surface ready-to-merge PR is approved, current, and waiting for final CI or merge

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants