ds4 icon indicating copy to clipboard operation
ds4 copied to clipboard

server: preserve reasoning_content token boundaries

Open rareba opened this issue 3 months ago • 1 comments

Summary

  • keep OpenAI reasoning_content SSE deltas aligned to decoded-token boundaries
  • retain one-token look-behind while checking for the closing think marker
  • replace the regression that expected arbitrary byte splitting

Root cause

The thinking stream held a fixed seven-byte suffix to detect a possibly split closing think marker. Every update therefore emitted the current decoded token minus seven bytes, then emitted that tail with the next token.

Remembering the previous decoded-token end preserves whole-token deltas while keeping the close-tag and UTF-8 safeguards intact. The only cost is one token of reasoning-stream latency.

Impact

Clients that process individual reasoning deltas now receive stable token units instead of character fragments. Reassembled output is unchanged.

Validation

Windows 11 host, WSL Linux CPU/reference build; no model required:

  • focused server test binary: ds4 tests: ok, server: OK
  • make -j2 cpu

Closes #685

rareba avatar Aug 05 '26 03:08 rareba

Nice diagnosis and fix. Since your diff is scoped to the OpenAI path, opened #712 to apply the exact same reasoning_token_end technique to responses_sse_stream_update() and anthropic_sse_stream_update() — the other two places #685 pointed at with the identical shape. Not touching your diff at all, just extending the same idea to the rest of the file.

Flor1an-B avatar Aug 05 '26 18:08 Flor1an-B