langgraph/libs/sdk-py/tests/streaming/_events.py
Elior Nataf Lackritz 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

328 lines
8.5 KiB
Python

"""Builders for protocol `Event` payloads used in tests.
Mirrors `libs/sdk/src/client/stream/test/event-builders.ts` from the JS SDK.
"""
from __future__ import annotations
from typing import Any
def _base(seq: int, method: str, namespace: list[str], data: Any) -> dict[str, Any]:
return {
"type": "event",
"method": method,
"params": {
"namespace": namespace,
"data": data,
},
"seq": seq,
"event_id": f"evt-{seq}",
}
def _normalize_lifecycle_data(data: dict[str, Any]) -> dict[str, Any]:
"""Map test-fixture shorthand to the wire shape `langgraph-api` emits.
The server emits the lifecycle status as `data.event` with values
`running` / `completed` / `failed` / `interrupted` (see
`api/langgraph_api/event_streaming/event_normalizers.py::to_lifecycle_status`).
The fixture historically accepted `phase=` and the legacy `"errored"`
value; translate them so tests exercise the real wire format
without touching every call site.
"""
normalized = dict(data)
if "phase" in normalized and "event" not in normalized:
normalized["event"] = normalized.pop("phase")
if normalized.get("event") == "errored":
normalized["event"] = "failed"
return normalized
def lifecycle_event(
seq: int = 0, namespace: list[str] | None = None, **data: Any
) -> dict[str, Any]:
payload = _normalize_lifecycle_data(data) if data else {"event": "started"}
return _base(seq, "lifecycle", namespace or [], payload)
def lifecycle_started_event(
seq: int = 0, namespace: list[str] | None = None
) -> dict[str, Any]:
"""Lifecycle event with `event="started"`."""
return _base(seq, "lifecycle", namespace or [], {"event": "started"})
def lifecycle_completed_event(
seq: int = 0, namespace: list[str] | None = None
) -> dict[str, Any]:
"""Lifecycle event with `event="completed"`."""
return _base(seq, "lifecycle", namespace or [], {"event": "completed"})
def lifecycle_errored_event(
seq: int = 0,
namespace: list[str] | None = None,
error: str = "run errored",
) -> dict[str, Any]:
"""Lifecycle event with `event="failed"` and an error message."""
return _base(seq, "lifecycle", namespace or [], {"event": "failed", "error": error})
def thread_stream_failed_frame() -> dict[str, Any]:
"""Unsolicited error frame the server sends before closing a failed stream."""
return {
"type": "error",
"id": None,
"error": "unknown_error",
"message": "Thread event stream failed.",
}
def values_event(
seq: int = 0, namespace: list[str] | None = None, **data: Any
) -> dict[str, Any]:
return _base(seq, "values", namespace or [], data or {"values": {}})
def updates_event(
seq: int = 0, namespace: list[str] | None = None, **data: Any
) -> dict[str, Any]:
return _base(seq, "updates", namespace or [], data or {})
def checkpoints_event(
seq: int = 0, namespace: list[str] | None = None, **data: Any
) -> dict[str, Any]:
return _base(seq, "checkpoints", namespace or [], data or {})
def custom_event(
seq: int = 0, name: str = "ext", namespace: list[str] | None = None, **data: Any
) -> dict[str, Any]:
payload = {"name": name, **data} if name else dict(data)
return _base(seq, "custom", namespace or [], payload)
def input_requested_event(
seq: int = 0, namespace: list[str] | None = None
) -> dict[str, Any]:
return _base(seq, "input.requested", namespace or [], {"interrupt_id": "i-1"})
def message_start_event(
seq: int = 0,
namespace: list[str] | None = None,
*,
message_id: str = "msg-1",
role: str = "ai",
run_id: str | None = None,
node: str = "agent",
) -> dict[str, Any]:
metadata: dict[str, Any] = {"langgraph_node": node}
if run_id is not None:
metadata["run_id"] = run_id
return _base(
seq,
"messages",
namespace or [],
{
"event": "message-start",
"id": message_id,
"role": role,
"metadata": metadata,
},
)
def message_text_delta_event(
seq: int = 0,
namespace: list[str] | None = None,
*,
text: str,
index: int = 0,
message_id: str | None = None,
) -> dict[str, Any]:
data: dict[str, Any] = {
"event": "content-block-delta",
"index": index,
"delta": {"type": "text-delta", "text": text},
}
if message_id is not None:
data["id"] = message_id
return _base(seq, "messages", namespace or [], data)
def message_text_finish_event(
seq: int = 0,
namespace: list[str] | None = None,
*,
text: str,
index: int = 0,
message_id: str | None = None,
) -> dict[str, Any]:
data: dict[str, Any] = {
"event": "content-block-finish",
"index": index,
"content": {"type": "text", "text": text},
}
if message_id is not None:
data["id"] = message_id
return _base(seq, "messages", namespace or [], data)
def message_finish_event(
seq: int = 0,
namespace: list[str] | None = None,
*,
input_tokens: int = 1,
output_tokens: int = 1,
message_id: str | None = None,
) -> dict[str, Any]:
data: dict[str, Any] = {
"event": "message-finish",
"usage": {
"input_tokens": input_tokens,
"output_tokens": output_tokens,
"total_tokens": input_tokens + output_tokens,
},
}
if message_id is not None:
data["id"] = message_id
return _base(seq, "messages", namespace or [], data)
def message_error_event(
seq: int = 0,
namespace: list[str] | None = None,
*,
message: str = "model failed",
code: str = "provider_error",
message_id: str | None = None,
) -> dict[str, Any]:
data: dict[str, Any] = {"event": "error", "message": message, "code": code}
if message_id is not None:
data["id"] = message_id
return _base(seq, "messages", namespace or [], data)
def tool_started_event(
seq: int = 0,
namespace: list[str] | None = None,
*,
tool_call_id: str = "call-1",
tool_name: str = "search",
input: Any = None,
) -> dict[str, Any]:
payload: dict[str, Any] = {
"event": "tool-started",
"tool_call_id": tool_call_id,
"tool_name": tool_name,
}
if input is not None:
payload["input"] = input
return _base(seq, "tools", namespace or [], payload)
def tool_output_delta_event(
seq: int = 0,
namespace: list[str] | None = None,
*,
tool_call_id: str = "call-1",
delta: str = "",
) -> dict[str, Any]:
return _base(
seq,
"tools",
namespace or [],
{
"event": "tool-output-delta",
"tool_call_id": tool_call_id,
"delta": delta,
},
)
def tool_finished_event(
seq: int = 0,
namespace: list[str] | None = None,
*,
tool_call_id: str = "call-1",
output: Any = None,
) -> dict[str, Any]:
return _base(
seq,
"tools",
namespace or [],
{
"event": "tool-finished",
"tool_call_id": tool_call_id,
"output": output,
},
)
def tool_error_event(
seq: int = 0,
namespace: list[str] | None = None,
*,
tool_call_id: str = "call-1",
message: str = "tool failed",
code: str = "tool_error",
) -> dict[str, Any]:
return _base(
seq,
"tools",
namespace or [],
{
"event": "tool-error",
"tool_call_id": tool_call_id,
"message": message,
"code": code,
},
)
def tasks_start_event(
seq: int = 0,
namespace: list[str] | None = None,
*,
task_id: str = "task-1",
name: str = "node",
input: Any = None,
) -> dict[str, Any]:
return _base(
seq,
"tasks",
namespace or [],
{
"id": task_id,
"name": name,
"input": input,
"triggers": [],
},
)
def tasks_result_event(
seq: int = 0,
namespace: list[str] | None = None,
*,
task_id: str = "task-1",
name: str = "node",
result: Any = None,
error: str | None = None,
interrupts: list[dict[str, Any]] | None = None,
) -> dict[str, Any]:
return _base(
seq,
"tasks",
namespace or [],
{
"id": task_id,
"name": name,
"result": result if result is not None else {},
"error": error,
"interrupts": interrupts or [],
},
)