Skip to content

The agent that grows with you

fix: treat a reasoning-only stream drop as a drop, not a clean stop

teknium1a8843ab98597
diff --git a/agent/chat_completion_helpers.py b/agent/chat_completion_helpers.py
index 7d8e12c68fb..6dda84e38ab 100644
--- a/agent/chat_completion_helpers.py
+++ b/agent/chat_completion_helpers.py
@@ -2948,9 +2948,11 @@ class _StreamingCall(StreamingWaitMonitor):
_dropped_names)
return _build_partial_stream_stub(
role, full_content, full_reasoning, model_name, usage_obj, dropped_tool_names=_dropped_names or None)
- if finish_reason is None and content_parts and not tool_calls_acc and usage_obj is None:
- # Text-only drop: otherwise the partial text is stamped "stop" and the next step is
- # lost. A usage object proves the provider finished (include_usage's final chunk).
+ if finish_reason is None and (content_parts or reasoning_parts) and not tool_calls_acc and usage_obj is None:
+ # Text-only (or reasoning-only) drop: otherwise the partial text is stamped "stop"
+ # and the next step is lost — for reasoning-only, the clean-stop promotion in
+ # finish_text_response would then surface a truncated thought as the answer.
+ # A usage object proves the provider finished (include_usage's final chunk).
logger.warning(
"Stream ended with no finish_reason after delivering text with no tool calls; treating as a mid-stream drop.")
return _build_partial_stream_stub(role, full_content, full_reasoning, model_name, usage_obj)
diff --git a/tests/agent/test_partial_stream_finish_reason.py b/tests/agent/test_partial_stream_finish_reason.py
index 7f3bc988f9c..95a2f1cc8c7 100644
--- a/tests/agent/test_partial_stream_finish_reason.py
+++ b/tests/agent/test_partial_stream_finish_reason.py
@@ -975,6 +975,36 @@ class TestStreamIncludeUsageFinalChunk:
assert response.choices[0].finish_reason == FINISH_REASON_LENGTH
assert response.choices[0].message.content == "Partial text before drop"
+ @patch("run_agent.AIAgent._create_request_openai_client")
+ @patch("run_agent.AIAgent._close_request_openai_client")
+ def test_reasoning_only_abrupt_drop_without_usage_still_returns_stub(
+ self, _mock_close, mock_create, monkeypatch,
+ ):
+ """A stream severed while still reasoning (only ``delta.reasoning`` frames,
+ no finish_reason, no usage) is a drop, not a clean stop: stamping "stop"
+ would let the reasoning-only clean-stop promotion surface the truncated
+ thought as the final answer instead of entering the continuation ladder."""
+ def _dropped_stream():
+ for text in ("Let me think about", " the question carefully, first"):
+ chunk = _make_stream_chunk()
+ chunk.choices[0].delta.reasoning = text
+ yield chunk
+
+ mock_client = MagicMock()
+ mock_client.chat.completions.create.side_effect = lambda *a, **kw: _dropped_stream()
+ mock_create.return_value = mock_client
+
+ agent = _make_agent()
+ monkeypatch.setenv("HERMES_STREAM_RETRIES", "0")
+ response = agent._interruptible_streaming_api_call({})
+
+ assert response.id == PARTIAL_STREAM_STUB_ID
+ assert response.choices[0].finish_reason == FINISH_REASON_LENGTH
+ assert response.choices[0].message.content is None
+ assert response.choices[0].message.reasoning_content == (
+ "Let me think about the question carefully, first"
+ )
+
# ── Merged-finish content chunk swallowed by the SSE-echo guard (#94614) ──