@langchain/langgraph-api 1.4.2 → 1.4.4

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.
@@ -7,6 +7,7 @@ import { getRuntimeGraphSchema } from "../graph/parser/index.mjs";
7
7
  import { HTTPException } from "hono/http-exception";
8
8
  import * as schemas from "../schemas.mjs";
9
9
  import { assistants } from "../storage/context.mjs";
10
+ import { omitUndefined } from "../utils/runnableConfig.mjs";
10
11
  const api = new Hono();
11
12
  const RunnableConfigSchema = z.object({
12
13
  tags: z.array(z.string()).optional(),
@@ -20,7 +21,9 @@ const RunnableConfigSchema = z.object({
20
21
  const getRunnableConfig = (userConfig) => {
21
22
  if (!userConfig)
22
23
  return {};
23
- return {
24
+ // Omit undefined keys so callers that spread this into Pregel options do not
25
+ // wipe graph `withConfig` defaults (notably recursionLimit → 25).
26
+ return omitUndefined({
24
27
  configurable: userConfig.configurable,
25
28
  tags: userConfig.tags,
26
29
  metadata: userConfig.metadata,
@@ -28,7 +31,7 @@ const getRunnableConfig = (userConfig) => {
28
31
  maxConcurrency: userConfig.max_concurrency,
29
32
  recursionLimit: userConfig.recursion_limit,
30
33
  runId: userConfig.run_id,
31
- };
34
+ });
32
35
  };
33
36
  api.post("/assistants", zValidator("json", schemas.AssistantCreate), async (c) => {
34
37
  // Create Assistant
@@ -3,8 +3,21 @@ import { zValidator } from "@hono/zod-validator";
3
3
  import * as schemas from "../schemas.mjs";
4
4
  import { HTTPException } from "hono/http-exception";
5
5
  import { store as storageStore } from "../storage/store.mjs";
6
- import { handleAuthEvent } from "../auth/index.mjs";
6
+ import { handleAuthEvent, isAuthMatching, } from "../auth/index.mjs";
7
7
  const api = new Hono();
8
+ const AUTH_FILTER_SCAN_PAGE_LIMIT = 1000;
9
+ const AUTH_FILTER_MAX_SCAN_LIMIT = 10_000;
10
+ const throwStoreAuthScanLimitError = () => {
11
+ throw new HTTPException(400, {
12
+ message: "Authenticated store query exceeded maximum scan limit. Narrow the namespace or filters.",
13
+ });
14
+ };
15
+ const getStoreAuthMetadata = (namespace, key, value) => ({
16
+ ...(value ?? {}),
17
+ namespace: namespace ?? [],
18
+ ...(key == null ? {} : { key }),
19
+ });
20
+ const isStoreAuthMatching = (namespace, key, value, filters) => isAuthMatching(getStoreAuthMetadata(namespace, key, value), filters);
8
21
  const validateNamespace = (namespace) => {
9
22
  if (!namespace || namespace.length === 0) {
10
23
  throw new HTTPException(400, { message: "Namespace is required" });
@@ -35,41 +48,99 @@ api.post("/store/namespaces", zValidator("json", schemas.StoreListNamespaces), a
35
48
  validateNamespace(payload.prefix);
36
49
  if (payload.suffix)
37
50
  validateNamespace(payload.suffix);
38
- await handleAuthEvent(c.var.auth, "store:list_namespaces", {
51
+ const [filters] = await handleAuthEvent(c.var.auth, "store:list_namespaces", {
39
52
  namespace: payload.prefix,
40
53
  suffix: payload.suffix,
41
54
  max_depth: payload.max_depth,
42
55
  limit: payload.limit,
43
56
  offset: payload.offset,
44
57
  });
45
- return c.json({
46
- namespaces: await storageStore.listNamespaces({
47
- limit: payload.limit ?? 100,
48
- offset: payload.offset ?? 0,
58
+ const limit = payload.limit ?? 100;
59
+ const offset = payload.offset ?? 0;
60
+ let namespaces;
61
+ if (filters == null) {
62
+ namespaces = await storageStore.listNamespaces({
63
+ limit,
64
+ offset,
49
65
  prefix: payload.prefix,
50
66
  suffix: payload.suffix,
51
67
  maxDepth: payload.max_depth,
52
- }),
53
- });
68
+ });
69
+ }
70
+ else {
71
+ const authorizedNamespaces = [];
72
+ const requiredCount = offset + limit;
73
+ let scannedCount = 0;
74
+ while (authorizedNamespaces.length < requiredCount &&
75
+ scannedCount < AUTH_FILTER_MAX_SCAN_LIMIT) {
76
+ const scanLimit = Math.min(AUTH_FILTER_SCAN_PAGE_LIMIT, AUTH_FILTER_MAX_SCAN_LIMIT - scannedCount);
77
+ const candidates = await storageStore.listNamespaces({
78
+ limit: scanLimit,
79
+ offset: scannedCount,
80
+ prefix: payload.prefix,
81
+ suffix: payload.suffix,
82
+ maxDepth: payload.max_depth,
83
+ });
84
+ scannedCount += candidates.length;
85
+ authorizedNamespaces.push(...candidates.filter((namespace) => isStoreAuthMatching(namespace, undefined, undefined, filters)));
86
+ if (candidates.length < scanLimit)
87
+ break;
88
+ }
89
+ if (authorizedNamespaces.length < requiredCount &&
90
+ scannedCount >= AUTH_FILTER_MAX_SCAN_LIMIT) {
91
+ throwStoreAuthScanLimitError();
92
+ }
93
+ namespaces = authorizedNamespaces.slice(offset, requiredCount);
94
+ }
95
+ return c.json({ namespaces });
54
96
  });
55
97
  api.post("/store/items/search", zValidator("json", schemas.StoreSearchItems), async (c) => {
56
98
  // Search Items
57
99
  const payload = c.req.valid("json");
58
- if (payload.namespace_prefix)
100
+ if (payload.namespace_prefix.length)
59
101
  validateNamespace(payload.namespace_prefix);
60
- await handleAuthEvent(c.var.auth, "store:search", {
102
+ const [filters] = await handleAuthEvent(c.var.auth, "store:search", {
61
103
  namespace: payload.namespace_prefix,
62
104
  filter: payload.filter,
63
105
  limit: payload.limit,
64
106
  offset: payload.offset,
65
107
  query: payload.query,
66
108
  });
67
- const items = await storageStore.search(payload.namespace_prefix, {
68
- filter: payload.filter,
69
- limit: payload.limit ?? 10,
70
- offset: payload.offset ?? 0,
71
- query: payload.query,
72
- });
109
+ const limit = payload.limit ?? 10;
110
+ const offset = payload.offset ?? 0;
111
+ let items;
112
+ if (filters == null) {
113
+ items = await storageStore.search(payload.namespace_prefix, {
114
+ filter: payload.filter,
115
+ limit,
116
+ offset,
117
+ query: payload.query,
118
+ });
119
+ }
120
+ else {
121
+ const authorizedItems = [];
122
+ const requiredCount = offset + limit;
123
+ let scannedCount = 0;
124
+ while (authorizedItems.length < requiredCount &&
125
+ scannedCount < AUTH_FILTER_MAX_SCAN_LIMIT) {
126
+ const scanLimit = Math.min(AUTH_FILTER_SCAN_PAGE_LIMIT, AUTH_FILTER_MAX_SCAN_LIMIT - scannedCount);
127
+ const candidates = await storageStore.search(payload.namespace_prefix, {
128
+ filter: payload.filter,
129
+ limit: scanLimit,
130
+ offset: scannedCount,
131
+ query: payload.query,
132
+ });
133
+ scannedCount += candidates.length;
134
+ authorizedItems.push(...candidates.filter((item) => isStoreAuthMatching(item.namespace, item.key, item.value, filters)));
135
+ if (candidates.length < scanLimit)
136
+ break;
137
+ }
138
+ if (authorizedItems.length < requiredCount &&
139
+ scannedCount >= AUTH_FILTER_MAX_SCAN_LIMIT) {
140
+ throwStoreAuthScanLimitError();
141
+ }
142
+ items = authorizedItems.slice(offset, requiredCount);
143
+ }
73
144
  return c.json({ items: items.map(mapItemsToApi) });
74
145
  });
75
146
  api.put("/store/items", zValidator("json", schemas.StorePutItem), async (c) => {
@@ -77,12 +148,22 @@ api.put("/store/items", zValidator("json", schemas.StorePutItem), async (c) => {
77
148
  const payload = c.req.valid("json");
78
149
  if (payload.namespace)
79
150
  validateNamespace(payload.namespace);
80
- await handleAuthEvent(c.var.auth, "store:put", {
151
+ const [filters, mutable] = await handleAuthEvent(c.var.auth, "store:put", {
81
152
  namespace: payload.namespace,
82
153
  key: payload.key,
83
154
  value: payload.value,
84
155
  });
85
- await storageStore.put(payload.namespace, payload.key, payload.value);
156
+ if (mutable.namespace)
157
+ validateNamespace(mutable.namespace);
158
+ const existingItem = await storageStore.get(mutable.namespace, mutable.key);
159
+ if (existingItem != null &&
160
+ !isStoreAuthMatching(existingItem.namespace, existingItem.key, existingItem.value, filters)) {
161
+ throw new HTTPException(404, { message: "Item not found" });
162
+ }
163
+ if (!isStoreAuthMatching(mutable.namespace, mutable.key, mutable.value, filters)) {
164
+ throw new HTTPException(403, { message: "Not authorized" });
165
+ }
166
+ await storageStore.put(mutable.namespace, mutable.key, mutable.value);
86
167
  return c.body(null, 204);
87
168
  });
88
169
  api.delete("/store/items", zValidator("json", schemas.StoreDeleteItem), async (c) => {
@@ -90,22 +171,38 @@ api.delete("/store/items", zValidator("json", schemas.StoreDeleteItem), async (c
90
171
  const payload = c.req.valid("json");
91
172
  if (payload.namespace)
92
173
  validateNamespace(payload.namespace);
93
- await handleAuthEvent(c.var.auth, "store:delete", {
174
+ const [filters, mutable] = await handleAuthEvent(c.var.auth, "store:delete", {
94
175
  namespace: payload.namespace,
95
176
  key: payload.key,
96
177
  });
97
- await storageStore.delete(payload.namespace ?? [], payload.key);
178
+ const namespace = mutable.namespace ?? [];
179
+ if (namespace.length)
180
+ validateNamespace(namespace);
181
+ const existingItem = await storageStore.get(namespace, mutable.key);
182
+ if (existingItem != null) {
183
+ if (!isStoreAuthMatching(existingItem.namespace, existingItem.key, existingItem.value, filters)) {
184
+ throw new HTTPException(404, { message: "Item not found" });
185
+ }
186
+ }
187
+ else if (!isStoreAuthMatching(namespace, mutable.key, undefined, filters)) {
188
+ throw new HTTPException(403, { message: "Not authorized" });
189
+ }
190
+ await storageStore.delete(namespace, mutable.key);
98
191
  return c.body(null, 204);
99
192
  });
100
193
  api.get("/store/items", zValidator("query", schemas.StoreGetItem), async (c) => {
101
194
  // Get Item
102
195
  const payload = c.req.valid("query");
103
- await handleAuthEvent(c.var.auth, "store:get", {
196
+ const [filters, mutable] = await handleAuthEvent(c.var.auth, "store:get", {
104
197
  namespace: payload.namespace,
105
198
  key: payload.key,
106
199
  });
107
- const key = payload.key;
108
- const namespace = payload.namespace;
109
- return c.json(mapItemsToApi(await storageStore.get(namespace, key)));
200
+ const namespace = mutable.namespace ?? [];
201
+ const item = await storageStore.get(namespace, mutable.key);
202
+ if (item != null &&
203
+ !isStoreAuthMatching(item.namespace, item.key, item.value, filters)) {
204
+ throw new HTTPException(404, { message: "Item not found" });
205
+ }
206
+ return c.json(mapItemsToApi(item));
110
207
  });
111
208
  export default api;
package/dist/queue.mjs CHANGED
@@ -41,6 +41,7 @@ const worker = async (ops, run, attempt, signal) => {
41
41
  ...result,
42
42
  };
43
43
  };
44
+ const isInterrupted = () => signal.aborted && signal.reason === "interrupt";
44
45
  try {
45
46
  if (attempt > MAX_RETRY_ATTEMPTS) {
46
47
  throw new Error(`Run ${run.run_id} exceeded max attempts`);
@@ -65,12 +66,14 @@ const worker = async (ops, run, attempt, signal) => {
65
66
  }
66
67
  }
67
68
  catch (error) {
68
- await ops.runs.stream.publish({
69
- runId,
70
- resumable,
71
- event: "error",
72
- data: serializeError(error),
73
- });
69
+ if (!isInterrupted()) {
70
+ await ops.runs.stream.publish({
71
+ runId,
72
+ resumable,
73
+ event: "error",
74
+ data: serializeError(error),
75
+ });
76
+ }
74
77
  throw error;
75
78
  }
76
79
  endedAt = new Date();
@@ -87,21 +90,28 @@ const worker = async (ops, run, attempt, signal) => {
87
90
  }
88
91
  catch (error) {
89
92
  endedAt = new Date();
90
- if (error instanceof Error)
91
- exception = error;
92
- logError(error, {
93
- prefix: "Background run failed",
94
- context: {
95
- run_id: run.run_id,
96
- run_attempt: attempt,
97
- run_created_at: run.created_at,
98
- run_started_at: startedAt,
99
- run_ended_at: endedAt,
100
- run_exec_ms: endedAt.valueOf() - startedAt.valueOf(),
101
- },
102
- });
103
- status = "error";
104
- await ops.runs.setStatus(run.run_id, "error");
93
+ if (isInterrupted()) {
94
+ checkpoint = undefined;
95
+ status = "interrupted";
96
+ await ops.runs.setStatus(run.run_id, status);
97
+ }
98
+ else {
99
+ if (error instanceof Error)
100
+ exception = error;
101
+ logError(error, {
102
+ prefix: "Background run failed",
103
+ context: {
104
+ run_id: run.run_id,
105
+ run_attempt: attempt,
106
+ run_created_at: run.created_at,
107
+ run_started_at: startedAt,
108
+ run_ended_at: endedAt,
109
+ run_exec_ms: endedAt.valueOf() - startedAt.valueOf(),
110
+ },
111
+ });
112
+ status = "error";
113
+ await ops.runs.setStatus(run.run_id, status);
114
+ }
105
115
  }
106
116
  finally {
107
117
  if (temporary) {
package/dist/server.mjs CHANGED
@@ -132,6 +132,13 @@ export async function startServer(options, storage) {
132
132
  });
133
133
  // Loopback fetch used by webhooks and custom routes
134
134
  bindLoopbackFetch(app);
135
+ app.use(cors(options.http?.cors));
136
+ app.use(requestLogger());
137
+ if (options.auth?.path) {
138
+ logger.info(`Loading auth from ${options.auth.path}`);
139
+ await registerAuth(options.auth, { cwd: options.cwd });
140
+ app.use(auth());
141
+ }
135
142
  app.post("/internal/truncate", zValidator("json", z.object({
136
143
  runs: z.boolean().optional(),
137
144
  threads: z.boolean().optional(),
@@ -143,13 +150,6 @@ export async function startServer(options, storage) {
143
150
  ops.truncate({ runs, threads, assistants, checkpointer, store });
144
151
  return c.json({ ok: true });
145
152
  });
146
- app.use(cors(options.http?.cors));
147
- app.use(requestLogger());
148
- if (options.auth?.path) {
149
- logger.info(`Loading auth from ${options.auth.path}`);
150
- await registerAuth(options.auth, { cwd: options.cwd });
151
- app.use(auth());
152
- }
153
153
  if (options.http?.app) {
154
154
  logger.info(`Loading HTTP app from ${options.http.app}`);
155
155
  const { api } = await registerHttp(options.http.app, { cwd: options.cwd });
package/dist/stream.mjs CHANGED
@@ -5,7 +5,7 @@ import { Client as LangSmithClient, getDefaultProjectName } from "langsmith";
5
5
  import { getLangGraphCommand } from "./command.mjs";
6
6
  import { PROTOCOL_STREAM_RUN_KEY } from "./protocol/constants.mjs";
7
7
  import { checkLangGraphSemver } from "./semver/index.mjs";
8
- import { runnableConfigToCheckpoint, taskRunnableConfigToCheckpoint, } from "./utils/runnableConfig.mjs";
8
+ import { omitUndefined, runnableConfigToCheckpoint, taskRunnableConfigToCheckpoint, } from "./utils/runnableConfig.mjs";
9
9
  const isRunnableConfig = (config) => {
10
10
  if (typeof config !== "object" || config == null)
11
11
  return false;
@@ -114,7 +114,11 @@ export async function* streamState(run, options) {
114
114
  : undefined;
115
115
  const events = graph.streamEvents(kwargs.command != null
116
116
  ? getLangGraphCommand(kwargs.command)
117
- : (kwargs.input ?? null), {
117
+ : (kwargs.input ?? null),
118
+ // Omit undefined keys: Pregel spreads options over the graph's withConfig
119
+ // defaults, so `recursionLimit: undefined` would wipe a bound limit back
120
+ // to langchain-core's DEFAULT_RECURSION_LIMIT (25).
121
+ omitUndefined({
118
122
  version: "v2",
119
123
  interruptAfter: kwargs.interrupt_after,
120
124
  interruptBefore: kwargs.interrupt_before,
@@ -128,7 +132,7 @@ export async function* streamState(run, options) {
128
132
  streamMode: [...libStreamMode],
129
133
  signal: options?.signal,
130
134
  ...(tracer && { callbacks: [tracer] }),
131
- });
135
+ }));
132
136
  const messages = {};
133
137
  const completedIds = new Set();
134
138
  for await (const event of events) {
@@ -330,7 +334,10 @@ export async function* streamStateV2(run, options) {
330
334
  const graphInput = kwargs.command != null
331
335
  ? getLangGraphCommand(kwargs.command)
332
336
  : (kwargs.input ?? null);
333
- const graphOptions = {
337
+ // Omit undefined keys: Pregel spreads options over the graph's withConfig
338
+ // defaults, so `recursionLimit: undefined` would wipe a bound limit back
339
+ // to langchain-core's DEFAULT_RECURSION_LIMIT (25).
340
+ const graphOptions = omitUndefined({
334
341
  version: "v3",
335
342
  interruptAfter: kwargs.interrupt_after,
336
343
  interruptBefore: kwargs.interrupt_before,
@@ -342,7 +349,7 @@ export async function* streamStateV2(run, options) {
342
349
  runId: run.run_id,
343
350
  signal: options?.signal,
344
351
  ...(tracer && { callbacks: [tracer] }),
345
- };
352
+ });
346
353
  let graphRun;
347
354
  try {
348
355
  graphRun = await graph.streamEvents(graphInput, graphOptions);
@@ -1,3 +1,22 @@
1
1
  import type { Checkpoint, RunnableConfig } from "../storage/types.mjs";
2
+ /**
3
+ * Drop keys whose value is `undefined`.
4
+ *
5
+ * Pregel merges invoke/stream options roughly as:
6
+ * `{ recursionLimit: this.config?.recursionLimit, ...options }`.
7
+ * An explicit `recursionLimit: undefined` (e.g. from
8
+ * `kwargs.config?.recursion_limit` when the run omits it) overwrites the
9
+ * graph's `withConfig` default and falls back to langchain-core's 25.
10
+ *
11
+ * Keys whose value type cannot be `undefined` stay required so call sites
12
+ * preserve discriminators like `version: "v2" | "v3"` for streamEvents.
13
+ */
14
+ type OmitUndefinedKeys<T> = {
15
+ [K in keyof T as undefined extends T[K] ? never : K]: Exclude<T[K], undefined>;
16
+ } & {
17
+ [K in keyof T as undefined extends T[K] ? K : never]?: Exclude<T[K], undefined>;
18
+ };
19
+ export declare function omitUndefined<T extends Record<string, unknown>>(obj: T): OmitUndefinedKeys<T>;
2
20
  export declare const runnableConfigToCheckpoint: (config: RunnableConfig | null | undefined) => Checkpoint | null;
3
21
  export declare const taskRunnableConfigToCheckpoint: (config: RunnableConfig | null | undefined) => Partial<Checkpoint> | null;
22
+ export {};
@@ -1,4 +1,7 @@
1
1
  import { z } from "zod/v3";
2
+ export function omitUndefined(obj) {
3
+ return Object.fromEntries(Object.entries(obj).filter(([, value]) => value !== undefined));
4
+ }
2
5
  const ConfigSchema = z.object({
3
6
  configurable: z.object({
4
7
  thread_id: z.string(),
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@langchain/langgraph-api",
3
- "version": "1.4.2",
3
+ "version": "1.4.4",
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": "^1.19.13",
55
+ "@hono/node-server": "^2.0.11",
56
56
  "@hono/node-ws": "^1.3.0",
57
57
  "@hono/zod-validator": "^0.7.6",
58
58
  "@langchain/protocol": "^0.0.18",
@@ -61,7 +61,7 @@
61
61
  "dedent": "^1.5.3",
62
62
  "dotenv": "^16.4.7",
63
63
  "exit-hook": "^4.0.0",
64
- "hono": "^4.12.25",
64
+ "hono": "^4.12.34",
65
65
  "langsmith": ">=0.5.19 <1.0.0",
66
66
  "open": "^10.1.0",
67
67
  "semver": "^7.7.1",
@@ -71,7 +71,7 @@
71
71
  "winston": "^3.17.0",
72
72
  "winston-console-format": "^1.0.8",
73
73
  "zod": "^3.25.76 || ^4",
74
- "@langchain/langgraph-ui": "1.4.2"
74
+ "@langchain/langgraph-ui": "1.4.4"
75
75
  },
76
76
  "peerDependencies": {
77
77
  "@langchain/core": "^1.1.48",
@@ -90,7 +90,7 @@
90
90
  }
91
91
  },
92
92
  "devDependencies": {
93
- "@langchain/core": "^1.2.1",
93
+ "@langchain/core": "^1.2.3",
94
94
  "@types/babel__code-frame": "^7.0.6",
95
95
  "@types/node": "^18.15.11",
96
96
  "@types/react": "^19.2.17",
@@ -98,14 +98,14 @@
98
98
  "@types/semver": "^7.7.0",
99
99
  "deepagents": "^1.10.5",
100
100
  "jose": "^6.0.10",
101
- "langchain": "^1.5.2",
101
+ "langchain": "^1.5.3",
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.9",
105
+ "vitest": "^4.1.10",
106
106
  "wait-port": "^1.1.0",
107
- "@langchain/langgraph": "1.4.7",
108
- "@langchain/langgraph-sdk": "1.9.25",
107
+ "@langchain/langgraph": "1.4.9",
108
+ "@langchain/langgraph-sdk": "1.9.28",
109
109
  "@langchain/langgraph-checkpoint": "1.1.3"
110
110
  },
111
111
  "scripts": {