@langchain/core 0.3.56 → 0.3.58-rc.0

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.
Files changed (47) hide show
  1. package/dist/language_models/base.d.ts +14 -7
  2. package/dist/language_models/chat_models.cjs +5 -5
  3. package/dist/language_models/chat_models.d.ts +9 -3
  4. package/dist/language_models/chat_models.js +5 -5
  5. package/dist/output_parsers/openai_functions/json_output_functions_parsers.d.ts +1 -1
  6. package/dist/output_parsers/openai_tools/json_output_tools_parsers.cjs +3 -2
  7. package/dist/output_parsers/openai_tools/json_output_tools_parsers.d.ts +19 -5
  8. package/dist/output_parsers/openai_tools/json_output_tools_parsers.js +3 -2
  9. package/dist/output_parsers/structured.cjs +8 -7
  10. package/dist/output_parsers/structured.d.ts +10 -9
  11. package/dist/output_parsers/structured.js +6 -5
  12. package/dist/prompts/structured.cjs +7 -9
  13. package/dist/prompts/structured.js +7 -9
  14. package/dist/runnables/base.cjs +35 -19
  15. package/dist/runnables/base.d.ts +33 -13
  16. package/dist/runnables/base.js +32 -16
  17. package/dist/runnables/graph.cjs +2 -2
  18. package/dist/runnables/graph.js +2 -2
  19. package/dist/runnables/passthrough.cjs +1 -1
  20. package/dist/runnables/passthrough.d.ts +1 -1
  21. package/dist/runnables/passthrough.js +1 -1
  22. package/dist/runnables/types.d.ts +2 -2
  23. package/dist/tools/index.cjs +10 -12
  24. package/dist/tools/index.d.ts +11 -8
  25. package/dist/tools/index.js +8 -10
  26. package/dist/tools/types.cjs +2 -2
  27. package/dist/tools/types.d.ts +11 -13
  28. package/dist/tools/types.js +2 -2
  29. package/dist/utils/function_calling.cjs +4 -6
  30. package/dist/utils/function_calling.d.ts +4 -6
  31. package/dist/utils/function_calling.js +4 -6
  32. package/dist/utils/json_schema.cjs +6 -2
  33. package/dist/utils/json_schema.d.ts +3 -2
  34. package/dist/utils/json_schema.js +6 -2
  35. package/dist/utils/testing/index.cjs +1 -1
  36. package/dist/utils/testing/index.d.ts +8 -7
  37. package/dist/utils/testing/index.js +1 -1
  38. package/dist/utils/types/index.cjs +1 -1
  39. package/dist/utils/types/index.d.ts +1 -1
  40. package/dist/utils/types/index.js +1 -1
  41. package/dist/utils/types/zod.cjs +426 -0
  42. package/dist/utils/types/zod.d.ts +173 -0
  43. package/dist/utils/types/zod.js +406 -0
  44. package/package.json +2 -2
  45. package/dist/utils/types/is_zod_schema.cjs +0 -38
  46. package/dist/utils/types/is_zod_schema.d.ts +0 -8
  47. package/dist/utils/types/is_zod_schema.js +0 -34
