mirror of
https://github.com/Niko1221/Strata.git
synced 2026-12-03 19:03:30 +07:00
[PR #2870] [Engine] serve: put the streamed usage in its own final chunk when include_usage is requested #4305
Labels
No labels
needs-format
pull-request
No milestone
No project
No assignees
1 participant
Notifications
Due date
No due date set.
Dependencies
No dependencies set.
Reference
Niko1221/Strata#4305
Loading…
Add table
Add a link
Reference in a new issue
No description provided.
Delete branch "%!s()"
Deleting a branch is permanent. Although the deleted branch may continue to exist for a short time before it actually gets removed, it CANNOT be undone in most cases. Continue?
📋 Pull Request Information
Original PR: https://github.com/Niko1221/Strata/pull/2870
Author: @Eranicus30666
Created: 10/17/2026
Status: 🔄 Open
Base:
main← Head:serve-stream-usage-final-chunk📝 Commits (2)
c961120serve: put the streamed usage in its own final chunk when include_usage is requested7eac2deserve: ignore a stream_options that is not an object when splitting the usage chunk📊 Changes
2 files changed (+81 additions, -0 deletions)
View changed files
📝
serve/server.py(+13 -0)📝
serve/test_server.py(+68 -0)📄 Description
Problem and result
/v1/chat/completionsignoresstream_options.include_usage: the streamedusagealways stays on thefinish_reasonchunk. OpenAI's streaming contract puts it in its own last chunk with"choices": []when the option is set, and gateways and clients read it there.With this change, when
stream_options.include_usageis true, the chunk that carriesusageis written as two chunks: the same chunk withoutusage, then a chunk with"choices": []and theusage. Without the option (absent,falseornull) the original write is used, so nothing changes. Non-streaming replies are untouched.Re-checked on the current
main(identical tov0.2.6,4b9e05a2):serve/server.pystill has no handling ofstream_options, so the behaviour described in #2865 is still there.Related discussion
Resolves #2865 (closed in the 0.2.0-beta tracker reset). No scope agreement needed: a focused fix of one streaming path.
Approach
In the streaming loop, a
split_usageflag is read fromstream_options.include_usage; only a chunk that carriesusageis split. Default path,/v1/messages,/v1/responsesand the legacy completions endpoint are not touched. Three tests inserve/test_server.py(usage alone in the lastchoices: []chunk with the option; usage unchanged on thefinish_reasonchunk without it, includingfalseandnull; same numbers as the non-streaming reply).Validation
Revision tested:
v0.2.6(main,4b9e05a2) + this one commit (c9611206, the current head of this branch). The tests and the stream checks ran on a clone of the pushed branch itself. Environment: Debian 13, venv fromrequirements.txt; engine built from the same tag for sm_86 with CUDA 13.4 (2x NVIDIA A2 16 GB, UD-Q4_K_XL); the built engine reportsstrata 0.1.51.Passed:
python -m unittest serve.test_server: 355 tests, OK (111.5 s).serve/server.py --engine strata) in an isolated trial: 3 of 3 pass (usage alone in the lastchoices: []chunk; nousageon thefinish_reasonchunk; unchanged without the option).include_usageends with achoices: []chunk carrying theusage, and thefinish_reasonchunk has none. (The without-option case was not repeated there.)serve/server.pyonv0.2.6has no handling ofstream_options: a search forinclude_usageonly finds a test payload inserve/test_effort_paths.py.Failed:
none observed with the commit.
Not run:
--engine mock) onv0.2.6; it was run onv0.2.1.1for the previous revision of this PR.v0.2.6without this commit. The only control is the earlier one onv0.2.1.1(the twoinclude_usagechecks fail, the default-path check passes).serve/), no engine file is touched, and the engine used was built from the unmodified tag.v0.2.6./v1/messages,/v1/responses, the legacy completions endpoint: unchanged by the diff, not exercised.Limits
usagekey. As I read OpenAI's reference, it sends"usage": nullon them when the option is set; this change does not add that. Clients that read the last chunk work either way, and I can add it here if you prefer it./v1/chat/completionsis covered.Co-authored with Claude.
🔄 This issue represents a GitHub Pull Request. It cannot be merged through Gitea due to API limitations.