From 5cbc645d09073ac0ba1f8d652b32944271c8666a Mon Sep 17 00:00:00 2001 From: beiyesi Date: Thu, 16 Jul 2026 22:59:11 +0800 Subject: [PATCH] fix: require terminal signal for streamed errors --- agent/chat_completion_helpers.py | 9 ++------- tests/run_agent/test_run_agent.py | 11 +++++++---- 2 files changed, 9 insertions(+), 11 deletions(-) diff --git a/agent/chat_completion_helpers.py b/agent/chat_completion_helpers.py index c4104e7769..ff9822c010 100644 --- a/agent/chat_completion_helpers.py +++ b/agent/chat_completion_helpers.py @@ -319,6 +319,8 @@ def _provider_stream_error_from_text( finish_reason_text = str(finish_reason or "").lower() has_error_finish = finish_reason_text in _PROVIDER_STREAM_ERROR_FINISH_REASONS + if not has_error_finish: + return None for event in _parse_provider_sse_events(text): event_name = str(event.get("event") or "").strip().lower() @@ -3882,13 +3884,6 @@ def interruptible_streaming_api_call(agent, api_kwargs: dict, *, on_first_delta= if pending_text_parts or _provider_stream_text_may_be_sse(delta.content): pending_text_parts.append(delta.content) pending_text = "".join(pending_text_parts) - provider_stream_error = _provider_stream_error_from_text( - pending_text, - None, - response=getattr(stream, "response", None), - ) - if provider_stream_error is not None: - raise provider_stream_error if _provider_stream_text_may_be_sse(pending_text): continue _flush_pending_stream_text() diff --git a/tests/run_agent/test_run_agent.py b/tests/run_agent/test_run_agent.py index 775d6504f8..dc11e0ed48 100644 --- a/tests/run_agent/test_run_agent.py +++ b/tests/run_agent/test_run_agent.py @@ -5668,9 +5668,12 @@ class TestStreamingApiCall: call.args[0] for call in agent.stream_delta_callback.call_args_list ] == ["id: product-42\n", "is ready"] - def test_literal_sse_error_example_with_stop_is_not_provider_error(self, agent): + def test_full_bailian_sse_error_example_with_stop_is_literal_text(self, agent): + error_text = _provider_sse_429_text(message="Example error payload.") + split_at = len(error_text) // 2 chunks = [ - _make_chunk(content='event: error\ndata: {"example": true}\n'), + _make_chunk(content=error_text[:split_at]), + _make_chunk(content=error_text[split_at:]), _make_chunk(finish_reason="stop"), ] agent.client.chat.completions.create.return_value = iter(chunks) @@ -5678,10 +5681,10 @@ class TestStreamingApiCall: resp = agent._interruptible_streaming_api_call({"messages": []}) - assert resp.choices[0].message.content == 'event: error\ndata: {"example": true}\n' + assert resp.choices[0].message.content == error_text assert [ call.args[0] for call in agent.stream_delta_callback.call_args_list - ] == ['event: error\ndata: {"example": true}\n'] + ] == [error_text[:split_at], error_text[split_at:]] def test_bare_sse_error_payload_with_stop_is_literal_text(self, agent): error_text = _provider_bare_sse_error_text(message="Example error payload.")