[PR #2870] [Engine] serve: put the streamed usage in its own final chunk when include_usage is requested #4305

Open
opened 2026-11-07 15:34:45 +07:00 by bsakaguchi · 0 comments
Owner

📋 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)

  • c961120 serve: put the streamed usage in its own final chunk when include_usage is requested
  • 7eac2de serve: 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/completions ignores stream_options.include_usage: the streamed usage always stays on the finish_reason chunk. 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_usage is true, the chunk that carries usage is written as two chunks: the same chunk without usage, then a chunk with "choices": [] and the usage. Without the option (absent, false or null) the original write is used, so nothing changes. Non-streaming replies are untouched.

Re-checked on the current main (identical to v0.2.6, 4b9e05a2): serve/server.py still has no handling of stream_options, so the behaviour described in #2865 is still there.

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_usage flag is read from stream_options.include_usage; only a chunk that carries usage is split. Default path, /v1/messages, /v1/responses and the legacy completions endpoint are not touched. Three tests in serve/test_server.py (usage alone in the last choices: [] chunk with the option; usage unchanged on the finish_reason chunk without it, including false and null; 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 from requirements.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 reports strata 0.1.51.

Passed:

  • python -m unittest serve.test_server: 355 tests, OK (111.5 s).
  • Stream check against the real engine (serve/server.py --engine strata) in an isolated trial: 3 of 3 pass (usage alone in the last choices: [] chunk; no usage on the finish_reason chunk; unchanged without the option).
  • The same service later in production use: after switching to this build, and again after a full VM reboot, a streamed request with include_usage ends with a choices: [] chunk carrying the usage, and the finish_reason chunk has none. (The without-option case was not repeated there.)
  • serve/server.py on v0.2.6 has no handling of stream_options: a search for include_usage only finds a test payload in serve/test_effort_paths.py.

Failed:
none observed with the commit.

Not run:

  • The stream check against the mock engine (--engine mock) on v0.2.6; it was run on v0.2.1.1 for the previous revision of this PR.
  • A control run of the checks on v0.2.6 without this commit. The only control is the earlier one on v0.2.1.1 (the two include_usage checks fail, the default-path check passes).
  • HIP, SYCL, Metal builds: the change is Python-only (serve/), no engine file is touched, and the engine used was built from the unmodified tag.
  • Windows and macOS.
  • Clients other than the plain HTTP check above. The LiteLLM symptom reported in #2865 was observed on 0.1.45 in my setup; I did not re-run LiteLLM on v0.2.6.
  • /v1/messages, /v1/responses, the legacy completions endpoint: unchanged by the diff, not exercised.
  • A byte-for-byte comparison of the default path: I compared the chunk structure.

Limits

  • The other chunks carry no usage key. As I read OpenAI's reference, it sends "usage": null on 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.
  • Only /v1/chat/completions is covered.

Co-authored with Claude.


🔄 This issue represents a GitHub Pull Request. It cannot be merged through Gitea due to API limitations.

## 📋 Pull Request Information **Original PR:** https://github.com/Niko1221/Strata/pull/2870 **Author:** [@Eranicus30666](https://github.com/Eranicus30666) **Created:** 10/17/2026 **Status:** 🔄 Open **Base:** `main` ← **Head:** `serve-stream-usage-final-chunk` --- ### 📝 Commits (2) - [`c961120`](https://github.com/Niko1221/Strata/commit/c96112068fc0bef9f8443f0d9d8cafa201c144ad) serve: put the streamed usage in its own final chunk when include_usage is requested - [`7eac2de`](https://github.com/Niko1221/Strata/commit/7eac2deeba29eaca154df726bef78c47e8f31947) serve: ignore a stream_options that is not an object when splitting the usage chunk ### 📊 Changes **2 files changed** (+81 additions, -0 deletions) <details> <summary>View changed files</summary> 📝 `serve/server.py` (+13 -0) 📝 `serve/test_server.py` (+68 -0) </details> ### 📄 Description ## Problem and result `/v1/chat/completions` ignores `stream_options.include_usage`: the streamed `usage` always stays on the `finish_reason` chunk. 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_usage` is true, the chunk that carries `usage` is written as two chunks: the same chunk without `usage`, then a chunk with `"choices": []` and the `usage`. Without the option (absent, `false` or `null`) the original write is used, so nothing changes. Non-streaming replies are untouched. Re-checked on the current `main` (identical to `v0.2.6`, `4b9e05a2`): `serve/server.py` still has no handling of `stream_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_usage` flag is read from `stream_options.include_usage`; only a chunk that carries `usage` is split. Default path, `/v1/messages`, `/v1/responses` and the legacy completions endpoint are not touched. Three tests in `serve/test_server.py` (usage alone in the last `choices: []` chunk with the option; usage unchanged on the `finish_reason` chunk without it, including `false` and `null`; 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 from `requirements.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 reports `strata 0.1.51`. **Passed:** - `python -m unittest serve.test_server`: 355 tests, OK (111.5 s). - Stream check against the real engine (`serve/server.py --engine strata`) in an isolated trial: 3 of 3 pass (usage alone in the last `choices: []` chunk; no `usage` on the `finish_reason` chunk; unchanged without the option). - The same service later in production use: after switching to this build, and again after a full VM reboot, a streamed request with `include_usage` ends with a `choices: []` chunk carrying the `usage`, and the `finish_reason` chunk has none. (The without-option case was not repeated there.) - `serve/server.py` on `v0.2.6` has no handling of `stream_options`: a search for `include_usage` only finds a test payload in `serve/test_effort_paths.py`. **Failed:** none observed with the commit. **Not run:** - The stream check against the mock engine (`--engine mock`) on `v0.2.6`; it was run on `v0.2.1.1` for the previous revision of this PR. - A control run of the checks on `v0.2.6` without this commit. The only control is the earlier one on `v0.2.1.1` (the two `include_usage` checks fail, the default-path check passes). - HIP, SYCL, Metal builds: the change is Python-only (`serve/`), no engine file is touched, and the engine used was built from the unmodified tag. - Windows and macOS. - Clients other than the plain HTTP check above. The LiteLLM symptom reported in #2865 was observed on 0.1.45 in my setup; I did not re-run LiteLLM on `v0.2.6`. - `/v1/messages`, `/v1/responses`, the legacy completions endpoint: unchanged by the diff, not exercised. - A byte-for-byte comparison of the default path: I compared the chunk structure. ## Limits - The other chunks carry no `usage` key. As I read OpenAI's reference, it sends `"usage": null` on 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. - Only `/v1/chat/completions` is covered. Co-authored with Claude. --- <sub>🔄 This issue represents a GitHub Pull Request. It cannot be merged through Gitea due to API limitations.</sub>
Sign in to join this conversation.
No milestone
No project
No assignees
1 participant
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Dependencies

No dependencies set.

Reference
Niko1221/Strata#4305
No description provided.