@@ -0,0 +1,406 @@
1
+ import { parse, parseAsync, globalRegistry, util, clone, _unknown, $ZodUnknown, $ZodOptional, } from "zod/v4/core";
2
+ export function isZodSchemaV4(schema) {
3
+ if (typeof schema !== "object" || schema === null) {
4
+ return false;
5
+ }
6
+ const obj = schema;
7
+ if (!("_zod" in obj)) {
8
+ return false;
9
+ }
10
+ const zod = obj._zod;
11
+ return (typeof zod === "object" &&
12
+ zod !== null &&
13
+ "def" in zod);
14
+ }
15
+ export function isZodSchemaV3(schema) {
16
+ if (typeof schema !== "object" || schema === null) {
17
+ return false;
18
+ }
19
+ const obj = schema;
20
+ if (!("_def" in obj) || "_zod" in obj) {
21
+ return false;
22
+ }
23
+ const def = obj._def;
24
+ return (typeof def === "object" &&
25
+ def != null &&
26
+ "typeName" in def);
27
+ }
28
+ /** Backward compatible isZodSchema for Zod 3 */
29
+ export function isZodSchema(schema) {
30
+ if (isZodSchemaV4(schema)) {
31
+ console.warn("[WARNING] Attempting to use Zod 4 schema in a context where Zod 3 schema is expected. This may cause unexpected behavior.");
32
+ }
33
+ return isZodSchemaV3(schema);
34
+ }
35
+ /**
36
+ * Given either a Zod schema, or plain object, determine if the input is a Zod schema.
37
+ *
38
+ * @param {unknown} input
39
+ * @returns {boolean} Whether or not the provided input is a Zod schema.
40
+ */
41
+ export function isInteropZodSchema(input) {
42
+ if (!input) {
43
+ return false;
44
+ }
45
+ if (typeof input !== "object") {
46
+ return false;
47
+ }
48
+ if (Array.isArray(input)) {
49
+ return false;
50
+ }
51
+ if (isZodSchemaV4(input) ||
52
+ isZodSchemaV3(input)) {
53
+ return true;
54
+ }
55
+ return false;
56
+ }
57
+ /**
58
+ * Asynchronously parses the input using the provided Zod schema (v3 or v4) and returns a safe parse result.
59
+ * This function handles both Zod v3 and v4 schemas, returning a result object indicating success or failure.
60
+ *
61
+ * @template T - The expected output type of the schema.
62
+ * @param {InteropZodType<T>} schema - The Zod schema (v3 or v4) to use for parsing.
63
+ * @param {unknown} input - The input value to parse.
64
+ * @returns {Promise<InteropZodSafeParseResult<T>>} A promise that resolves to a safe parse result object.
65
+ * @throws {Error} If the schema is not a recognized Zod v3 or v4 schema.
66
+ */
67
+ export async function interopSafeParseAsync(schema, input) {
68
+ if (isZodSchemaV4(schema)) {
69
+ try {
70
+ const data = await parseAsync(schema, input);
71
+ return {
72
+ success: true,
73
+ data,
74
+ };
75
+ }
76
+ catch (error) {
77
+ return {
78
+ success: false,
79
+ error: error,
80
+ };
81
+ }
82
+ }
83
+ if (isZodSchemaV3(schema)) {
84
+ return schema.safeParse(input);
85
+ }
86
+ throw new Error("Schema must be an instance of z3.ZodType or z4.$ZodType");
87
+ }
88
+ /**
89
+ * Asynchronously parses the input using the provided Zod schema (v3 or v4) and returns the parsed value.
90
+ * Throws an error if parsing fails or if the schema is not a recognized Zod v3 or v4 schema.
91
+ *
92
+ * @template T - The expected output type of the schema.
93
+ * @param {InteropZodType<T>} schema - The Zod schema (v3 or v4) to use for parsing.
94
+ * @param {unknown} input - The input value to parse.
95
+ * @returns {Promise<T>} A promise that resolves to the parsed value.
96
+ * @throws {Error} If parsing fails or the schema is not a recognized Zod v3 or v4 schema.
97
+ */
98
+ export async function interopParseAsync(schema, input) {
99
+ if (isZodSchemaV4(schema)) {
100
+ return parse(schema, input);
101
+ }
102
+ if (isZodSchemaV3(schema)) {
103
+ return schema.parse(input);
104
+ }
105
+ throw new Error("Schema must be an instance of z3.ZodType or z4.$ZodType");
106
+ }
107
+ /**
108
+ * Safely parses the input using the provided Zod schema (v3 or v4) and returns a result object
109
+ * indicating success or failure. This function is compatible with both Zod v3 and v4 schemas.
110
+ *
111
+ * @template T - The expected output type of the schema.
112
+ * @param {InteropZodType<T>} schema - The Zod schema (v3 or v4) to use for parsing.
113
+ * @param {unknown} input - The input value to parse.
114
+ * @returns {InteropZodSafeParseResult<T>} An object with either the parsed data (on success)
115
+ * or the error (on failure).
116
+ * @throws {Error} If the schema is not a recognized Zod v3 or v4 schema.
117
+ */
118
+ export function interopSafeParse(schema, input) {
119
+ if (isZodSchemaV4(schema)) {
120
+ try {
121
+ const data = parse(schema, input);
122
+ return {
123
+ success: true,
124
+ data,
125
+ };
126
+ }
127
+ catch (error) {
128
+ return {
129
+ success: false,
130
+ error: error,
131
+ };
132
+ }
133
+ }
134
+ if (isZodSchemaV3(schema)) {
135
+ return schema.safeParse(input);
136
+ }
137
+ throw new Error("Schema must be an instance of z3.ZodType or z4.$ZodType");
138
+ }
139
+ /**
140
+ * Parses the input using the provided Zod schema (v3 or v4) and returns the parsed value.
141
+ * Throws an error if parsing fails or if the schema is not a recognized Zod v3 or v4 schema.
142
+ *
143
+ * @template T - The expected output type of the schema.
144
+ * @param {InteropZodType<T>} schema - The Zod schema (v3 or v4) to use for parsing.
145
+ * @param {unknown} input - The input value to parse.
146
+ * @returns {T} The parsed value.
147
+ * @throws {Error} If parsing fails or the schema is not a recognized Zod v3 or v4 schema.
148
+ */
149
+ export function interopParse(schema, input) {
150
+ if (isZodSchemaV4(schema)) {
151
+ return parse(schema, input);
152
+ }
153
+ if (isZodSchemaV3(schema)) {
154
+ return schema.parse(input);
155
+ }
156
+ throw new Error("Schema must be an instance of z3.ZodType or z4.$ZodType");
157
+ }
158
+ /**
159
+ * Retrieves the description from a schema definition (v3, v4, or plain object), if available.
160
+ *
161
+ * @param {unknown} schema - The schema to extract the description from.
162
+ * @returns {string | undefined} The description of the schema, or undefined if not present.
163
+ */
164
+ export function getSchemaDescription(schema) {
165
+ if (isZodSchemaV4(schema)) {
166
+ return globalRegistry.get(schema)?.description;
167
+ }
168
+ if (isZodSchemaV3(schema)) {
169
+ return schema.description;
170
+ }
171
+ if ("description" in schema && typeof schema.description === "string") {
172
+ return schema.description;
173
+ }
174
+ return undefined;
175
+ }
176
+ /**
177
+ * Determines if the provided Zod schema is "shapeless".
178
+ * A shapeless schema is one that does not define any object shape,
179
+ * such as ZodString, ZodNumber, ZodBoolean, ZodAny, etc.
180
+ * For ZodObject, it must have no shape keys to be considered shapeless.
181
+ * ZodRecord schemas are considered shapeless since they define dynamic
182
+ * key-value mappings without fixed keys.
183
+ *
184
+ * @param schema The Zod schema to check.
185
+ * @returns {boolean} True if the schema is shapeless, false otherwise.
186
+ */
187
+ export function isShapelessZodSchema(schema) {
188
+ if (!isInteropZodSchema(schema)) {
189
+ return false;
190
+ }
191
+ // Check for v3 schemas
192
+ if (isZodSchemaV3(schema)) {
193
+ // @ts-expect-error - zod v3 types are not compatible with zod v4 types
194
+ const def = schema._def;
195
+ // ZodObject is only shaped if it has actual shape keys
196
+ if (def.typeName === "ZodObject") {
197
+ const obj = schema;
198
+ return !obj.shape || Object.keys(obj.shape).length === 0;
199
+ }
200
+ // ZodRecord is shapeless (dynamic key-value mapping)
201
+ if (def.typeName === "ZodRecord") {
202
+ return true;
203
+ }
204
+ }
205
+ // Check for v4 schemas
206
+ if (isZodSchemaV4(schema)) {
207
+ const def = schema._zod.def;
208
+ // Object type is only shaped if it has actual shape keys
209
+ if (def.type === "object") {
210
+ const obj = schema;
211
+ return !obj.shape || Object.keys(obj.shape).length === 0;
212
+ }
213
+ // Record type is shapeless (dynamic key-value mapping)
214
+ if (def.type === "record") {
215
+ return true;
216
+ }
217
+ }
218
+ // For other schemas, check if they have a `shape` property
219
+ // If they don't have shape, they're likely shapeless
220
+ if (typeof schema === "object" && schema !== null && !("shape" in schema)) {
221
+ return true;
222
+ }
223
+ return false;
224
+ }
225
+ /**
226
+ * Determines if the provided Zod schema should be treated as a simple string schema
227
+ * that maps to DynamicTool. This aligns with the type-level constraint of
228
+ * InteropZodType<string | undefined> which only matches basic string schemas.
229
+ * If the provided schema is just z.string(), we can make the determination that
230
+ * the tool is just a generic string tool that doesn't require any input validation.
231
+ *
232
+ * This function only returns true for basic ZodString schemas, including:
233
+ * - Basic string schemas (z.string())
234
+ * - String schemas with validations (z.string().min(1), z.string().email(), etc.)
235
+ *
236
+ * This function returns false for everything else, including:
237
+ * - String schemas with defaults (z.string().default("value"))
238
+ * - Branded string schemas (z.string().brand<"UserId">())
239
+ * - String schemas with catch operations (z.string().catch("default"))
240
+ * - Optional/nullable string schemas (z.string().optional())
241
+ * - Transformed schemas (z.string().transform() or z.object().transform())
242
+ * - Object or record schemas, even if they're empty
243
+ * - Any other schema type
244
+ *
245
+ * @param schema The Zod schema to check.
246
+ * @returns {boolean} True if the schema is a basic ZodString, false otherwise.
247
+ */
248
+ export function isSimpleStringZodSchema(schema) {
249
+ if (!isInteropZodSchema(schema)) {
250
+ return false;
251
+ }
252
+ // For v3 schemas
253
+ if (isZodSchemaV3(schema)) {
254
+ // @ts-expect-error - zod v3 types are not compatible with zod v4 types
255
+ const def = schema._def;
256
+ // Only accept basic ZodString
257
+ return def.typeName === "ZodString";
258
+ }
259
+ // For v4 schemas
260
+ if (isZodSchemaV4(schema)) {
261
+ const def = schema._zod.def;
262
+ // Only accept basic string type
263
+ return def.type === "string";
264
+ }
265
+ return false;
266
+ }
267
+ /**
268
+ * Determines if the provided value is an InteropZodObject (Zod v3 or v4 object schema).
269
+ *
270
+ * @param obj The value to check.
271
+ * @returns {boolean} True if the value is a Zod v3 or v4 object schema, false otherwise.
272
+ */
273
+ export function isInteropZodObject(obj) {
274
+ if (isZodSchemaV3(obj)) {
275
+ // Zod v3 object schemas have _def.typeName === "ZodObject"
276
+ if (typeof obj === "object" &&
277
+ obj !== null &&
278
+ "_def" in obj &&
279
+ typeof obj._def === "object" &&
280
+ obj._def !== null &&
281
+ "typeName" in obj._def &&
282
+ obj._def.typeName === "ZodObject") {
283
+ return true;
284
+ }
285
+ }
286
+ if (isZodSchemaV4(obj)) {
287
+ // Zod v4 object schemas have _zod.def.type === "object"
288
+ if (typeof obj === "object" &&
289
+ obj !== null &&
290
+ "_zod" in obj &&
291
+ typeof obj._zod === "object" &&
292
+ obj._zod !== null &&
293
+ "def" in obj._zod &&
294
+ typeof obj._zod.def === "object" &&
295
+ obj._zod.def !== null &&
296
+ "type" in obj._zod.def &&
297
+ obj._zod.def.type === "object") {
298
+ return true;
299
+ }
300
+ }
301
+ return false;
302
+ }
303
+ /**
304
+ * Retrieves the shape (fields) of a Zod object schema, supporting both Zod v3 and v4.
305
+ *
306
+ * @template T - The type of the Zod object schema.
307
+ * @param {T} schema - The Zod object schema instance (either v3 or v4).
308
+ * @returns {InteropZodObjectShape<T>} The shape of the object schema.
309
+ * @throws {Error} If the schema is not a Zod v3 or v4 object.
310
+ */
311
+ export function getInteropZodObjectShape(schema) {
312
+ if (isZodSchemaV3(schema)) {
313
+ return schema.shape;
314
+ }
315
+ if (isZodSchemaV4(schema)) {
316
+ return schema._zod.def.shape;
317
+ }
318
+ throw new Error("Schema must be an instance of z3.ZodObject or z4.$ZodObject");
319
+ }
320
+ /**
321
+ * Extends a Zod object schema with additional fields, supporting both Zod v3 and v4.
322
+ *
323
+ * @template T - The type of the Zod object schema.
324
+ * @param {T} schema - The Zod object schema instance (either v3 or v4).
325
+ * @param {InteropZodObjectShape} extension - The fields to add to the schema.
326
+ * @returns {InteropZodObject} The extended Zod object schema.
327
+ * @throws {Error} If the schema is not a Zod v3 or v4 object.
328
+ */
329
+ export function extendInteropZodObject(schema, extension) {
330
+ if (isZodSchemaV3(schema)) {
331
+ // z3: .extend exists and works as expected
332
+ return schema.extend(extension);
333
+ }
334
+ if (isZodSchemaV4(schema)) {
335
+ // z4: .extend exists and works as expected
336
+ return util.extend(schema, extension);
337
+ }
338
+ throw new Error("Schema must be an instance of z3.ZodObject or z4.$ZodObject");
339
+ }
340
+ /**
341
+ * Returns a partial version of a Zod object schema, making all fields optional.
342
+ * Supports both Zod v3 and v4.
343
+ *
344
+ * @template T - The type of the Zod object schema.
345
+ * @param {T} schema - The Zod object schema instance (either v3 or v4).
346
+ * @returns {InteropZodObject} The partial Zod object schema.
347
+ * @throws {Error} If the schema is not a Zod v3 or v4 object.
348
+ */
349
+ export function interopZodObjectPartial(schema) {
350
+ if (isZodSchemaV3(schema)) {
351
+ // z3: .partial() exists and works as expected
352
+ return schema.partial();
353
+ }
354
+ if (isZodSchemaV4(schema)) {
355
+ // z4: util.partial exists and works as expected
356
+ return util.partial($ZodOptional, schema, undefined);
357
+ }
358
+ throw new Error("Schema must be an instance of z3.ZodObject or z4.$ZodObject");
359
+ }
360
+ export function interopZodObjectPassthrough(schema) {
361
+ if (isInteropZodObject(schema)) {
362
+ if (isZodSchemaV3(schema)) {
363
+ return schema.passthrough();
364
+ }
365
+ if (isZodSchemaV4(schema)) {
366
+ // Type reassign since ZodObjectV4 assumes that generics should be washed
367
+ const objectSchema = schema;
368
+ return clone(objectSchema, {
369
+ ...objectSchema._zod.def,
370
+ catchall: _unknown($ZodUnknown),
371
+ });
372
+ }
373
+ }
374
+ throw new Error("Schema must be an instance of z3.ZodObject or z4.$ZodObject");
375
+ }
376
+ /**
377
+ * Returns a getter function for the default value of a Zod schema, if one is defined.
378
+ * Supports both Zod v3 and v4 schemas. If the schema has a default value,
379
+ * the returned function will return that value when called. If no default is defined,
380
+ * returns undefined.
381
+ *
382
+ * @template T - The type of the Zod schema.
383
+ * @param {T} schema - The Zod schema instance (either v3 or v4).
384
+ * @returns {(() => InferInteropZodOutput<T>) | undefined} A function that returns the default value, or undefined if no default is set.
385
+ */
386
+ export function getInteropZodDefaultGetter(schema) {
387
+ if (isZodSchemaV3(schema)) {
388
+ try {
389
+ const defaultValue = schema.parse(undefined);
390
+ return () => defaultValue;
391
+ }
392
+ catch {
393
+ return undefined;
394
+ }
395
+ }
396
+ if (isZodSchemaV4(schema)) {
397
+ try {
398
+ const defaultValue = parse(schema, undefined);
399
+ return () => defaultValue;
400
+ }
401
+ catch {
402
+ return undefined;
403
+ }
404
+ }
405
+ return undefined;
406
+ }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@langchain/core",
3
- "version": "0.3.56",
3
+ "version": "0.3.58-rc.0",
4
4
  "description": "Core LangChain.js abstractions and schemas",
