Local UI to run and train LLMs and diffusion models. Supports GGUF, MLX, Qwen3.8, DeepSeek-V4.1, MiniMax-H3, Gemma 4, FLUX and more.
Python agentaichatgptdeepseekfine-tuninggemmaimage-generationllamallmllmsopenaipythonqwenreinforcement-learningself-hostedstable-diffusiontext-to-speechttsuiunsloth
CLI: keep a reply's trailing '<' or '<th' once the stream has ended (#11893)
brekene5424c38ecb3
diff --git a/unsloth_cli/_inference.py b/unsloth_cli/_inference.pyindex 6e76b6c1291..5ab50a56883 100644--- a/unsloth_cli/_inference.py+++ b/unsloth_cli/_inference.py@@ -288,7 +288,11 @@ def quiet_if_nonzero_mlx_rank():os.close(saved_stderr_fd)-def visible_text(text: str, show_thinking: bool) -> str:+def visible_text(+ text: str,+ show_thinking: bool,+ final: bool = False,+) -> str:if show_thinking:return texttext = _THINK_BLOCK.sub("", text)@@ -296,6 +300,9 @@ def visible_text(text: str, show_thinking: bool) -> str:open_idx = text.find(_THINK_OPEN)if open_idx != -1:text = text[:open_idx]+ if final:+ # Ended stream: a trailing "<" or "<th" can no longer become <think>.+ return textmax_prefix = min(len(text), len(_THINK_OPEN) - 1)for size in range(max_prefix, 0, -1):if _THINK_OPEN.startswith(text[-size:]):@@ -317,7 +324,10 @@ def stream_to_stdout(stream, show_thinking: bool) -> str:if delta:sys.stdout.write(delta)sys.stdout.flush()- shown = rendered+ shown = rendered+ tail = visible_text(raw, show_thinking, final = True)[len(shown) :]+ if tail:+ sys.stdout.write(tail)sys.stdout.write("\n")sys.stdout.flush()return raw@@ -336,6 +346,8 @@ def stream_markdown(stream, show_thinking: bool, *, console) -> str:raw = chunkvisible = visible_text(chunk, show_thinking)live.update(Markdown(visible) if visible.strip() else Text(""))+ visible = visible_text(raw, show_thinking, final = True)+ live.update(Markdown(visible) if visible.strip() else Text(""))return raw@@ -344,7 +356,7 @@ def collect_stream(stream, show_thinking: bool) -> str:for chunk in stream:if isinstance(chunk, str):raw = chunk- return visible_text(raw, show_thinking)+ return visible_text(raw, show_thinking, final = True)def raise_on_streamed_error(stream):diff --git a/unsloth_cli/commands/chat.py b/unsloth_cli/commands/chat.pyindex f3b855ddc0e..93f497a1307 100644--- a/unsloth_cli/commands/chat.py+++ b/unsloth_cli/commands/chat.py@@ -504,7 +504,10 @@ def chat()messages.append(- {"role": "assistant", "content": visible_text(answer, show_thinking = False)}+ {+ "role": "assistant",+ "content": visible_text(answer, show_thinking = False, final = True),+ })finally:chat_backend.close()diff --git a/unsloth_cli/tests/test_inference_chat.py b/unsloth_cli/tests/test_inference_chat.pyindex 32d7e1a8026..8d6933ebf0d 100644--- a/unsloth_cli/tests/test_inference_chat.py+++ b/unsloth_cli/tests/test_inference_chat.py@@ -125,6 +125,28 @@ def test_visible_text_holds_partial_think_prefix():assert visible_text("2 < 3", show_thinking = False) == "2 < 3"+def test_finished_stream_keeps_a_trailing_think_prefix(capsys):+ from unsloth_cli._inference import stream_to_stdout++ def stream():+ yield "Use the less-than operator"+ yield "Use the less-than operator <"+ yield {"done": True}++ assert collect_stream(stream(), show_thinking = False) == "Use the less-than operator <"+ assert visible_text("x <th", show_thinking = False, final = True) == "x <th"+ assert visible_text("done.<think>more", show_thinking = False, final = True) == "done."++ raw = stream_to_stdout(stream(), show_thinking = False)+ assert raw == "Use the less-than operator <"+ assert capsys.readouterr().out == "Use the less-than operator <\n"++ # "<th" is printed once "<th<" rules it out, then "<think>" shrinks the render: no reprint.+ shrinking = iter(["x <th", "x <th<", "x <th<think>r", {"done": True}])+ stream_to_stdout(shrinking, show_thinking = False)+ assert capsys.readouterr().out == "x <th\n"++def _option(command_fn, name):return inspect.signature(command_fn).parameters[name].default