langgraph/libs/sdk-py/langgraph_sdk/_shared
Elior Nataf Lackritz 9791c686ee
fix(sdk-py): rejoin a run stream that closes before the run ends (#9554)
Fixes #9016

A run stream whose response ends cleanly before the run does, say on a
proxy timeout or a server restart, looked like a finished run:
`HttpClient.stream` and `SyncHttpClient.stream` reconnect to the
`Location` only when the connection fails, so the caller got a cut
answer and no error. Servers generally send no `end` event when a run
ends, so a clean close alone cannot tell the cases apart, and
reconnecting on every clean close would end every finished run in five
empty reconnects and an error.

**The rule.** After a clean close, when the stream's `Location` is a
run's stream (`<prefix>/threads/{thread_id}/runs/{run_id}/stream`, any
query) and no `end` or `error` event arrived across reconnects, the
client sends `GET <prefix>/threads/{thread_id}/runs/{run_id}` with the
reconnect headers, but `Accept: application/json` and no
`Last-Event-ID`.

- `pending` or `running`, or a 429, 5xx or transport error (the restart
that closed the stream can fail the check too): handled like a dropped
connection, with the same `Last-Event-ID`, attempt budget, backoff and
warnings, so repeated closes with no new events end in the usual
max-attempts error.
- Any other status, a 4xx other than 429, or a body that isn't a run:
the stream ends as it does today. The check never raises and uses the
client's normal timeout.

Thread streams, streams without a `Location`, and the threadless
`/runs/{run_id}/stream` `Location` older servers send for stateless runs
are unchanged. A stateless run whose `Location` names its temporary
thread is checked like any other run: it rejoins while the run is
pending or running, and once the run ends and the thread is deleted the
check gets a 404 and the stream ends. As after a drop, runs created
without `stream_resumable=True` reconnect without a cursor and warn.

**Cost.** One extra GET at the end of every run stream that ends without
an `end` or `error` event. It returns the whole run record, `kwargs`
(the run's input) included, so a run started with a large input
downloads it again. With a server that sends neither event, that is
every run stream, so the iterator finishes about one round trip later.

**Tests.** The new tests mock the transport, and `main` stops at the
first clean close. 38 of the 50 new cases fail on `main`; the 12
no-check cases pass on both.

Thanks to @Bruce-Yii for the report and root cause, and to
@anushamukka9, whose #9023 proposed reconnecting after a clean close;
this version asks the run first.

Paired JS change: https://github.com/langchain-ai/langgraphjs/pull/3185
2026-11-02 19:57:22 +00:00
..
__init__.py chore: Re-organize client files. (#6787) 2026-02-12 17:22:59 -05:00
types.py chore: Re-organize client files. (#6787) 2026-02-12 17:22:59 -05:00
utilities.py fix(sdk-py): rejoin a run stream that closes before the run ends (#9554) 2026-11-02 19:57:22 +00:00