Skip to content

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.

Report incomplete Studio streams in the CLI (#16868)

Max-Reisinger62c16a63d066
diff --git a/unsloth_cli/_inference.py b/unsloth_cli/_inference.py
index 0ef9772a1be..403f5f760b0 100644
--- a/unsloth_cli/_inference.py
+++ b/unsloth_cli/_inference.py
@@ -1164,6 +1164,7 @@ class HttpChatBackend:
# Accumulate SSE deltas into the full-text-so-far form; re-wrap delta.reasoning_content in think tags for visible_text.
text = ""
thinking = False
+ saw_finish = False
with resp:
for raw_line in resp:
line = raw_line.decode("utf-8", "replace").strip()
@@ -1171,7 +1172,7 @@ class HttpChatBackend:
continue
data = line[len("data:") :].strip()
if data == "[DONE]":
- break
+ return
try:
parsed = json.loads(data)
except ValueError:
@@ -1186,6 +1187,7 @@ class HttpChatBackend:
continue
# Read before the delta: the finish-reason chunk may carry no content.
if choice.get("finish_reason") is not None:
+ saw_finish = True
self.reply_hit_token_limit = choice["finish_reason"] == "length"
try:
reasoning = choice["delta"].get("reasoning_content")
@@ -1208,6 +1210,8 @@ class HttpChatBackend:
if "\ud800" <= visible[-1] <= "\udbff":
visible = visible[:-1]
yield visible.encode("utf-16", "surrogatepass").decode("utf-16", "replace")
+ if not saw_finish:
+ raise RuntimeError("Stream ended before the reply completed.")
return cumulative()
diff --git a/unsloth_cli/tests/test_inference_chat.py b/unsloth_cli/tests/test_inference_chat.py
index 96500b2294c..96d72437d6b 100644
--- a/unsloth_cli/tests/test_inference_chat.py
+++ b/unsloth_cli/tests/test_inference_chat.py
@@ -1445,6 +1445,50 @@ def test_http_backend_streams_cumulative_text(monkeypatch):
assert out == ["He", "Hello"]
+def test_inference_reports_an_incomplete_studio_stream(monkeypatch):
+ from unsloth_cli.commands import inference as infermod
+
+ backend = HttpChatBackend("http://localhost:8888", "token")
+ response = _FakeSSEResponse(
+ [b'data: {"choices":[{"delta":{"content":"partial"},"finish_reason":null}]}\n']
+ )
+ monkeypatch.setattr(backend, "_request", lambda *a, **k: response)
+ monkeypatch.setattr(infermod, "connect_studio_server", lambda *a, **k: backend)
+
+ result = CliRunner().invoke(_inference_app(), ["fake-model", "hello", "--verbose"])
+
+ assert result.exit_code == 1
+ assert "partial" in result.stdout
+ assert "Stream ended before the reply completed" in result.stderr
+
+
+def test_chat_drops_a_turn_with_an_incomplete_studio_stream(monkeypatch):
+ backend = HttpChatBackend("http://localhost:8888", "token")
+ requests = []
+
+ def fake_request(method, path, payload):
+ requests.append(payload["messages"])
+ if len(requests) == 1:
+ return _FakeSSEResponse([b'data: {"choices":[{"delta":{"content":"partial"}}]}\n'])
+ return _FakeSSEResponse(
+ [b'data: {"choices":[{"delta":{"content":"complete"}}]}\n', b"data: [DONE]\n"]
+ )
+
+ monkeypatch.setattr(backend, "_request", fake_request)
+ monkeypatch.setattr(chatmod, "connect_studio_server", lambda *a, **k: backend)
+ monkeypatch.setattr(chatmod, "resolve_model_config", lambda *a, **k: _FakeConfig())
+ monkeypatch.setattr(chatmod, "_compare_needs_second_model", lambda: False)
+
+ result = CliRunner().invoke(_chat_app(), ["fake-model"], input = "first\nsecond\n/exit\n")
+
+ assert result.exit_code == 0
+ assert "Stream ended before the reply completed" in result.stderr
+ assert requests == [
+ [{"role": "user", "content": "first"}],
+ [{"role": "user", "content": "second"}],
+ ]
+
+
def _http_stream_body(monkeypatch, max_new_tokens):
backend = HttpChatBackend("http://localhost:8888", "token")
bodies = []
@@ -1515,23 +1559,22 @@ def test_cli_leaves_unset_sampling_to_the_server(monkeypatch, command):
]
-def _http_finish(monkeypatch, finish_reason):
[email protected]("finish_reason", ["length", "stop"])
[email protected]("send_done", [True, False])
+def test_http_backend_reports_whether_a_reply_ran_out_of_budget(
+ monkeypatch, finish_reason, send_done
+):
backend = HttpChatBackend("http://localhost:8888", "token")
chunk = json.dumps({"choices": [{"delta": {"content": "hi"}, "finish_reason": finish_reason}]})
- monkeypatch.setattr(
- backend,
- "_request",
- lambda *a, **k: _FakeSSEResponse([f"data: {chunk}\n".encode(), b"data: [DONE]\n"]),
- )
- out = list(backend.stream([{"role": "user", "content": "hi"}], **_STREAM_KWARGS))
- return backend.reply_hit_token_limit, out
+ lines = [f"data: {chunk}\n".encode()]
+ if send_done:
+ lines.append(b"data: [DONE]\n")
+ monkeypatch.setattr(backend, "_request", lambda *a, **k: _FakeSSEResponse(lines))
+ out = list(backend.stream([{"role": "user", "content": "hi"}], **_STREAM_KWARGS))
-def test_http_backend_reports_whether_a_reply_ran_out_of_budget(monkeypatch):
- hit, out = _http_finish(monkeypatch, "length")
- assert hit is True
assert out == ["hi"]
- assert _http_finish(monkeypatch, "stop")[0] is False
+ assert backend.reply_hit_token_limit is (finish_reason == "length")
def _http_reasoning_reply(monkeypatch):