From a8843ab985978bbb8c355c4eaaaef36a5f835287 Mon Sep 17 00:00:00 2001 From: teknium1 <127238744+teknium1@users.noreply.github.com> Date: Sun, 13 Sep 2026 13:44:40 -0700 Subject: [PATCH] fix: treat a reasoning-only stream drop as a drop, not a clean stop The text-only drop guard in _finish_chat_stream required content_parts, so a stream that died while still emitting delta.reasoning (no finish_reason, no usage) fell through to the synthesized "stop". With the reasoning-only clean-stop promotion in finish_text_response that stamped "stop" turned the truncated thought into the final answer, where main entered the continuation ladder. Extend the guard with reasoning_parts so the drop yields the partial-stream stub and the ladder still runs; a real clean stop carries finish_reason="stop" and is unaffected. Review follow-up on #110227. --- agent/chat_completion_helpers.py | 8 +++-- .../test_partial_stream_finish_reason.py | 30 +++++++++++++++++++ 2 files changed, 35 insertions(+), 3 deletions(-) diff --git a/agent/chat_completion_helpers.py b/agent/chat_completion_helpers.py index 7d8e12c68f..6dda84e38a 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 7f3bc988f9..95a2f1cc8c 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) ──