[PR #605] [CLOSED] Open WebUI streaming: no long loop-guard silences, no early thinking close #623

Closed
opened 2026-10-29 21:27:36 +07:00 by bsakaguchi · 0 comments
Owner

📋 Pull Request Information

Original PR: https://github.com/mw00/project-maya/pull/605
Author: @mw00
Created: 10/28/2026
Status: ❌ Closed

Base: main ← Head: owui-stream


📝 Commits (1)

  • a5d730d Open WebUI streaming: bounded loop-guard hold, rarer thinking close, budget, MiniMax re-think

📊 Changes

7 files changed (+223 additions, -69 deletions)

View changed files

📝 docs/SETTINGS.md (+17 -12)
📝 serve/frontend.py (+15 -3)
📝 serve/loop_guard.py (+42 -22)
📝 serve/server.py (+25 -10)
📝 serve/test_loop_guard.py (+89 -21)
📝 serve/test_minimax_frontend.py (+25 -0)
📝 serve/test_server.py (+10 -1)

📄 Description

Problem

In Open WebUI, Maya streams could go silent for many seconds and then dump a burst of text. The "thinking" panel could also close while the model was still reasoning, with the rest of the reasoning shown as the answer. The causes:

  1. Loop-guard hold-back. The stream held back every token that could still turn out to be a loop's repeat. When the model copies a block it wrote earlier (an answer that repeats a draft from the thinking, a restated plan), the whole copy was held until it ended. That is tens of seconds of silence, then a burst.
  2. The loop guard closing the thinking. In the thinking, the second loop inserted a </think> the model never wrote. A loop was easy to hit: 3 copies of a block of 32+ tokens. Open WebUI then ends its thinking block early. A stop could also end the stream with finish_reason: stop and no answer at all.
  3. Thinking budget. A client's max_tokens cut the thinking budget. For example, 4096 gave about 3071 tokens, and then the thinking was closed.
  4. MiniMax. A second <mm:think> block after the first close was sent as answer text.

Fixes

  1. Bounded hold (serve/loop_guard.py, HOLD_MAX = 64). The stream is at most 64 tokens behind the model. A copied block streams as it is written. What was sent stays sent. When a loop is confirmed, only the repeats not sent yet are cut, and the short repetition penalty applies as before. After a loop, the detector starts its runs from zero, so the copies already on screen don't count toward the next loop.
  2. Closing the thinking is much rarer.
    • In the thinking, a loop needs more copies: 5 copies for a block under 32 tokens, 4 copies for a longer block, and at least 192 tokens in all. The answer keeps 4 / 3 copies and 96 tokens.
    • The first two thinking loops get the penalty. Only the third closes the thinking.
    • The guard never stops the output while it is still in the thinking.
    • An answer stops on its second loop, or on a fourth loop in all.
    • If a stop comes before any answer text (a model with no end-of-thinking token), the answer is one line saying the model kept repeating itself. It never ends empty.
  3. Thinking budget (serve/server.py, THINK_TINY_LIMIT = 1024). max_tokens (or the context left) lowers the budget only when it is 1024 tokens or fewer, for side requests such as a title. A larger max_tokens is the cap on the whole output and leaves the configured thinking_budget alone. The rule is documented in docs/SETTINGS.md and in the code.
  4. MiniMax re-think (serve/frontend.py). The rewriter turns <mm:think> into an internal marker. The parser drops the marker while already reasoning, and switches back to reasoning when it appears after an answer part or a tool call. Inside a tool-call value the tag stays text, as before. Open WebUI shows that as a second thinking block, since a reasoning_content delta after content opens a new reasoning item there.

docs/SETTINGS.md ("The loop guard" and the MAYA_GLM_THINK_RESERVE row) describes the new rules.

Evidence

Mock engine (20 ms a token, a </think> token, the engine continues from where the prompt got to). The stream is rendered with Open WebUI's rules (open_webui/utils/middleware.py, v0.11.4 and main):

scenario main (v1.1.35) this branch
thinking re-drafts a ~190-token plan 3 times, the answer repeats it longest silence 20.0 s; guard: penalty, then thinking closed at token 237; thinking panel ends at 25 s; the rest of the thinking and a literal </think> shown as the answer longest silence 1.36 s; no guard action; one thinking block with all of it (635 chars), then the answer
a real loop (one sentence 60 times in the thinking, penalty ignored) penalty, thinking closed, stopped (103 tokens) penalty, penalty, thinking closed, stopped (375 tokens): still stopped, and the answer still follows

Real model: GLM-5.3-Flash Maya-S24 on one V100 each, main and branch side by side, the same prompts, streamed with the same Open WebUI-rules client:

prompt main branch
the answer copies a ~250-word draft from the thinking, word for word 10.7 s of silence before the answer, then a burst longest gap 3.0 s (the end of the thinking); the answer streams as it is written
a long planning answer, max_tokens 10000 longest gap 1.0 s longest gap 0.43 s
a long planning answer, max_tokens 4096 thinking reached its budget (3071 tokens): the thinking closed early, then an answer the thinking keeps going until it reaches 4096, finish_reason: length (see the note below)

No loop-guard line in the real-model runs, as expected: copying a draft is not a loop.

Open WebUI itself: the owner's instance (192.168.0.81:3000, v0.11.4) needs a sign-in, and sign-up is off. So it was not used, and nothing was changed there. Jupiter's test ports were reachable from Neptune. The client above applies the same rules as the v0.11.4 source.

Tests: the full serve suite passes on Linux (470 tests, OK). New and updated tests:

  • test_loop_guard.py: the bounded hold, a copied block that streams on, two penalties before the close, never a stop while thinking, more copies needed in the thinking, a fresh count after a reset, the line instead of an empty answer.
  • test_server.py: larger limits keep the configured budget.
  • test_minimax_frontend.py: a second <mm:think> is reasoning.

Notes

  • Trade-off of fix 3: a client that sets a small max_tokens (for example 4096) and a model that thinks past it now ends with finish_reason: length and little or no answer, instead of a closed thinking at ~3K tokens. Open WebUI sends max_tokens only when it is set in the chat's advanced parameters.
  • Trade-off of fix 1: a real loop can show up to two of its copies on screen before it is cut, since only the last 64 tokens are held.
  • The MiniMax change is unit-tested only. A plain chat doesn't make MiniMax think twice, and the slot was used for the GLM comparison.
  • Open WebUI side (not changed here): Open WebUI sends background requests to the same model: title, tags, follow-ups, autocomplete and query generation. Maya runs one request at a time, so a chat that arrives while one of those runs waits for it, with only keep-alives. Setting a separate Task Model, or turning those tasks off (Admin Settings > Interface), avoids that wait.

How to test

python -m unittest serve.test_loop_guard serve.test_server serve.test_minimax_frontend -v

To check with a real client:

  1. Start a thinking model with this branch.
  2. Ask for a draft in the thinking that the answer must repeat word for word.
  3. Watch Open WebUI or any SSE client. The answer should stream as it is written, with no silence of several seconds.
  4. The server window should show no loop guard: ... thinking closed line.

🔄 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/mw00/project-maya/pull/605 **Author:** [@mw00](https://github.com/mw00) **Created:** 10/28/2026 **Status:** ❌ Closed **Base:** `main` ← **Head:** `owui-stream` --- ### 📝 Commits (1) - [`a5d730d`](https://github.com/mw00/project-maya/commit/a5d730d4976ed01374f4381beae4e0885c1a2c60) Open WebUI streaming: bounded loop-guard hold, rarer thinking close, budget, MiniMax re-think ### 📊 Changes **7 files changed** (+223 additions, -69 deletions) <details> <summary>View changed files</summary> 📝 `docs/SETTINGS.md` (+17 -12) 📝 `serve/frontend.py` (+15 -3) 📝 `serve/loop_guard.py` (+42 -22) 📝 `serve/server.py` (+25 -10) 📝 `serve/test_loop_guard.py` (+89 -21) 📝 `serve/test_minimax_frontend.py` (+25 -0) 📝 `serve/test_server.py` (+10 -1) </details> ### 📄 Description ## Problem In Open WebUI, Maya streams could go silent for many seconds and then dump a burst of text. The "thinking" panel could also close while the model was still reasoning, with the rest of the reasoning shown as the answer. The causes: 1. **Loop-guard hold-back.** The stream held back every token that could still turn out to be a loop's repeat. When the model copies a block it wrote earlier (an answer that repeats a draft from the thinking, a restated plan), the whole copy was held until it ended. That is tens of seconds of silence, then a burst. 2. **The loop guard closing the thinking.** In the thinking, the second loop inserted a `</think>` the model never wrote. A loop was easy to hit: 3 copies of a block of 32+ tokens. Open WebUI then ends its thinking block early. A stop could also end the stream with `finish_reason: stop` and no answer at all. 3. **Thinking budget.** A client's `max_tokens` cut the thinking budget. For example, 4096 gave about 3071 tokens, and then the thinking was closed. 4. **MiniMax.** A second `<mm:think>` block after the first close was sent as answer text. ## Fixes 1. **Bounded hold** (`serve/loop_guard.py`, `HOLD_MAX = 64`). The stream is at most 64 tokens behind the model. A copied block streams as it is written. What was sent stays sent. When a loop is confirmed, only the repeats not sent yet are cut, and the short repetition penalty applies as before. After a loop, the detector starts its runs from zero, so the copies already on screen don't count toward the next loop. 2. **Closing the thinking is much rarer.** - In the thinking, a loop needs more copies: 5 copies for a block under 32 tokens, 4 copies for a longer block, and at least 192 tokens in all. The answer keeps 4 / 3 copies and 96 tokens. - The first two thinking loops get the penalty. Only the third closes the thinking. - The guard never stops the output while it is still in the thinking. - An answer stops on its second loop, or on a fourth loop in all. - If a stop comes before any answer text (a model with no end-of-thinking token), the answer is one line saying the model kept repeating itself. It never ends empty. 3. **Thinking budget** (`serve/server.py`, `THINK_TINY_LIMIT = 1024`). `max_tokens` (or the context left) lowers the budget only when it is 1024 tokens or fewer, for side requests such as a title. A larger `max_tokens` is the cap on the whole output and leaves the configured `thinking_budget` alone. The rule is documented in `docs/SETTINGS.md` and in the code. 4. **MiniMax re-think** (`serve/frontend.py`). The rewriter turns `<mm:think>` into an internal marker. The parser drops the marker while already reasoning, and switches back to reasoning when it appears after an answer part or a tool call. Inside a tool-call value the tag stays text, as before. Open WebUI shows that as a second thinking block, since a `reasoning_content` delta after content opens a new reasoning item there. `docs/SETTINGS.md` ("The loop guard" and the `MAYA_GLM_THINK_RESERVE` row) describes the new rules. ## Evidence **Mock engine** (20 ms a token, a `</think>` token, the engine continues from where the prompt got to). The stream is rendered with Open WebUI's rules (`open_webui/utils/middleware.py`, v0.11.4 and main): | scenario | main (v1.1.35) | this branch | |---|---|---| | thinking re-drafts a ~190-token plan 3 times, the answer repeats it | longest silence **20.0 s**; guard: penalty, then `thinking closed` at token 237; thinking panel ends at 25 s; the rest of the thinking and a literal `</think>` shown as the answer | longest silence **1.36 s**; no guard action; one thinking block with all of it (635 chars), then the answer | | a real loop (one sentence 60 times in the thinking, penalty ignored) | penalty, thinking closed, stopped (103 tokens) | penalty, penalty, thinking closed, stopped (375 tokens): still stopped, and the answer still follows | **Real model**: GLM-5.3-Flash Maya-S24 on one V100 each, main and branch side by side, the same prompts, streamed with the same Open WebUI-rules client: | prompt | main | branch | |---|---|---| | the answer copies a ~250-word draft from the thinking, word for word | **10.7 s** of silence before the answer, then a burst | longest gap 3.0 s (the end of the thinking); the answer streams as it is written | | a long planning answer, `max_tokens` 10000 | longest gap 1.0 s | longest gap 0.43 s | | a long planning answer, `max_tokens` 4096 | `thinking reached its budget (3071 tokens)`: the thinking closed early, then an answer | the thinking keeps going until it reaches 4096, `finish_reason: length` (see the note below) | No loop-guard line in the real-model runs, as expected: copying a draft is not a loop. **Open WebUI itself**: the owner's instance (192.168.0.81:3000, v0.11.4) needs a sign-in, and sign-up is off. So it was not used, and nothing was changed there. Jupiter's test ports were reachable from Neptune. The client above applies the same rules as the v0.11.4 source. **Tests**: the full `serve` suite passes on Linux (470 tests, OK). New and updated tests: - `test_loop_guard.py`: the bounded hold, a copied block that streams on, two penalties before the close, never a stop while thinking, more copies needed in the thinking, a fresh count after a reset, the line instead of an empty answer. - `test_server.py`: larger limits keep the configured budget. - `test_minimax_frontend.py`: a second `<mm:think>` is reasoning. ## Notes - **Trade-off of fix 3:** a client that sets a small `max_tokens` (for example 4096) and a model that thinks past it now ends with `finish_reason: length` and little or no answer, instead of a closed thinking at ~3K tokens. Open WebUI sends `max_tokens` only when it is set in the chat's advanced parameters. - **Trade-off of fix 1:** a real loop can show up to two of its copies on screen before it is cut, since only the last 64 tokens are held. - The MiniMax change is unit-tested only. A plain chat doesn't make MiniMax think twice, and the slot was used for the GLM comparison. - **Open WebUI side (not changed here):** Open WebUI sends background requests to the same model: title, tags, follow-ups, autocomplete and query generation. Maya runs one request at a time, so a chat that arrives while one of those runs waits for it, with only keep-alives. Setting a separate Task Model, or turning those tasks off (Admin Settings > Interface), avoids that wait. ## How to test ``` python -m unittest serve.test_loop_guard serve.test_server serve.test_minimax_frontend -v ``` To check with a real client: 1. Start a thinking model with this branch. 2. Ask for a draft in the thinking that the answer must repeat word for word. 3. Watch Open WebUI or any SSE client. The answer should stream as it is written, with no silence of several seconds. 4. The server window should show no `loop guard: ... thinking closed` line. --- <sub>🔄 This issue represents a GitHub Pull Request. It cannot be merged through Gitea due to API limitations.</sub>
bsakaguchi 2026-10-29 21:27:36 +07:00
Sign in to join this conversation.
No labels
pull-request
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
mw00/project-maya#623
No description provided.