Repository navigation
fix(capture): keep unknown usage counts for streamed /v1/responses calls - #3317
Conversation
…ed SSE stream Signed-off-by: jiwasawa <[email protected]>
| ("output_tokens_details", "reasoning_tokens"), | ||
| ): | ||
| if (usage.get(details_key) or {}).get(counter_key) is None: | ||
| usage.pop(details_key, None) |
There was a problem hiding this comment.
[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.
There was a problem hiding this comment.
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.
Signed-off-by: jiwasawa <[email protected]>
cmunley1
left a comment
There was a problem hiding this comment.
Can you resolve the conflicts and also add the preserve unknown fix you suggest either here or in a 2nd stacked PR?
Signed-off-by: jiwasawa <[email protected]>
Signed-off-by: jiwasawa <[email protected]>
|
Thanks! Resolved the conflicts by merging main. #3968 had already landed the same SSE zeroing ( 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 |
|
/ok to test 1a5417a |
Signed-off-by: jiwasawa <[email protected]>
|
Merged main to pick up #4966. The required check failed only on @yaoyu-33 could you run |
|
/ok to test bd8f39e |
|
🔄 Merge queue validation started! You can track the progress here: https://github.com/NVIDIA-NeMo/Gym/actions/runs/40384930291 |
|
🔄 Merge queue validation started! You can track the progress here: https://github.com/NVIDIA-NeMo/Gym/actions/runs/40389313403 |
What does this PR do?
Refs #3313 (the wire-side fix landed upstream in #3968 ).
#3968 reports an unknown
cached_tokens/reasoning_tokensas0in 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 recordnull(#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
TestResponsesDispatchRoute::test_streaming_capture_keeps_unknown_usage_counts. The SSE stream carries0, and the captured record carriesnull.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
pre-commit run --all-files) (so CI lint/format/copyright pass).git commit -s) (so the DCO check passes).