Skip to content

DeepTutor: Lifelong Personalized Tutoring. https://deeptutor.info/.

fix(llm): raise a streamed provider error instead of answering with it (#1298)

Hyeonsang Chof3bbe85f9894
diff --git a/deeptutor/services/llm/factory.py b/deeptutor/services/llm/factory.py
index efa02b987..31cb30ed2 100644
--- a/deeptutor/services/llm/factory.py
+++ b/deeptutor/services/llm/factory.py
@@ -593,7 +593,7 @@ async def stream(
await queue.put(chunk)
async def _runner() -> None:
- nonlocal in_think_block
+ nonlocal saw_output, in_think_block
try:
response = await provider.chat_stream_with_retry(
messages=request_messages,
@@ -609,9 +609,12 @@ async def stream(
in_think_block = False
await queue.put("</think>")
# Some providers synthesize a final response only after the stream.
- # Do not replay reasoning_content as user-visible answer text.
+ # Do not replay reasoning_content as user-visible answer text, and
+ # never surface an error-shaped response's operator message as if
+ # the model had written it: that case is raised below instead.
if (
- not saw_content
+ response.finish_reason != "error"
+ and not saw_content
and response.content
and response.content != response.reasoning_content
):
diff --git a/tests/services/llm/test_factory_provider_exec.py b/tests/services/llm/test_factory_provider_exec.py
index 39e4c2b55..fadfdd865 100644
--- a/tests/services/llm/test_factory_provider_exec.py
+++ b/tests/services/llm/test_factory_provider_exec.py
@@ -7,6 +7,7 @@ from typing import Any
import pytest
from deeptutor.services.llm.config import LLMConfig
+from deeptutor.services.llm.exceptions import LLMAPIError
from deeptutor.services.llm.factory import complete, complete_with_config, stream
from deeptutor.services.llm.provider_core.base import LLMResponse
@@ -339,3 +340,50 @@ async def test_complete_passes_retry_delays(monkeypatch) -> None:
await complete("hello", max_retries=3, retry_delay=0.5, exponential_backoff=True)
assert provider.complete_kwargs["retry_delays"] == (0.5, 1.0, 2.0)
+
+
+async def test_stream_raises_when_provider_reports_error_before_any_output(monkeypatch) -> None:
+ """An error-shaped response must surface as an exception, not as answer text."""
+ cfg = _make_cfg()
+ provider = _FakeProvider(
+ stream_chunk="",
+ stream_response=LLMResponse(content="Error calling LLM: boom", finish_reason="error"),
+ )
+
+ monkeypatch.setattr("deeptutor.services.llm.factory.get_llm_config", lambda: cfg)
+ monkeypatch.setattr(
+ "deeptutor.services.llm.factory.get_runtime_provider",
+ lambda _config: provider,
+ )
+
+ chunks = []
+ with pytest.raises(LLMAPIError, match="boom"):
+ async for chunk in stream("hello"):
+ chunks.append(chunk)
+
+ assert chunks == []
+
+
+async def test_stream_keeps_delivered_output_when_provider_reports_error_late(
+ monkeypatch,
+) -> None:
+ """Text already streamed stays the answer; a late error neither replays nor crashes."""
+ cfg = _make_cfg()
+ provider = _FakeProvider(
+ stream_chunk="Hello",
+ stream_response=LLMResponse(content="Error calling LLM: boom", finish_reason="error"),
+ )
+
+ monkeypatch.setattr("deeptutor.services.llm.factory.get_llm_config", lambda: cfg)
+ monkeypatch.setattr(
+ "deeptutor.services.llm.factory.get_runtime_provider",
+ lambda _config: provider,
+ )
+
+ chunks = []
+ async for chunk in stream("hello"):
+ chunks.append(chunk)
+
+ assert chunks == ["Hello"]