Skip to content

Studio: keep stream deltas that contain U+2028, U+2029 or U+0085 - #17725

Merged
danielhanchen merged 5 commits into
unslothai:mainfrom
jaeminbird:fix-chat-run-sse-unicode-separators
Nov 15, 2026
Merged

danielhanchen merged 5 commits into
unslothai:mainfrom
jaeminbird:fix-chat-run-sse-unicode-separators

Conversation

@jaeminbird

@jaeminbird jaeminbird commented Nov 14, 2026 •

Copy link
Copy Markdown
Contributor

Studio's chat page streams through durable chat runs, and the run worker's _SSEDecoder split each SSE event with str.splitlines(). That also breaks at U+2028, U+2029 and U+0085, which JSON does not escape: both pydantic's model_dump_json and llama-server write them raw. A delta holding one of these characters is cut in two, json.loads fails on the first half, and the except/continue drops the chunk without a log line. The text is missing from the streamed reply and from the stored message, so it stays missing after a reload. Models emit these characters when they repeat text pasted from a web page or a PDF. This is the same character class as #16725 (S3 JSONL).

The fix splits on \n only. The decoder already turns CRLF into LF and splits events on `

, and the stream it reads is Studio's own LF-only SSE, so nothing else changes; multi-line data:` fields are still joined with newlines.

Feeding _chat_chunk_sse output into _SSEDecoder a few characters at a time, main drops the content chunk and keeps only the finish chunk; with the fix the content arrives intact. The new tests/test_chat_generation_runs_sse_decoder.py (each separator, LF and CRLF) fails 6 of 7 on main and passes 7 of 7 with the fix; together with tests/test_chat_generation_runs.py that is 80 passed, on Windows with Python 3.12.

Same bug on the upstream streams (added by maintainer)

httpx's aiter_lines() uses the same str.splitlines() rule, and every upstream SSE stream Studio reads went through it: the llama-server /v1/chat/completions and /v1/messages passthroughs (the API that agents launched with unsloth start and other clients use), external and managed-engine providers (vLLM writes these characters raw too), the subscription Responses client, and research runs.

Reproduced on main through the real passthrough routes with a llama-server stream whose delta holds U+2028, U+2029 or U+0085:

  • /v1/chat/completions stream: the first half is forwarded as its own SSE event, so the client gets Unterminated string JSON and the rest of the delta is lost.
  • /v1/messages stream: the delta is dropped, the client receives empty text.

Changes:

  • core/inference/http_stream.py: aiter_sse_lines(response), a drop-in for aiter_lines() that splits only at CR, LF and CRLF (a trailing CR is held until the next chunk), and closes its inner aiter_text() iterator itself. Used at every upstream aiter_lines() call site and by closing_response_lines. Objects without aiter_text keep their own aiter_lines().
  • The same split (SSE_LINE_END) in _SSEDecoder, in llama_cpp's _sse_event_payload (first-output detection missed a separator delta) and in the /v1/chat/completions and /v1/messages API monitors (an upstream error whose message held one was not flagged).

Tests: tests/test_http_stream_sse_lines.py (line-for-line parity with httpx aiter_lines() at every chunk boundary of a mixed CR / LF / CRLF stream, separators kept at every cut, stream closed, llama payload and monitor cases) plus end-to-end passthrough cases in test_openai_passthrough_respawn.py and test_anthropic_passthrough_respawn.py, which fail on main and pass here. The backend files with fakes of aiter_lines() (research runs, managed engines, subscription Responses client, tool confirm) pass unchanged; the failure set of every other file that failed in a loaded full run is identical on main and on this branch. Per event the new iterator costs about 1 us over httpx's.

