The model should stop at "\nUser:", but tokens arrive in pieces: "\nUs" might come in one chunk and "er:" in the next. If you print chunks as they arrive, you'll show "\nUs" before realising it was a stop sequence.
Implement StopFilter(stops) with:
feed(chunk) -> str: return the text that is safe to show now. Hold back any trailing text that could be the start of a stop sequence. When a stop sequence completes, return everything before it, set stopped = True, and ignore all later chunks (returning "").finish() -> str: at the end of the stream, release any held-back text (unless stopped).The stop sequence itself is never emitted. If several stop sequences match, cut at the earliest position.