5
5
  "type": "module",
6
6
  "engines": {
@@ -43,7 +43,7 @@
43
43
  "p-queue": "^6.6.2",
44
44
  "p-retry": "4",
45
45
  "uuid": "^10.0.0",
46
- "zod": "^3.22.4",
46
+ "zod": "^3.25.32",
47
47
  "zod-to-json-schema": "^3.22.3"
48
48
  },
49
49
  "devDependencies": {
@@ -1,38 +0,0 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.isZodSchema = void 0;
4
- const zod_1 = require("zod");
5
- /**
6
- * Given either a Zod schema, or plain object, determine if the input is a Zod schema.
7
- *
8
- * @param {z.ZodType<RunOutput> | Record<string, unknown>} input
9
- * @returns {boolean} Whether or not the provided input is a Zod schema.
10
- */
11
- function isZodSchema(input) {
12
- if (!input) {
13
- return false;
14
- }
15
- if (typeof input !== "object") {
16
- return false;
17
- }
18
- if (Array.isArray(input)) {
19
- return false;
20
- }
21
- const asZodSchema = input;
22
- // relies on an internal property of Zod schemas, so this may break in the future, hence the
23
- // additional fallback checks below
24
- if (asZodSchema._def) {
25
- return true;
26
- }
27
- const zodFirstPartyTypeKinds = Object.values(zod_1.z.ZodFirstPartyTypeKind);
28
- if (zodFirstPartyTypeKinds.includes(asZodSchema.constructor?.name ?? "NOT_INCLUDED")) {
29
- return true;
30
- }
31
- // if all else fails, assume based on the presence of parse, parseAsync, safeParse, and
32
- // safeParseAsync, as these are characteristic of Zod schemas
33
- return (typeof asZodSchema.parse === "function" &&
34
- typeof asZodSchema.parseAsync === "function" &&
35
- typeof asZodSchema.safeParse === "function" &&
36
- typeof asZodSchema.safeParseAsync === "function");
37
- }
38
- exports.isZodSchema = isZodSchema;
@@ -1,8 +0,0 @@
1
- import { z } from "zod";
2
- /**
3
- * Given either a Zod schema, or plain object, determine if the input is a Zod schema.
4
- *
5
- * @param {z.ZodType<RunOutput> | Record<string, unknown>} input
6
- * @returns {boolean} Whether or not the provided input is a Zod schema.
7
- */
8
- export declare function isZodSchema<RunOutput extends Record<string, unknown> = Record<string, unknown>>(input: z.ZodType<RunOutput> | Record<string, unknown>): input is z.ZodType<RunOutput>;
@@ -1,34 +0,0 @@
1
- import { z } from "zod";
2
- /**
3
- * Given either a Zod schema, or plain object, determine if the input is a Zod schema.
4
- *
5
- * @param {z.ZodType<RunOutput> | Record<string, unknown>} input
6
- * @returns {boolean} Whether or not the provided input is a Zod schema.
7
- */
8
- export function isZodSchema(input) {
9
- if (!input) {
10
- return false;
11
- }
12
- if (typeof input !== "object") {
13
- return false;
14
- }
15
- if (Array.isArray(input)) {
16
- return false;
17
- }
18
- const asZodSchema = input;
19
- // relies on an internal property of Zod schemas, so this may break in the future, hence the
20
- // additional fallback checks below
21
- if (asZodSchema._def) {
22
- return true;
23
- }
24
- const zodFirstPartyTypeKinds = Object.values(z.ZodFirstPartyTypeKind);
25
- if (zodFirstPartyTypeKinds.includes(asZodSchema.constructor?.name ?? "NOT_INCLUDED")) {
26
- return true;
27
- }
28
- // if all else fails, assume based on the presence of parse, parseAsync, safeParse, and
29
- // safeParseAsync, as these are characteristic of Zod schemas
30
- return (typeof asZodSchema.parse === "function" &&
31
- typeof asZodSchema.parseAsync === "function" &&
32
- typeof asZodSchema.safeParse === "function" &&
33
- typeof asZodSchema.safeParseAsync === "function");
34
- }