The durable chat-run worker split each SSE event with str.splitlines(),
which also breaks at U+2028, U+2029 and U+0085. JSON leaves those
characters unescaped, and both model_dump_json and llama-server write
them raw, so a delta holding one was cut in two, failed json.loads and
was silently dropped from the stored reply. Split on LF only, as the
SSE line endings are already normalized to LF.
httpx aiter_lines() and str.splitlines() also break at U+2028, U+2029 and
U+0085, which llama-server, vLLM and pydantic leave unescaped in JSON. The
OpenAI passthrough forwarded the broken half as an invalid SSE event, the
Anthropic passthrough dropped the delta. Read every upstream stream through
one CR/LF-only line iterator and use the same split in the in-process
event parsers.
@danielhanchen danielhanchen changed the title Studio: keep chat-run deltas that contain U+2028, U+2029 or U+0085 Studio: keep stream deltas that contain U+2028, U+2029 or U+0085 Nov 15, 2026
@danielhanchen

Copy link
Copy Markdown
Member

@codex review

1 similar comment
@danielhanchen

Copy link
Copy Markdown
Member

@codex review

@chatgpt-codex-connector

chatgpt-codex-connector Bot commented Nov 15, 2026 •

Copy link
Copy Markdown
Codex Review Summary: ✅ Completed

This comment shows the latest Codex review activity on this pull request.

Review Status Commit Review trigger
📝 Code Review ✅ Completed 2026-11-15T09:16:13.731361Z 72efd34 Manual request
ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review" or "@codex security review".

Codex reacts with 👀 while any review is running, comments if it has suggestions, and reacts with 👍 once all reviews finish with no findings.

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: b81a0348b9

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

Comment on lines +30 to +31
if "\n" not in text and "\r" not in text:
continue

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

P2 Badge Process buffered CRs before skipping newline-free chunks

