@langchain/langgraph-api 1.5.5 → 1.5.7

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/api/runs.mjs CHANGED
@@ -12,6 +12,36 @@ import { getDisconnectAbortSignal, jsonExtra, waitKeepAlive, } from "../utils/ho
12
12
  import { applyAuthToRunConfig, applyRequestHeadersToRunConfig, } from "../utils/run-auth.mjs";
13
13
  import { serialiseAsDict } from "../utils/serde.mjs";
14
14
  const api = new Hono();
15
+ // JS SDKs before 1.12 send several stream modes as one JSON array.
16
+ const parseStreamMode = (value) => {
17
+ if (!value.startsWith("["))
18
+ return value ? [value] : [];
19
+ try {
20
+ return JSON.parse(value);
21
+ }
22
+ catch {
23
+ return [value];
24
+ }
25
+ };
26
+ const JoinStreamQuery = z.object({
27
+ cancel_on_disconnect: schemas.coercedBoolean.optional(),
28
+ stream_mode: z
29
+ .union([z.string(), z.array(z.string())])
30
+ .optional()
31
+ .transform((value) => [value ?? []].flat().flatMap(parseStreamMode)),
32
+ });
33
+ const matchesStreamMode = (event, streamMode) => {
34
+ // A run's error is its outcome, not a mode, so no filter drops it.
35
+ if (streamMode.length === 0 || event === "error")
36
+ return true;
37
+ const mode = event.split("|")[0];
38
+ if (streamMode.includes(mode))
39
+ return true;
40
+ // The messages modes stream `messages` (messages-tuple) and `messages/*`
41
+ // (messages) events; as on the Agent Server, either mode keeps both.
42
+ return (mode.startsWith("messages") &&
43
+ (streamMode.includes("messages") || streamMode.includes("messages-tuple")));
44
+ };
15
45
  const createValidRun = async (threadId, payload, kwargs) => {
16
46
  const { assistant_id: assistantId, ...run } = payload;
17
47
  const { auth, headers } = kwargs ?? {};
@@ -23,7 +53,7 @@ const createValidRun = async (threadId, payload, kwargs) => {
23
53
  : [];
24
54
  if (streamMode.length === 0)
25
55
  streamMode.push("values");
26
- const multitaskStrategy = payload.multitask_strategy ?? "reject";
56
+ const multitaskStrategy = payload.multitask_strategy ?? "enqueue";
27
57
  const preventInsertInInflight = multitaskStrategy === "reject";
28
58
  const config = { ...run.config };
29
59
  if (run.checkpoint_id) {
@@ -143,7 +173,7 @@ api.post("/runs/stream", zValidator("json", schemas.RunCreate), async (c) => {
143
173
  });
144
174
  });
145
175
  // TODO: port to Python API
146
- api.get("/runs/:run_id/stream", zValidator("param", z.object({ run_id: z.string().uuid() })), zValidator("query", z.object({ cancel_on_disconnect: schemas.coercedBoolean.optional() })), async (c) => {
176
+ api.get("/runs/:run_id/stream", zValidator("param", z.object({ run_id: z.string().uuid() })), zValidator("query", JoinStreamQuery), async (c) => {
147
177
  const { run_id } = c.req.valid("param");
148
178
  const query = c.req.valid("query");
149
179
  const lastEventId = c.req.header("Last-Event-ID") || undefined;
@@ -159,6 +189,8 @@ api.get("/runs/:run_id/stream", zValidator("param", z.object({ run_id: z.string(
159
189
  lastEventId,
160
190
  ignore404: true,
161
191
  }, c.var.auth)) {
192
+ if (!matchesStreamMode(event, query.stream_mode))
193
+ continue;
162
194
  await stream.writeSSE({ id, data: serialiseAsDict(data), event });
163
195
  }
164
196
  }
@@ -281,10 +313,10 @@ api.get("/threads/:thread_id/runs/:run_id/join", zValidator("param", z.object({
281
313
  const { thread_id, run_id } = c.req.valid("param");
282
314
  return jsonExtra(c, await runs().join(run_id, thread_id, c.var.auth));
283
315
  });
284
- api.get("/threads/:thread_id/runs/:run_id/stream", zValidator("param", z.object({ thread_id: z.string().uuid(), run_id: z.string().uuid() })), zValidator("query", z.object({ cancel_on_disconnect: schemas.coercedBoolean.optional() })), async (c) => {
316
+ api.get("/threads/:thread_id/runs/:run_id/stream", zValidator("param", z.object({ thread_id: z.string().uuid(), run_id: z.string().uuid() })), zValidator("query", JoinStreamQuery), async (c) => {
285
317
  // Stream Run Http
286
318
  const { thread_id, run_id } = c.req.valid("param");
287
- const { cancel_on_disconnect } = c.req.valid("query");
319
+ const { cancel_on_disconnect, stream_mode } = c.req.valid("query");
288
320
  const lastEventId = c.req.header("Last-Event-ID") || undefined;
289
321
  return streamSSE(c, async (stream) => {
290
322
  const signal = cancel_on_disconnect
@@ -295,6 +327,8 @@ api.get("/threads/:thread_id/runs/:run_id/stream", zValidator("param", z.object(
295
327
  cancelOnDisconnect: signal != null,
296
328
  lastEventId,
297
329
  }, c.var.auth)) {
330
+ if (!matchesStreamMode(event, stream_mode))
331
+ continue;
298
332
  await stream.writeSSE({ id, data: serialiseAsDict(data), event });
299
333
  }
300
334
  });
@@ -307,8 +341,15 @@ api.post("/threads/:thread_id/runs/:run_id/cancel", zValidator("param", z.object
307
341
  const { thread_id, run_id } = c.req.valid("param");
308
342
  const { wait, action } = c.req.valid("query");
309
343
  await runs().cancel(thread_id, [run_id], { action }, c.var.auth);
310
- if (wait)
311
- await runs().join(run_id, thread_id, c.var.auth);
344
+ if (wait) {
345
+ // A cancelled pending stateless run is deleted with its thread.
346
+ await runs()
347
+ .join(run_id, thread_id, c.var.auth)
348
+ .catch((error) => {
349
+ if (!(error instanceof HTTPException) || error.status !== 404)
350
+ throw error;
351
+ });
352
+ }
312
353
  return c.body(null, wait ? 204 : 202);
313
354
  });
314
355
  export default api;
@@ -27,7 +27,10 @@ export async function getStaticGraphSchema(input, options) {
27
27
  clearTimeout(timeoutId);
28
28
  resolve(result);
29
29
  });
30
- worker.on("error", reject);
30
+ worker.on("error", (error) => {
31
+ clearTimeout(timeoutId);
32
+ reject(error);
33
+ });
31
34
  worker.postMessage(specs);
32
35
  });
33
36
  }
package/dist/queue.mjs CHANGED
@@ -1,3 +1,4 @@
1
+ import { HTTPException } from "hono/http-exception";
1
2
  import { streamState, } from "./stream.mjs";
2
3
  import { logError, logger } from "./logging.mjs";
3
4
  import { serializeError } from "./utils/serde.mjs";
@@ -8,7 +9,11 @@ const sleep = (ms) => new Promise((resolve) => setTimeout(resolve, ms));
8
9
  export const queue = async (ops) => {
9
10
  while (true) {
10
11
  for await (const { run, attempt, signal } of ops.runs.next()) {
11
- await worker(ops, run, attempt, signal);
12
+ // Nothing restarts this loop, so one run's failure must not end it.
13
+ await worker(ops, run, attempt, signal).catch((error) => logError(error, {
14
+ prefix: "Background worker failed",
15
+ context: { run_id: run.run_id },
16
+ }));
12
17
  }
13
18
  // TODO: this is very suboptimal, we should implement subscription to the run
14
19
  await sleep(1000 * Math.random());
@@ -41,7 +46,25 @@ const worker = async (ops, run, attempt, signal) => {
41
46
  ...result,
42
47
  };
43
48
  };
44
- const isInterrupted = () => signal.aborted && signal.reason === "interrupt";
49
+ const isCancelled = () => signal.aborted &&
50
+ (signal.reason === "interrupt" || signal.reason === "rollback");
51
+ // The run, or its thread, can be deleted while it runs: a final write that
52
+ // fails because its record is gone has nothing left to update.
53
+ const unlessGone = async (write, isGone) => {
54
+ try {
55
+ await write;
56
+ }
57
+ catch (error) {
58
+ if (!(await isGone()))
59
+ throw error;
60
+ }
61
+ };
62
+ const runGone = async () => (await ops.runs.get(run.run_id, run.thread_id, undefined)) == null;
63
+ const threadGone = () => ops.threads.get(run.thread_id, undefined).then(() => false, (error) => {
64
+ if (error instanceof HTTPException && error.status === 404)
65
+ return true;
66
+ throw error;
67
+ });
45
68
  try {
46
69
  if (attempt > MAX_RETRY_ATTEMPTS) {
47
70
  throw new Error(`Run ${run.run_id} exceeded max attempts`);
@@ -66,7 +89,7 @@ const worker = async (ops, run, attempt, signal) => {
66
89
  }
67
90
  }
68
91
  catch (error) {
69
- if (!isInterrupted()) {
92
+ if (!isCancelled()) {
70
93
  await ops.runs.stream.publish({
71
94
  runId,
72
95
  resumable,
@@ -86,14 +109,20 @@ const worker = async (ops, run, attempt, signal) => {
86
109
  run_exec_ms: endedAt.valueOf() - startedAt.valueOf(),
87
110
  });
88
111
  status = "success";
89
- await ops.runs.setStatus(run.run_id, status);
112
+ await unlessGone(ops.runs.setStatus(run.run_id, status), runGone);
90
113
  }
91
114
  catch (error) {
92
115
  endedAt = new Date();
93
- if (isInterrupted()) {
116
+ if (isCancelled()) {
94
117
  checkpoint = undefined;
95
- status = "interrupted";
96
- await ops.runs.setStatus(run.run_id, status);
118
+ if (signal.reason === "rollback") {
119
+ status = "rollback";
120
+ await unlessGone(ops.runs.delete(run.run_id, run.thread_id, undefined), runGone);
121
+ }
122
+ else {
123
+ status = "interrupted";
124
+ await unlessGone(ops.runs.setStatus(run.run_id, status), runGone);
125
+ }
97
126
  }
98
127
  else {
99
128
  if (error instanceof Error)
@@ -110,15 +139,15 @@ const worker = async (ops, run, attempt, signal) => {
110
139
  },
111
140
  });
112
141
  status = "error";
113
- await ops.runs.setStatus(run.run_id, status);
142
+ await unlessGone(ops.runs.setStatus(run.run_id, status), runGone);
114
143
  }
115
144
  }
116
145
  finally {
117
146
  if (temporary) {
118
- await ops.threads.delete(run.thread_id, undefined);
147
+ await unlessGone(ops.threads.delete(run.thread_id, undefined), threadGone);
119
148
  }
120
149
  else {
121
- await ops.threads.setStatus(run.thread_id, { checkpoint, exception });
150
+ await unlessGone(ops.threads.setStatus(run.thread_id, { checkpoint, exception }), threadGone);
122
151
  }
123
152
  if (webhook) {
124
153
  await callWebhook({
@@ -131,6 +131,7 @@ export declare class FileSystemThreads implements ThreadsRepo {
131
131
  export declare class FileSystemRuns implements RunsRepo {
132
132
  private readonly conn;
133
133
  private readonly threads;
134
+ private requeuedPreviousProcessRuns;
134
135
  readonly stream: RunsStreamRepo;
135
136
  constructor(conn: FileSystemPersistence<Store>);
136
137
  next(): AsyncGenerator<{
@@ -870,6 +870,7 @@ export class FileSystemThreads {
870
870
  export class FileSystemRuns {
871
871
  conn;
872
872
  threads;
873
+ requeuedPreviousProcessRuns = false;
873
874
  stream;
874
875
  constructor(conn) {
875
876
  this.conn = conn;
@@ -877,6 +878,24 @@ export class FileSystemRuns {
877
878
  this.stream = new FileSystemRuns.Stream(conn, this);
878
879
  }
879
880
  async *next() {
881
+ if (!this.requeuedPreviousProcessRuns) {
882
+ this.requeuedPreviousProcessRuns = true;
883
+ // No run is running in this process before the first claim, so a
884
+ // `running` run was cut off by a restart (a hot reload) and would
885
+ // otherwise block its thread for good.
886
+ await this.conn.with((STORE) => {
887
+ const cutOff = Object.values(STORE.runs).filter((run) => run.status === "running");
888
+ for (const run of cutOff) {
889
+ run.status = "pending";
890
+ run.updated_at = new Date();
891
+ }
892
+ if (cutOff.length) {
893
+ logger.warn("Retrying runs cut off by a server restart", {
894
+ run_ids: cutOff.map((run) => run.run_id),
895
+ });
896
+ }
897
+ });
898
+ }
880
899
  yield* this.conn.withGenerator(async function* (STORE, options) {
881
900
  const now = new Date();
882
901
  const pendingRunIds = Object.values(STORE.runs)
@@ -1058,6 +1077,12 @@ export class FileSystemRuns {
1058
1077
  throw new HTTPException(404, { message: "Run not found" });
1059
1078
  }
1060
1079
  }
1080
+ // Ends the run's open streams, which would otherwise wait out their
1081
+ // timeout and report the run as not found.
1082
+ StreamManager.readers[run_id]?.push({
1083
+ topic: `run:${run_id}:control`,
1084
+ data: "done",
1085
+ });
1061
1086
  if (thread_id != null)
1062
1087
  checkpointer.delete(thread_id, run_id);
1063
1088
  delete STORE.runs[run_id];
@@ -1118,7 +1143,16 @@ export class FileSystemRuns {
1118
1143
  const control = StreamManager.getControl(runId);
1119
1144
  control?.abort(options.action ?? "interrupt");
1120
1145
  if (run.status === "pending") {
1121
- if (control || action !== "rollback") {
1146
+ if (control == null && run.kwargs.temporary) {
1147
+ // No worker will run it now, so nothing else deletes it, or its
1148
+ // temporary thread once no other run is left on it.
1149
+ delete STORE.runs[runId];
1150
+ const threadHasOtherRuns = Object.values(STORE.runs).some((other) => other.thread_id === run.thread_id);
1151
+ if (!threadHasOtherRuns) {
1152
+ promises.push(this.threads.delete(run.thread_id, undefined));
1153
+ }
1154
+ }
1155
+ else if (control || action !== "rollback") {
1122
1156
  run.status = "interrupted";
1123
1157
  run.updated_at = new Date();
1124
1158
  const thread = STORE.threads[run.thread_id];
@@ -1135,7 +1169,7 @@ export class FileSystemRuns {
1135
1169
  promises.push(this.delete(runId, threadId, auth));
1136
1170
  }
1137
1171
  }
1138
- else {
1172
+ else if (run.status !== "running") {
1139
1173
  logger.warn("Attempted to cancel non-pending run.", {
1140
1174
  run_id: runId,
1141
1175
  status: run.status,
@@ -1259,10 +1293,14 @@ export class FileSystemRuns {
1259
1293
  }
1260
1294
  }
1261
1295
  }
1262
- if (signal?.aborted &&
1263
- options?.cancelOnDisconnect &&
1264
- threadId != null) {
1265
- await runs.cancel(threadId, [runId], { action: "interrupt" }, auth);
1296
+ if (signal?.aborted && options?.cancelOnDisconnect) {
1297
+ // A stateless run is deleted with its thread as soon as it ends.
1298
+ await runs
1299
+ .cancel(threadId, [runId], { action: "interrupt" }, auth)
1300
+ .catch((error) => {
1301
+ if (!(error instanceof HTTPException) || error.status !== 404)
1302
+ throw error;
1303
+ });
1266
1304
  }
1267
1305
  });
1268
1306
  }
@@ -83,7 +83,7 @@ export interface Store {
83
83
  retry_counter: Record<string, number>;
84
84
  }
85
85
  export interface Message {
86
- topic: `run:${string}:stream:${string}`;
86
+ topic: `run:${string}:stream:${string}` | `run:${string}:control`;
87
87
  data: unknown;
88
88
  /**
89
89
  * Marks payloads that have already been converted to their protocol
package/dist/stream.mjs CHANGED
@@ -136,12 +136,16 @@ export async function* streamState(run, options) {
136
136
  ...(tracer && { callbacks: [tracer] }),
137
137
  }));
138
138
  const messages = {};
139
+ const messageIdByRun = {};
139
140
  const completedIds = new Set();
140
141
  for await (const event of events) {
141
142
  if (event.tags?.includes("langsmith:hidden"))
142
143
  continue;
143
- if (event.event === "on_chain_stream" &&
144
- (kwargs.subgraphs || event.run_id === run.run_id)) {
144
+ // A runnable streamed inside a node emits on_chain_stream with a plain chunk.
145
+ const isGraphChunk = event.event === "on_chain_stream" &&
146
+ ((kwargs.subgraphs && Array.isArray(event.data.chunk)) ||
147
+ event.run_id === run.run_id);
148
+ if (isGraphChunk) {
145
149
  // Pregel stream chunks are `[ns, mode, payload]`. Lightweight checkpoint
146
150
  // envelopes arrive as a separate `checkpoints` chunk before `values`.
147
151
  const rawTuple = (kwargs.subgraphs ? event.data.chunk : [null, ...event.data.chunk]);
@@ -215,10 +219,14 @@ export async function* streamState(run, options) {
215
219
  // - handleLLMEnd should not dedupe the message
216
220
  // - Don't think there's an utility that would convert a BaseMessageChunk to a BaseMessage?
217
221
  if (userStreamMode.includes("messages")) {
218
- if (event.event === "on_chain_stream" &&
219
- (kwargs.subgraphs || event.run_id === run.run_id)) {
222
+ if (isGraphChunk) {
220
223
  const newMessages = [];
221
- const [_, chunk] = event.data.chunk;
224
+ const tuple = event.data.chunk;
225
+ // With subgraphs on, chunks are [ns, mode, chunk]; a subgraph's
226
+ // values are not the root graph's messages.
227
+ if (tuple.length === 3 && tuple[0].length > 0)
228
+ continue;
229
+ const chunk = tuple.at(-1);
222
230
  let chunkMessages = [];
223
231
  if (typeof chunk === "object" &&
224
232
  chunk != null &&
@@ -242,19 +250,21 @@ export async function* streamState(run, options) {
242
250
  else if (event.event === "on_chat_model_stream" &&
243
251
  !event.tags?.includes("nostream")) {
244
252
  const message = event.data.chunk;
245
- if (!message.id)
253
+ // Anthropic sets the id on the first chunk only; the rest get run-<runId>.
254
+ const id = (messageIdByRun[event.run_id] ??= message.id);
255
+ if (!id)
246
256
  continue;
247
- if (messages[message.id] == null) {
248
- messages[message.id] = message;
257
+ if (messages[id] == null) {
258
+ messages[id] = message;
249
259
  yield {
250
260
  event: "messages/metadata",
251
- data: { [message.id]: { metadata: event.metadata } },
261
+ data: { [id]: { metadata: event.metadata } },
252
262
  };
253
263
  }
254
264
  else {
255
- messages[message.id] = messages[message.id].concat(message);
265
+ messages[id] = messages[id].concat(message);
256
266
  }
257
- yield { event: "messages/partial", data: [messages[message.id]] };
267
+ yield { event: "messages/partial", data: [messages[id]] };
258
268
  }
259
269
  }
260
270
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@langchain/langgraph-api",
3
- "version": "1.5.5",
3
+ "version": "1.5.7",
4
4
  "type": "module",
5
5
  "engines": {
6
6
  "node": "^18.19.0 || >=20.16.0"
@@ -52,7 +52,7 @@
52
52
  },
53
53
  "dependencies": {
54
54
  "@babel/code-frame": "^7.26.2",
55
- "@hono/node-server": "^2.1.0",
55
+ "@hono/node-server": "^2.1.3",
56
56
  "@hono/node-ws": "^1.3.0",
57
57
  "@hono/zod-validator": "^0.7.6",
58
58
  "@langchain/protocol": "^0.0.20",
@@ -70,8 +70,8 @@
70
70
  "tsx": "^4.19.3",
71
71
  "winston": "^3.17.0",
72
72
  "winston-console-format": "^1.0.8",
73
- "zod": "^3.25.76 || ^4",
74
- "@langchain/langgraph-ui": "1.5.5"
73
+ "zod": "^4.6.6",
74
+ "@langchain/langgraph-ui": "1.5.7"
75
75
  },
76
76
  "peerDependencies": {
77
77
  "@langchain/core": "^1.1.48",
@@ -102,11 +102,11 @@
102
102
  "postgres": "^3.4.5",
103
103
  "ts-node": "^10.9.2",
104
104
  "typescript": "^4.9.5 || ^5.4.5",
105
- "vitest": "^4.1.11",
105
+ "vitest": "^4.1.12",
106
106
  "wait-port": "^1.1.0",
107
- "@langchain/langgraph": "1.5.2",
108
- "@langchain/langgraph-sdk": "1.12.6",
109
- "@langchain/langgraph-checkpoint": "1.2.1"
107
+ "@langchain/langgraph": "1.6.0",
108
+ "@langchain/langgraph-checkpoint": "1.2.2",
109
+ "@langchain/langgraph-sdk": "1.12.9"
110
110
  },
111
111
  "scripts": {
112
112
  "clean": "rm -rf dist/ .turbo/ ./tests/graphs/.langgraph_api/ ./tests/protocol-v2/graphs/.langgraph_api/",