Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
74 changes: 56 additions & 18 deletions dify-agent/src/dify_agent/client/_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -667,6 +667,12 @@ async def stream_events(
response, but after observing a terminal event it will not reconnect when that
response ends normally or raises a reconnectable transport error.

``should_stop`` and ``timeout_seconds`` are checked before connecting,
before reconnect sleeps, on each received chunk, and immediately before
yielding each decoded or flushed event. Triggering either closes the
active response without reconnecting and raises ``DifyAgentStreamError``
or ``DifyAgentTimeoutError``.

If you stop consuming early, close the returned async generator with
``await events.aclose()`` or ``contextlib.aclosing`` to release the active
response promptly without reconnecting. HTTPX errors during caller-driven
Expand Down Expand Up @@ -739,7 +745,7 @@ def stream_events_sync(
timeout_seconds: float | None = None,
should_stop: Callable[[], bool] | None = None,
) -> Iterator[RunEvent]:
"""Synchronous variant of ``stream_events`` with the same reconnect rules."""
"""Synchronous variant of ``stream_events`` with the same reconnect and stop rules."""
_validate_stream_options(max_reconnects, reconnect_delay_seconds, timeout_seconds)
cursor = after or "0-0"
reconnect_attempts = 0
Expand Down Expand Up @@ -843,9 +849,13 @@ async def _stream_events_once(
) -> AsyncGenerator[RunEvent]:
"""Open one SSE connection and yield events until it ends or fails.

Caller interruption must not make response cleanup failures reconnectable.
Stop conditions are checked per chunk (for comment/heartbeat chunks that
emit no events) and immediately before each decoded or flushed yield.
Caller interruption and already-raised stop/stream errors must not make
response cleanup failures reconnectable.
"""
caller_interrupted = False
stop_error: DifyAgentStreamError | DifyAgentTimeoutError | None = None
try:
async with self._get_async_http_client().stream(
"GET",
Expand All @@ -861,28 +871,39 @@ async def _stream_events_once(
decoder = _SSEDecoder()
line_decoder = _SSELineDecoder()
async for text in response.aiter_text():
# Check per chunk so comment/heartbeat chunks still observe cancellation and deadlines.
_raise_if_stream_stopped(run_id, deadline=deadline, should_stop=should_stop)
for line in line_decoder.decode(text):
event = decoder.feed_line(line)
if event is not None:
# Re-check immediately before yielding so buffered events stop promptly.
_raise_if_stream_stopped(run_id, deadline=deadline, should_stop=should_stop)
yield event
for line in line_decoder.flush():
event = decoder.feed_line(line)
if event is not None:
_raise_if_stream_stopped(run_id, deadline=deadline, should_stop=should_stop)
yield event
except (GeneratorExit, asyncio.CancelledError):
caller_interrupted = True
raise
except (DifyAgentStreamError, DifyAgentTimeoutError) as exc:
stop_error = exc
raise
except DifyAgentHTTPError:
raise
except DifyAgentStreamError:
raise
except httpx.TimeoutException as exc:
if stop_error is not None:
raise stop_error from stop_error.__cause__
timeout_error = DifyAgentTimeoutError("SSE stream timed out")
if caller_interrupted:
raise timeout_error from exc
raise _ReconnectableStreamError(timeout_error) from exc
except (httpx.TransportError, httpx.StreamError) as exc:
if stop_error is not None:
raise stop_error from stop_error.__cause__
stream_error = DifyAgentStreamError(f"SSE stream failed: {exc}")
if caller_interrupted:
raise stream_error from exc
Expand All @@ -896,7 +917,14 @@ def _stream_events_once_sync(
deadline: float | None,
should_stop: Callable[[], bool] | None,
) -> Iterator[RunEvent]:
"""Open one synchronous SSE connection and yield events until it ends or fails."""
"""Open one synchronous SSE connection and yield events until it ends or fails.

Stop conditions are checked per chunk (for comment/heartbeat chunks that
emit no events) and immediately before each decoded or flushed yield,
and already-raised stop/stream errors take precedence over response
cleanup failures.
"""
stop_error: DifyAgentStreamError | DifyAgentTimeoutError | None = None
try:
with self._get_sync_http_client().stream(
"GET",
Expand All @@ -905,30 +933,40 @@ def _stream_events_once_sync(
headers=self._merged_headers(),
timeout=self._stream_timeout,
) as response:
if response.status_code >= 400:
_ = response.read()
_raise_for_stream_status(response)
decoder = _SSEDecoder()
line_decoder = _SSELineDecoder()
for text in response.iter_text():
_raise_if_stream_stopped(run_id, deadline=deadline, should_stop=should_stop)
for line in line_decoder.decode(text):
try:
if response.status_code >= 400:
_ = response.read()
_raise_for_stream_status(response)
decoder = _SSEDecoder()
line_decoder = _SSELineDecoder()
for text in response.iter_text():
# Check per chunk so comment/heartbeat chunks still observe cancellation and deadlines.
_raise_if_stream_stopped(run_id, deadline=deadline, should_stop=should_stop)
for line in line_decoder.decode(text):
event = decoder.feed_line(line)
if event is not None:
# Re-check immediately before yielding so buffered events stop promptly.
_raise_if_stream_stopped(run_id, deadline=deadline, should_stop=should_stop)
yield event
for line in line_decoder.flush():
event = decoder.feed_line(line)
if event is not None:
_raise_if_stream_stopped(run_id, deadline=deadline, should_stop=should_stop)
yield event
for line in line_decoder.flush():
event = decoder.feed_line(line)
if event is not None:
yield event
except (DifyAgentStreamError, DifyAgentTimeoutError) as exc:
stop_error = exc
raise
except DifyAgentHTTPError:
raise
except DifyAgentStreamError:
raise
except httpx.TimeoutException as exc:
if stop_error is not None:
raise stop_error from stop_error.__cause__
raise _ReconnectableStreamError(DifyAgentTimeoutError("SSE stream timed out")) from exc
except httpx.TransportError as exc:
raise _ReconnectableStreamError(DifyAgentStreamError(f"SSE stream failed: {exc}")) from exc
except httpx.StreamError as exc:
except (httpx.TransportError, httpx.StreamError) as exc:
if stop_error is not None:
raise stop_error from stop_error.__cause__
raise _ReconnectableStreamError(DifyAgentStreamError(f"SSE stream failed: {exc}")) from exc

def _get_sync_http_client(self) -> httpx.Client:
Expand Down
Loading
Loading