For CR-delimited upstream streams, if a chunk ends with \r and the next chunk contains no CR/LF, this fast path skips the delimiter already buffered in pending. I reproduced this with a live response yielding a complete data: JSON line ending in CR, followed by data: {: httpx.aiter_lines() delivers the first line when the second chunk arrives, but this replacement blocks until another delimiter or EOF. The relay's _aiter_llama_stream_items consequently can raise its first-token timeout despite having received a complete data line. Account for a held CR in this condition and test delivery while the response remains open; collecting through EOF hides the regression.

Useful? React with 👍 / 👎.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Fixed in 72efd34: a held CR now ends its line on the next chunk, matching httpx aiter_lines(); regression test added.

@danielhanchen

Copy link
Copy Markdown
Member

@codex review

1 similar comment
@danielhanchen

Copy link
Copy Markdown
Member

@codex review

@chatgpt-codex-connector

Copy link
Copy Markdown

Codex Review: Didn't find any major issues. Keep it up!

Reviewed commit: 72efd34476

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

danielhanchen added a commit that referenced this pull request Nov 15, 2026
… stock baseline (#17808)

## Summary


`tests/test_fp8_ragged_block_dequant.py::test_ragged_rows_match_block_reference[glm5_3]`
and `[ling3]` fail in `Repo tests (CPU, rest)` on every PR whose CI
installs unsloth-zoo at or after `898a6b0d`
(unslothai/unsloth-zoo#2515). Seen on #17725 and #17801:

```
tests/test_fp8_ragged_block_dequant.py:74: in test_ragged_rows_match_block_reference
    with pytest.raises(ValueError):
E   Failed: DID NOT RAISE ValueError
```

unsloth-zoo#2515 now patches `Fp8Dequantize._dequantize_one` on import
to dequantize partial edge blocks correctly. Its wrapper is marked
`_unsloth_partial_block_patched` and keeps the stock function in
`_unsloth_original`. This test's "unpatched" baseline only unwrapped
unsloth's own ragged-block wrapper (`_unsloth_ragged_blocks` /
`__wrapped__`). So the baseline was actually the zoo-fixed op, and the
expected stock `ValueError` never came. The zoo patch is correct; the
test's notion of "unpatched" was too narrow.

## Changes (test only)

- `_op_cls(patched=False)` unwraps both markers, so the baseline is
transformers' own op again.
- `test_unpatched_op_carries_no_unsloth_wrapper` pins that the baseline
has no unsloth or unsloth-zoo wrapper left. The `ValueError` assertion
only means something against the stock op.
- `test_ragged_padding_composes_over_the_zoo_partial_block_patch` covers
the real-world case, where both patches are live in one process. It
stacks unsloth's ragged padding on top of the zoo partial-block patch
and checks the block reference for 576x384, 576x2560 and stacked
2x256x200 experts. It skips when the zoo module is absent.

## Verification

| unsloth-zoo | before | after |
|---|---|---|
| `0c225609` (before #2515) | 6 passed | 7 passed, 1 skipped (no zoo
patch module) |
| `c8655b99` (main) | 2 failed, 4 passed | 8 passed |
| `c8655b99`, zoo patch applied explicitly first | 2 failed, 4 passed |
8 passed |
| main, with the new unwrap branch removed | | 3 failed (the two
reference cases and the new baseline guard) |
danielhanchen added a commit to unslothai/unsloth-zoo that referenced this pull request Nov 15, 2026
…solve new forward globals in the MoE prefix harness (#2517)

## Summary

Two zoo tests fail deterministically since #2515 and #2510 landed. They
show up in unsloth's `Core zoo` jobs (HF=4.57.6, default and latest) for
every unsloth PR whose CI uses zoo `c8655b99`, for example on the
staging runs of unslothai/unsloth#17725 and #17801:

```
tests/test_temporary_patches_imports.py::test_temporary_patches_submodule_list_is_complete
E   AssertionError: New temporary_patches submodule(s) on disk are NOT tested by this suite:
    ['unsloth_zoo.temporary_patches.fp8_partial_block_dequant']

tests/test_moe_triton_config_cache_dtype.py::test_dtype_change_never_reselects_inside_compiled_forward
E   NameError: name 'is_mxfp4_expert_param' is not defined
```

## Changes (tests only)

- `tests/test_temporary_patches_imports.py`: add
`unsloth_zoo.temporary_patches.fp8_partial_block_dequant` (new in #2515)
to `TEMPORARY_PATCHES_SUBMODULES`, so the import smoke test covers it.
- `tests/test_moe_triton_config_cache_dtype.py`: `_selection_prefix`
execs the start of the real `forward_triton_grouped_gemm`, up to its
first `with` block, against a hand-written stub namespace. #2510 made
that prefix call `is_mxfp4_expert_param` (to route packed MXFP4 stacks
away from the raw Triton GEMM), and the stub namespace had no such name.
The stubs are now layered over the real `moe_utils` globals, so a helper
the prefix starts calling resolves to the real one while every stubbed
name still wins.

## Verification

transformers 4.57.6 and 5.17.0, `tests/test_temporary_patches_imports.py
tests/test_moe_triton_config_cache_dtype.py`:

| | before | after |
|---|---|---|
| 4.57.6 | 2 failed | both pass (81 passed) |
| 5.17.0 | 2 failed | 81 passed, 5 skipped |

On 4.57.6 the five other tests in
`test_moe_triton_config_cache_dtype.py` error at setup in this local
environment. They do the same on zoo `0c225609`, before either PR, so
the cause is my environment, not this change.

Not covered here: the staging runs also show order-dependent Gemma3
failures (`test_gemma3_fa2_sliding_window_mask.py` "generic Gemma3
attention patch did not install", and
`test_gemma3_forced_float32_undo.py::test_bf16_then_fp16_still_installs_forced_set`).
Both pass in isolation, so I am keeping them separate.
@danielhanchen
danielhanchen merged commit 9015223 into unslothai:main Nov 15, 2026
danielhanchen added a commit to DimitriChrysafis/unsloth that referenced this pull request Nov 18, 2026
Resolve conflicts with main's SSE_LINE_END / aiter_sse_lines (unslothai#17725) and NPU error
explanation; fold the producer's EOF flush into its read loop.
danielhanchen added a commit to DimitriChrysafis/unsloth that referenced this pull request Nov 18, 2026
…in's decoder test

test_chat_generation_runs_sse_decoder.py (from unslothai#17725) imported the removed _SSEDecoder.
The regex split matches the per-char loop on 30k random split streams and is ~4x faster per chunk.
@jaeminbird
jaeminbird deleted the fix-chat-run-sse-unicode-separators branch November 20, 2026 03:48
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants