fix: require terminal signal for streamed errors
This commit is contained in:
@@ -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()
|
||||
|
||||
@@ -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.")
|
||||
|
||||
Reference in New Issue
Block a user