Files
langgraph/libs/sdk-py/tests/streaming/_sync_fake_server.py
T
Elior Nataf LackritzandGitHub 9e6fc45c6f fix(sdk-py): surface thread stream errors and stop retrying a rejected stream (#9317)
## Summary

Thread streams in `langgraph_sdk` now report why they ended. An `id:
null` error frame becomes the error `thread.output` raises, the sync
client no longer blocks forever when the server closes the stream, and a
stream the server rejects with a 4xx ends at once instead of being
retried.

## Problem

- A server sends `{"type": "error", "id": null, ...}` just before it
closes a thread stream whose read failed. The SDK dropped the frame, so
`thread.output` raised a generic "lifecycle stream ended before terminal
event" (async) or blocked forever (sync: the lifecycle watcher returned
without settling the run).
- A 403 or 404 on the stream request was retried like a network error,
then reported as `RuntimeError("Lifecycle transport failed: ...")` with
no status. A server that rejects a WebSocket after accepting it closes
with 4000 plus the HTTP status (4403, 4404, 4422), since a browser can't
read a refused upgrade's status. The shared stream counted each such
reconnect as a success and reconnected forever.

## Fix

- The lifecycle watcher fails the run with the frame's error on a clean
close (both clients). The shared stream logs the frame instead of
dropping it: projections still end when the stream closes, as before,
and `thread.output` carries the error.
- A 4xx other than 408 and 429, as a status or as a 4000+status close
code, ends the stream without retries. `thread.output` now raises that
`httpx.HTTPStatusError` or `websockets.exceptions.ConnectionClosedError`
instead of the generic `RuntimeError`.
- For a minted `thread_id`, the lifecycle watcher starts after
`run.start` creates the thread. Without this, the 4xx stop would turn a
stream opened under custom auth for a not-yet-created thread from
retried into failed. The sync client starts it under a lock, since
`run.start` can be called from several threads.
- Drops the first of two `SyncStreamController._reconnect_shared_stream`
definitions. It was shadowed by the second and never ran.

Not in this PR: over WebSocket the server closes with 1011 after an
error frame, which is still retried, while over SSE the close is clean
and fails the stream. Reconnecting after a clean close, as
`langchain-ai/langgraphjs#2812` does for the JS SDK, is a separate
change.

## Test plan

- [x] The 10 new behavior tests (async and sync: error frame, clean
close, 404, 4404 close, reconnect on 404, minted `thread_id`) fail on
`main` and pass here; a race test checks that two concurrent `run.start`
calls start one watcher.
- [x] `make test` in `libs/sdk-py`: 541 passed. `make lint` clean,
`check_sdk_methods.py` passes.
- [x] `RemoteGraph` v3 tests in `libs/langgraph`
(`test_remote_graph_v3.py`,
`test_stream_events_v3_kwarg_forwarding.py`): 94 passed against this
SDK.
- [x] Against a local server with custom auth: a thread the caller can't
read fails at once with 404 (SSE) or 4404 (WebSocket), and a new thread
works with a 1 s pause before `run.start`, which fails on `main`.
2026-10-16 15:29:34 -04:00

133 lines
5.0 KiB
Python

"""Synchronous fake v3 protocol server for sync streaming tests."""
from __future__ import annotations
from collections.abc import Iterator
from dataclasses import dataclass
from typing import Any
import httpx
import orjson
@dataclass
class SyncStreamScript:
events: list[dict[str, Any]]
fail_after: int | None = None
status: int = 200
class _SseByteStream(httpx.SyncByteStream):
def __init__(self, script: SyncStreamScript) -> None:
self._script = script
def __iter__(self) -> Iterator[bytes]:
for index, event in enumerate(self._script.events, start=1):
payload = orjson.dumps(event).decode()
yield f"id: {event.get('event_id', '')}\n".encode()
yield f"event: message\ndata: {payload}\n\n".encode()
if self._script.fail_after is not None and index >= self._script.fail_after:
raise httpx.ReadError("scripted sync stream failure")
class SyncFakeServer:
"""Synchronous fake for `/commands`, `/stream/events`, and `/state`."""
def __init__(self) -> None:
self.received_commands: list[dict[str, Any]] = []
self.stream_request_bodies: list[dict[str, Any]] = []
self.command_request_headers: list[dict[str, str]] = []
self.stream_request_headers_list: list[dict[str, str]] = []
self.state_request_headers: list[dict[str, str]] = []
self.state_request_count = 0
self.scripted_events: list[dict[str, Any]] = []
self.state: dict[str, Any] = {}
self.transport = httpx.MockTransport(self._handle)
self._stream_scripts: list[SyncStreamScript] = []
self._command_response: dict[str, Any] | None = None
self.graph_response: dict[str, Any] = {
"nodes": [{"id": "agent", "type": "runnable", "data": {"name": "agent"}}],
"edges": [],
}
self.graph_request_params: list[dict[str, str]] = []
self.graph_request_headers: list[dict[str, str]] = []
def script(
self,
events: list[dict[str, Any]],
*,
fail_after: int | None = None,
) -> None:
self.scripted_events = list(events)
self._stream_scripts = [
SyncStreamScript(events=list(events), fail_after=fail_after)
]
def script_sequence(self, scripts: list[SyncStreamScript]) -> None:
self._stream_scripts = list(scripts)
self.scripted_events = []
def set_graph(self, graph: dict[str, Any]) -> None:
self.graph_response = dict(graph)
def script_command_response(self, response: dict[str, Any]) -> None:
self._command_response = dict(response)
def set_state(
self,
values: dict[str, Any],
next: list[Any] | None = None,
metadata: dict[str, Any] | None = None,
) -> None:
self.state = {
"values": values,
"next": next if next is not None else [],
"tasks": [],
"metadata": metadata if metadata is not None else {},
"checkpoint": None,
"created_at": None,
}
def _handle(self, request: httpx.Request) -> httpx.Response:
path = request.url.path
if path.endswith("/commands"):
body = orjson.loads(request.content)
self.received_commands.append(body)
self.command_request_headers.append(dict(request.headers))
if self._command_response is not None:
response = dict(self._command_response)
response["id"] = body.get("id")
return httpx.Response(200, json=response)
return httpx.Response(
200,
json={
"type": "success",
"id": body.get("id"),
"result": {"run_id": "run-1"},
},
)
if path.endswith("/stream/events"):
self.stream_request_bodies.append(orjson.loads(request.content))
self.stream_request_headers_list.append(dict(request.headers))
script = (
self._stream_scripts.pop(0)
if self._stream_scripts
else SyncStreamScript(events=list(self.scripted_events))
)
if script.status != 200:
return httpx.Response(script.status, json={"detail": "scripted"})
return httpx.Response(
200,
headers={"content-type": "text/event-stream"},
stream=_SseByteStream(script),
)
if path.endswith("/graph") and "/assistants/" in path:
self.graph_request_params.append(dict(request.url.params))
self.graph_request_headers.append(dict(request.headers))
return httpx.Response(200, json=self.graph_response)
if path.endswith("/state"):
self.state_request_count += 1
self.state_request_headers.append(dict(request.headers))
return httpx.Response(200, json=self.state)
return httpx.Response(404, json={"error": f"unexpected path: {path}"})