@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
@@ -4,7 +4,7 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
4
4
  };
5
5
  Object.defineProperty(exports, "__esModule", { value: true });
6
6
  exports.convertRunnableToTool = exports.RunnableToolLike = exports.RunnablePick = exports.RunnableAssign = exports._coerceToRunnable = exports.RunnableWithFallbacks = exports.RunnableParallel = exports.RunnableLambda = exports.RunnableTraceable = exports.RunnableMap = exports.RunnableSequence = exports.RunnableRetry = exports.RunnableEach = exports.RunnableBinding = exports.Runnable = exports._coerceToDict = void 0;
7
- const zod_1 = require("zod");
7
+ const v3_1 = require("zod/v3");
8
8
  const p_retry_1 = __importDefault(require("p-retry"));
9
9
  const uuid_1 = require("uuid");
10
10
  const traceable_1 = require("langsmith/singletons/traceable");
@@ -22,6 +22,7 @@ const graph_js_1 = require("./graph.cjs");
22
22
  const wrappers_js_1 = require("./wrappers.cjs");
23
23
  const iter_js_1 = require("./iter.cjs");
24
24
  const utils_js_2 = require("../tools/utils.cjs");
25
+ const zod_js_1 = require("../utils/types/zod.cjs");
25
26
  // eslint-disable-next-line @typescript-eslint/no-explicit-any
26
27
  function _coerceToDict(value, defaultKey) {
27
28
  return value &&
@@ -63,6 +64,8 @@ class Runnable extends serializable_js_1.Serializable {
63
64
  * Bind arguments to a Runnable, returning a new Runnable.
64
65
  * @param kwargs
65
66
  * @returns A new RunnableBinding that, when invoked, will apply the bound args.
67
+ *
68
+ * @deprecated Use {@link withConfig} instead. This will be removed in the next breaking release.
66
69
  */
67
70
  bind(kwargs) {
68
71
  // eslint-disable-next-line @typescript-eslint/no-use-before-define
@@ -71,6 +74,8 @@ class Runnable extends serializable_js_1.Serializable {
71
74
  /**
72
75
  * Return a new Runnable that maps a list of inputs to a list of outputs,
73
76
  * by calling invoke() with each input.
77
+ *
78
+ * @deprecated This will be removed in the next breaking release.
74
79
  */
75
80
  map() {
76
81
  // eslint-disable-next-line @typescript-eslint/no-use-before-define
@@ -78,7 +83,8 @@ class Runnable extends serializable_js_1.Serializable {
78
83
  }
79
84
  /**
80
85
  * Add retry logic to an existing runnable.
81
- * @param kwargs
86
+ * @param fields.stopAfterAttempt The number of attempts to retry.
87
+ * @param fields.onFailedAttempt A function that is called when a retry fails.
82
88
  * @returns A new RunnableRetry that, when invoked, will retry according to the parameters.
83
89
  */
84
90
  withRetry(fields) {
@@ -337,13 +343,13 @@ class Runnable extends serializable_js_1.Serializable {
337
343
  // TODO: Add input schema for runnables
338
344
  const inputNode = graph.addNode({
339
345
  name: `${this.getName()}Input`,
340
- schema: zod_1.z.any(),
346
+ schema: v3_1.z.any(),
341
347
  });
342
348
  const runnableNode = graph.addNode(this);
343
349
  // TODO: Add output schemas for runnables
344
350
  const outputNode = graph.addNode({
345
351
  name: `${this.getName()}Output`,
346
- schema: zod_1.z.any(),
352
+ schema: v3_1.z.any(),
347
353
  });
348
354
  graph.addEdge(inputNode, runnableNode);
349
355
  graph.addEdge(runnableNode, outputNode);
@@ -774,7 +780,8 @@ class Runnable extends serializable_js_1.Serializable {
774
780
  }
775
781
  exports.Runnable = Runnable;
776
782
  /**
777
- * A runnable that delegates calls to another runnable with a set of kwargs.
783
+ * Wraps a runnable and applies partial config upon invocation.
784
+ *
778
785
  * @example
779
786
  * ```typescript
780
787
  * import {
@@ -869,8 +876,14 @@ class RunnableBinding extends Runnable {
869
876
  ? await Promise.all(this.configFactories.map(async (configFactory) => await configFactory(config)))
870
877
  : []));
871
878
  }
879
+ /**
880
+ * Binds the runnable with the specified arguments.
881
+ * @param kwargs The arguments to bind the runnable with.
882
+ * @returns A new instance of the `RunnableBinding` class that is bound with the specified arguments.
883
+ *
884
+ * @deprecated Use {@link withConfig} instead. This will be removed in the next breaking release.
885
+ */
872
886
  bind(kwargs) {
873
- // eslint-disable-next-line @typescript-eslint/no-explicit-any
874
887
  return new this.constructor({
875
888
  bound: this.bound,
876
889
  kwargs: { ...this.kwargs, ...kwargs },
@@ -878,7 +891,6 @@ class RunnableBinding extends Runnable {
878
891
  });
879
892
  }
880
893
  withConfig(config) {
881
- // eslint-disable-next-line @typescript-eslint/no-explicit-any
882
894
  return new this.constructor({
883
895
  bound: this.bound,
884
896
  kwargs: this.kwargs,
@@ -886,11 +898,13 @@ class RunnableBinding extends Runnable {
886
898
  });
887
899
  }
888
900
  withRetry(fields) {
889
- // eslint-disable-next-line @typescript-eslint/no-explicit-any
890
- return new this.constructor({
891
- bound: this.bound.withRetry(fields),
901
+ // eslint-disable-next-line @typescript-eslint/no-use-before-define
902
+ return new RunnableRetry({
903
+ bound: this.bound,
892
904
  kwargs: this.kwargs,
893
905
  config: this.config,
906
+ maxAttemptNumber: fields?.stopAfterAttempt,
907
+ ...fields,
894
908
  });
895
909
  }
896
910
  async invoke(input, options) {
@@ -982,6 +996,8 @@ exports.RunnableBinding = RunnableBinding;
982
996
  *
983
997
  * // ["Hello, ALICE!", "Hello, BOB!", "Hello, CAROL!"]
984
998
  * ```
999
+ *
1000
+ * @deprecated This will be removed in the next breaking release.
985
1001
  */
986
1002
  class RunnableEach extends Runnable {
987
1003
  static lc_name() {
@@ -1013,6 +1029,8 @@ class RunnableEach extends Runnable {
1013
1029
  * Binds the runnable with the specified arguments.
1014
1030
  * @param kwargs The arguments to bind the runnable with.
1015
1031
  * @returns A new instance of the `RunnableEach` class that is bound with the specified arguments.
1032
+ *
1033
+ * @deprecated Use {@link withConfig} instead. This will be removed in the next breaking release.
1016
1034
  */
1017
1035
  bind(kwargs) {
1018
1036
  return new RunnableEach({
@@ -2317,7 +2335,7 @@ class RunnableToolLike extends RunnableBinding {
2317
2335
  let toolInput;
2318
2336
  if ((0, utils_js_2._isToolCall)(input)) {
2319
2337
  try {
2320
- toolInput = await this.schema.parseAsync(input.args);
2338
+ toolInput = await (0, zod_js_1.interopParseAsync)(this.schema, input.args);
2321
2339
  }
2322
2340
  catch (e) {
2323
2341
  throw new utils_js_2.ToolInputParsingException(`Received tool input did not match expected schema`, JSON.stringify(input.args));
@@ -2371,20 +2389,18 @@ exports.RunnableToolLike = RunnableToolLike;
2371
2389
  * @param fields
2372
2390
  * @param {string | undefined} [fields.name] The name of the tool. If not provided, it will default to the name of the runnable.
2373
2391
  * @param {string | undefined} [fields.description] The description of the tool. Falls back to the description on the Zod schema if not provided, or undefined if neither are provided.
2374
- * @param {z.ZodType<RunInput>} [fields.schema] The Zod schema for the input of the tool. Infers the Zod type from the input type of the runnable.
2375
- * @returns {RunnableToolLike<z.ZodType<RunInput>, RunOutput>} An instance of `RunnableToolLike` which is a runnable that can be used as a tool.
2392
+ * @param {InteropZodType<RunInput>} [fields.schema] The Zod schema for the input of the tool. Infers the Zod type from the input type of the runnable.
2393
+ * @returns {RunnableToolLike<InteropZodType<RunInput>, RunOutput>} An instance of `RunnableToolLike` which is a runnable that can be used as a tool.
2376
2394
  */
2377
2395
  function convertRunnableToTool(runnable, fields) {
2378
2396
  const name = fields.name ?? runnable.getName();
2379
- const description = fields.description ?? fields.schema?.description;
2380
- if (fields.schema.constructor === zod_1.z.ZodString) {
2397
+ const description = fields.description ?? (0, zod_js_1.getSchemaDescription)(fields.schema);
2398
+ if ((0, zod_js_1.isSimpleStringZodSchema)(fields.schema)) {
2381
2399
  return new RunnableToolLike({
2382
2400
  name,
2383
2401
  description,
2384
- schema: zod_1.z
2385
- .object({
2386
- input: zod_1.z.string(),
2387
- })
2402
+ schema: v3_1.z
2403
+ .object({ input: v3_1.z.string() })
2388
2404
  .transform((input) => input.input),
2389
2405
  bound: runnable,
2390
2406
  });
@@ -1,4 +1,3 @@
1
- import { z } from "zod";
2
1
  import { type TraceableFunction } from "langsmith/singletons/traceable";
3
2
  import type { RunnableInterface, RunnableBatchOptions, RunnableConfig } from "./types.js";
4
3
  import { CallbackManagerForChainRun } from "../callbacks/manager.js";
@@ -9,6 +8,7 @@ import { IterableReadableStream } from "../utils/stream.js";
9
8
  import { Run } from "../tracers/base.js";
10
9
  import { Graph } from "./graph.js";
11
10
  import { ToolCall } from "../messages/tool.js";
11
+ import { InferInteropZodOutput, InteropZodType } from "../utils/types/zod.js";
12
12
  export { type RunnableInterface, RunnableBatchOptions };
13
13
  export type RunnableFunc<RunInput, RunOutput, CallOptions extends RunnableConfig = RunnableConfig> = (input: RunInput, options: CallOptions | Record<string, any> | (Record<string, any> & CallOptions)) => RunOutput | Promise<RunOutput>;
14
14
  export type RunnableMapLike<RunInput, RunOutput> = {
@@ -30,16 +30,21 @@ export declare abstract class Runnable<RunInput = any, RunOutput = any, CallOpti
30
30
  * Bind arguments to a Runnable, returning a new Runnable.
31
31
  * @param kwargs
32
32
  * @returns A new RunnableBinding that, when invoked, will apply the bound args.
33
+ *
34
+ * @deprecated Use {@link withConfig} instead. This will be removed in the next breaking release.
33
35
  */
34
36
  bind(kwargs: Partial<CallOptions>): Runnable<RunInput, RunOutput, CallOptions>;
35
37
  /**
36
38
  * Return a new Runnable that maps a list of inputs to a list of outputs,
37
39
  * by calling invoke() with each input.
40
+ *
41
+ * @deprecated This will be removed in the next breaking release.
38
42
  */
39
43
  map(): Runnable<RunInput[], RunOutput[], CallOptions>;
40
44
  /**
41
45
  * Add retry logic to an existing runnable.
42
- * @param kwargs
46
+ * @param fields.stopAfterAttempt The number of attempts to retry.
47
+ * @param fields.onFailedAttempt A function that is called when a retry fails.
43
48
  * @returns A new RunnableRetry that, when invoked, will retry according to the parameters.
44
49
  */
45
50
  withRetry(fields?: {
@@ -305,17 +310,21 @@ export declare abstract class Runnable<RunInput = any, RunOutput = any, CallOpti
305
310
  asTool<T extends RunInput = RunInput>(fields: {
306
311
  name?: string;
307
312
  description?: string;
308
- schema: z.ZodType<T>;
309
- }): RunnableToolLike<z.ZodType<T | ToolCall>, RunOutput>;
313
+ schema: InteropZodType<T>;
314
+ }): RunnableToolLike<InteropZodType<T | ToolCall>, RunOutput>;
310
315
  }
311
316
  export type RunnableBindingArgs<RunInput, RunOutput, CallOptions extends RunnableConfig = RunnableConfig> = {
312
317
  bound: Runnable<RunInput, RunOutput, CallOptions>;
318
+ /**
319
+ * @deprecated use {@link config} instead
320
+ */
313
321
  kwargs?: Partial<CallOptions>;
314
322
  config: RunnableConfig;
315
- configFactories?: Array<(config: RunnableConfig) => RunnableConfig>;
323
+ configFactories?: Array<(config: RunnableConfig) => RunnableConfig | Promise<RunnableConfig>>;
316
324
  };
317
325
  /**
318
- * A runnable that delegates calls to another runnable with a set of kwargs.
326
+ * Wraps a runnable and applies partial config upon invocation.
327
+ *
319
328
  * @example
320
329
  * ```typescript
321
330
  * import {
@@ -365,8 +374,15 @@ export declare class RunnableBinding<RunInput, RunOutput, CallOptions extends Ru
365
374
  constructor(fields: RunnableBindingArgs<RunInput, RunOutput, CallOptions>);
366
375
  getName(suffix?: string | undefined): string;
367
376
  _mergeConfig(...options: (Partial<CallOptions> | RunnableConfig | undefined)[]): Promise<Partial<CallOptions>>;
377
+ /**
378
+ * Binds the runnable with the specified arguments.
379
+ * @param kwargs The arguments to bind the runnable with.
380
+ * @returns A new instance of the `RunnableBinding` class that is bound with the specified arguments.
381
+ *
382
+ * @deprecated Use {@link withConfig} instead. This will be removed in the next breaking release.
383
+ */
368
384
  bind(kwargs: Partial<CallOptions>): RunnableBinding<RunInput, RunOutput, CallOptions>;
369
- withConfig(config: RunnableConfig): Runnable<RunInput, RunOutput, CallOptions>;
385
+ withConfig(config: Partial<CallOptions>): Runnable<RunInput, RunOutput, CallOptions>;
370
386
  withRetry(fields?: {
371
387
  stopAfterAttempt?: number;
372
388
  onFailedAttempt?: RunnableRetryFailedAttemptHandler;
@@ -428,6 +444,8 @@ export declare class RunnableBinding<RunInput, RunOutput, CallOptions extends Ru
428
444
  *
429
445
  * // ["Hello, ALICE!", "Hello, BOB!", "Hello, CAROL!"]
430
446
  * ```
447
+ *
448
+ * @deprecated This will be removed in the next breaking release.
431
449
  */
432
450
  export declare class RunnableEach<RunInputItem, RunOutputItem, CallOptions extends RunnableConfig> extends Runnable<RunInputItem[], RunOutputItem[], CallOptions> {
433
451
  static lc_name(): string;
@@ -441,6 +459,8 @@ export declare class RunnableEach<RunInputItem, RunOutputItem, CallOptions exten
441
459
  * Binds the runnable with the specified arguments.
442
460
  * @param kwargs The arguments to bind the runnable with.
443
461
  * @returns A new instance of the `RunnableEach` class that is bound with the specified arguments.
462
+ *
463
+ * @deprecated Use {@link withConfig} instead. This will be removed in the next breaking release.
444
464
  */
445
465
  bind(kwargs: Partial<CallOptions>): RunnableEach<RunInputItem, RunOutputItem, CallOptions>;
446
466
  /**
@@ -890,13 +910,13 @@ export declare class RunnablePick<RunInput extends Record<string, any> = Record<
890
910
  transform(generator: AsyncGenerator<RunInput>, options?: Partial<RunnableConfig>): AsyncGenerator<RunOutput>;
891
911
  stream(input: RunInput, options?: Partial<RunnableConfig>): Promise<IterableReadableStream<RunOutput>>;
892
912
  }
893
- export interface RunnableToolLikeArgs<RunInput extends z.ZodType = z.ZodType, RunOutput = unknown> extends Omit<RunnableBindingArgs<z.infer<RunInput>, RunOutput>, "config"> {
913
+ export interface RunnableToolLikeArgs<RunInput extends InteropZodType = InteropZodType, RunOutput = unknown> extends Omit<RunnableBindingArgs<InferInteropZodOutput<RunInput>, RunOutput>, "config"> {
894
914
  name: string;
895
915
  description?: string;
896
916
  schema: RunInput;
897
917
  config?: RunnableConfig;
898
918
  }
899
- export declare class RunnableToolLike<RunInput extends z.ZodType = z.ZodType, RunOutput = unknown> extends RunnableBinding<z.infer<RunInput>, RunOutput> {
919
+ export declare class RunnableToolLike<RunInput extends InteropZodType = InteropZodType, RunOutput = unknown> extends RunnableBinding<InferInteropZodOutput<RunInput>, RunOutput> {
900
920
  name: string;
901
921
  description?: string;
902
922
  schema: RunInput;
@@ -913,11 +933,11 @@ export declare class RunnableToolLike<RunInput extends z.ZodType = z.ZodType, Ru
913
933
  * @param fields
914
934
  * @param {string | undefined} [fields.name] The name of the tool. If not provided, it will default to the name of the runnable.
915
935
  * @param {string | undefined} [fields.description] The description of the tool. Falls back to the description on the Zod schema if not provided, or undefined if neither are provided.
916
- * @param {z.ZodType<RunInput>} [fields.schema] The Zod schema for the input of the tool. Infers the Zod type from the input type of the runnable.
917
- * @returns {RunnableToolLike<z.ZodType<RunInput>, RunOutput>} An instance of `RunnableToolLike` which is a runnable that can be used as a tool.
936
+ * @param {InteropZodType<RunInput>} [fields.schema] The Zod schema for the input of the tool. Infers the Zod type from the input type of the runnable.
937
+ * @returns {RunnableToolLike<InteropZodType<RunInput>, RunOutput>} An instance of `RunnableToolLike` which is a runnable that can be used as a tool.
918
938
  */
919
939
  export declare function convertRunnableToTool<RunInput, RunOutput>(runnable: Runnable<RunInput, RunOutput>, fields: {
920
940
  name?: string;
921
941
  description?: string;
922
- schema: z.ZodType<RunInput>;
923
- }): RunnableToolLike<z.ZodType<RunInput | ToolCall>, RunOutput>;
942
+ schema: InteropZodType<RunInput>;
943
+ }): RunnableToolLike<InteropZodType<RunInput | ToolCall>, RunOutput>;
@@ -1,4 +1,4 @@
1
- import { z } from "zod";
1
+ import { z } from "zod/v3";
2
2
  import pRetry from "p-retry";
3
3
  import { v4 as uuidv4 } from "uuid";
4
4
  import { isTraceableFunction, } from "langsmith/singletons/traceable";
@@ -16,6 +16,7 @@ import { Graph } from "./graph.js";
16
16
  import { convertToHttpEventStream } from "./wrappers.js";
17
17
  import { consumeAsyncIterableInContext, consumeIteratorInContext, isAsyncIterable, isIterableIterator, isIterator, } from "./iter.js";
18
18
  import { _isToolCall, ToolInputParsingException } from "../tools/utils.js";
19
+ import { getSchemaDescription, interopParseAsync, isSimpleStringZodSchema, } from "../utils/types/zod.js";
19
20
  // eslint-disable-next-line @typescript-eslint/no-explicit-any
20
21
  export function _coerceToDict(value, defaultKey) {
21
22
  return value &&
@@ -56,6 +57,8 @@ export class Runnable extends Serializable {
56
57
  * Bind arguments to a Runnable, returning a new Runnable.
57
58
  * @param kwargs
58
59
  * @returns A new RunnableBinding that, when invoked, will apply the bound args.
60
+ *
61
+ * @deprecated Use {@link withConfig} instead. This will be removed in the next breaking release.
59
62
  */
60
63
  bind(kwargs) {
61
64
  // eslint-disable-next-line @typescript-eslint/no-use-before-define
@@ -64,6 +67,8 @@ export class Runnable extends Serializable {
64
67
  /**
65
68
  * Return a new Runnable that maps a list of inputs to a list of outputs,
66
69
  * by calling invoke() with each input.
70
+ *
71
+ * @deprecated This will be removed in the next breaking release.
67
72
  */
68
73
  map() {
69
74
  // eslint-disable-next-line @typescript-eslint/no-use-before-define
@@ -71,7 +76,8 @@ export class Runnable extends Serializable {
71
76
  }
72
77
  /**
73
78
  * Add retry logic to an existing runnable.
74
- * @param kwargs
79
+ * @param fields.stopAfterAttempt The number of attempts to retry.
80
+ * @param fields.onFailedAttempt A function that is called when a retry fails.
75
81
  * @returns A new RunnableRetry that, when invoked, will retry according to the parameters.
76
82
  */
77
83
  withRetry(fields) {
@@ -766,7 +772,8 @@ export class Runnable extends Serializable {
766
772
  }
767
773
  }
768
774
  /**
769
- * A runnable that delegates calls to another runnable with a set of kwargs.
775
+ * Wraps a runnable and applies partial config upon invocation.
776
+ *
770
777
  * @example
771
778
  * ```typescript
772
779
  * import {
@@ -861,8 +868,14 @@ export class RunnableBinding extends Runnable {
861
868
  ? await Promise.all(this.configFactories.map(async (configFactory) => await configFactory(config)))
862
869
  : []));
863
870
  }
871
+ /**
872
+ * Binds the runnable with the specified arguments.
873
+ * @param kwargs The arguments to bind the runnable with.
874
+ * @returns A new instance of the `RunnableBinding` class that is bound with the specified arguments.
875
+ *
876
+ * @deprecated Use {@link withConfig} instead. This will be removed in the next breaking release.
877
+ */
864
878
  bind(kwargs) {
865
- // eslint-disable-next-line @typescript-eslint/no-explicit-any
866
879
  return new this.constructor({
867
880
  bound: this.bound,
868
881
  kwargs: { ...this.kwargs, ...kwargs },
@@ -870,7 +883,6 @@ export class RunnableBinding extends Runnable {
870
883
  });
871
884
  }
872
885
  withConfig(config) {
873
- // eslint-disable-next-line @typescript-eslint/no-explicit-any
874
886
  return new this.constructor({
875
887
  bound: this.bound,
876
888
  kwargs: this.kwargs,
@@ -878,11 +890,13 @@ export class RunnableBinding extends Runnable {
878
890
  });
879
891
  }
880
892
  withRetry(fields) {
881
- // eslint-disable-next-line @typescript-eslint/no-explicit-any
882
- return new this.constructor({
883
- bound: this.bound.withRetry(fields),
893
+ // eslint-disable-next-line @typescript-eslint/no-use-before-define
894
+ return new RunnableRetry({
895
+ bound: this.bound,
884
896
  kwargs: this.kwargs,
885
897
  config: this.config,
898
+ maxAttemptNumber: fields?.stopAfterAttempt,
899
+ ...fields,
886
900
  });
887
901
  }
888
902
  async invoke(input, options) {
@@ -973,6 +987,8 @@ export class RunnableBinding extends Runnable {
973
987
  *
974
988
  * // ["Hello, ALICE!", "Hello, BOB!", "Hello, CAROL!"]
975
989
  * ```
990
+ *
991
+ * @deprecated This will be removed in the next breaking release.
976
992
  */
977
993
  export class RunnableEach extends Runnable {
978
994
  static lc_name() {
@@ -1004,6 +1020,8 @@ export class RunnableEach extends Runnable {
1004
1020
  * Binds the runnable with the specified arguments.
1005
1021
  * @param kwargs The arguments to bind the runnable with.
1006
1022
  * @returns A new instance of the `RunnableEach` class that is bound with the specified arguments.
1023
+ *
1024
+ * @deprecated Use {@link withConfig} instead. This will be removed in the next breaking release.
1007
1025
  */
1008
1026
  bind(kwargs) {
1009
1027
  return new RunnableEach({
@@ -2297,7 +2315,7 @@ export class RunnableToolLike extends RunnableBinding {
2297
2315
  let toolInput;
2298
2316
  if (_isToolCall(input)) {
2299
2317
  try {
2300
- toolInput = await this.schema.parseAsync(input.args);
2318
+ toolInput = await interopParseAsync(this.schema, input.args);
2301
2319
  }
2302
2320
  catch (e) {
2303
2321
  throw new ToolInputParsingException(`Received tool input did not match expected schema`, JSON.stringify(input.args));
@@ -2350,20 +2368,18 @@ export class RunnableToolLike extends RunnableBinding {
2350
2368
  * @param fields
2351
2369
  * @param {string | undefined} [fields.name] The name of the tool. If not provided, it will default to the name of the runnable.
2352
2370
  * @param {string | undefined} [fields.description] The description of the tool. Falls back to the description on the Zod schema if not provided, or undefined if neither are provided.
2353
- * @param {z.ZodType<RunInput>} [fields.schema] The Zod schema for the input of the tool. Infers the Zod type from the input type of the runnable.
2354
- * @returns {RunnableToolLike<z.ZodType<RunInput>, RunOutput>} An instance of `RunnableToolLike` which is a runnable that can be used as a tool.
2371
+ * @param {InteropZodType<RunInput>} [fields.schema] The Zod schema for the input of the tool. Infers the Zod type from the input type of the runnable.
2372
+ * @returns {RunnableToolLike<InteropZodType<RunInput>, RunOutput>} An instance of `RunnableToolLike` which is a runnable that can be used as a tool.
2355
2373
  */
2356
2374
  export function convertRunnableToTool(runnable, fields) {
2357
2375
  const name = fields.name ?? runnable.getName();
2358
- const description = fields.description ?? fields.schema?.description;
2359
- if (fields.schema.constructor === z.ZodString) {
2376
+ const description = fields.description ?? getSchemaDescription(fields.schema);
2377
+ if (isSimpleStringZodSchema(fields.schema)) {
2360
2378
  return new RunnableToolLike({
2361
2379
  name,
2362
2380
  description,
2363
2381
  schema: z
2364
- .object({
2365
- input: z.string(),
2366
- })
2382
+ .object({ input: z.string() })
2367
2383
  .transform((input) => input.input),
2368
2384
  bound: runnable,
2369
2385
  });
@@ -1,10 +1,10 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.Graph = void 0;
4
- const zod_to_json_schema_1 = require("zod-to-json-schema");
5
4
  const uuid_1 = require("uuid");
6
5
  const utils_js_1 = require("./utils.cjs");
7
6
  const graph_mermaid_js_1 = require("./graph_mermaid.cjs");
7
+ const json_schema_js_1 = require("../utils/json_schema.cjs");
8
8
  function nodeDataStr(id, data) {
9
9
  if (id !== undefined && !(0, uuid_1.validate)(id)) {
10
10
  return id;
@@ -39,7 +39,7 @@ function nodeDataJson(node) {
39
39
  else {
40
40
  return {
41
41
  type: "schema",
42
- data: { ...(0, zod_to_json_schema_1.zodToJsonSchema)(node.data.schema), title: node.data.name },
42
+ data: { ...(0, json_schema_js_1.toJsonSchema)(node.data.schema), title: node.data.name },
43
43
  };
44
44
  }
45
45
  }
@@ -1,7 +1,7 @@
1
- import { zodToJsonSchema } from "zod-to-json-schema";
2
1
  import { v4 as uuidv4, validate as isUuid } from "uuid";
3
2
  import { isRunnableInterface } from "./utils.js";
4
3
  import { drawMermaid, drawMermaidPng } from "./graph_mermaid.js";
4
+ import { toJsonSchema } from "../utils/json_schema.js";
5
5
  function nodeDataStr(id, data) {
6
6
  if (id !== undefined && !isUuid(id)) {
7
7
  return id;
@@ -36,7 +36,7 @@ function nodeDataJson(node) {
36
36
  else {
37
37
  return {
38
38
  type: "schema",
39
- data: { ...zodToJsonSchema(node.data.schema), title: node.data.name },
39
+ data: { ...toJsonSchema(node.data.schema), title: node.data.name },
40
40
  };
41
41
  }
42
42
  }
@@ -112,7 +112,7 @@ class RunnablePassthrough extends base_js_1.Runnable {
112
112
  * schema: async () => db.getTableInfo(),
113
113
  * }),
114
114
  * prompt,
115
- * new ChatOpenAI({}).bind({ stop: ["\nSQLResult:"] }),
115
+ * new ChatOpenAI({}).withConfig({ stop: ["\nSQLResult:"] }),
116
116
  * new StringOutputParser(),
117
117
  * ]);
118
118
  * const result = await sqlQueryGeneratorChain.invoke({
@@ -57,7 +57,7 @@ export declare class RunnablePassthrough<RunInput = any> extends Runnable<RunInp
57
57
  * schema: async () => db.getTableInfo(),
58
58
  * }),
59
59
  * prompt,
60
- * new ChatOpenAI({}).bind({ stop: ["\nSQLResult:"] }),
60
+ * new ChatOpenAI({}).withConfig({ stop: ["\nSQLResult:"] }),
61
61
  * new StringOutputParser(),
62
62
  * ]);
63
63
  * const result = await sqlQueryGeneratorChain.invoke({
@@ -109,7 +109,7 @@ export class RunnablePassthrough extends Runnable {
109
109
  * schema: async () => db.getTableInfo(),
110
110
  * }),
111
111
  * prompt,
112
- * new ChatOpenAI({}).bind({ stop: ["\nSQLResult:"] }),
112
+ * new ChatOpenAI({}).withConfig({ stop: ["\nSQLResult:"] }),
113
113
  * new StringOutputParser(),
114
114
  * ]);
115
115
  * const result = await sqlQueryGeneratorChain.invoke({
@@ -1,7 +1,7 @@
1
- import type { z } from "zod";
2
1
  import type { SerializableInterface } from "../load/serializable.js";
3
2
  import type { BaseCallbackConfig } from "../callbacks/manager.js";
4
3
  import type { IterableReadableStreamInterface } from "../types/_internal.js";
4
+ import { InteropZodType } from "../utils/types/zod.js";
5
5
  export type RunnableBatchOptions = {
6
6
  /** @deprecated Pass in via the standard runnable config object instead */
7
7
  maxConcurrency?: number;
@@ -9,7 +9,7 @@ export type RunnableBatchOptions = {
9
9
  };
10
10
  export type RunnableIOSchema = {
11
11
  name?: string;
12
- schema: z.ZodType;
12
+ schema: InteropZodType;
13
13
  };
14
14
  /**
15
15
  * Base interface implemented by all runnables.
@@ -1,7 +1,7 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.tool = exports.BaseToolkit = exports.DynamicStructuredTool = exports.DynamicTool = exports.Tool = exports.StructuredTool = exports.ToolInputParsingException = exports.isStructuredToolParams = exports.isStructuredTool = exports.isRunnableToolLike = exports.isLangChainTool = void 0;
4
- const zod_1 = require("zod");
4
+ const v3_1 = require("zod/v3");
5
5
  const json_schema_1 = require("@cfworker/json-schema");
6
6
  const manager_js_1 = require("../callbacks/manager.cjs");
7
7
  const base_js_1 = require("../language_models/base.cjs");
@@ -10,7 +10,7 @@ const tool_js_1 = require("../messages/tool.cjs");
10
10
  const index_js_1 = require("../singletons/index.cjs");
11
11
  const utils_js_1 = require("./utils.cjs");
12
12
  Object.defineProperty(exports, "ToolInputParsingException", { enumerable: true, get: function () { return utils_js_1.ToolInputParsingException; } });
13
- const is_zod_schema_js_1 = require("../utils/types/is_zod_schema.cjs");
13
+ const zod_js_1 = require("../utils/types/zod.cjs");
14
14
  const json_schema_js_1 = require("../utils/json_schema.cjs");
15
15
  var types_js_1 = require("./types.cjs");
16
16
  Object.defineProperty(exports, "isLangChainTool", { enumerable: true, get: function () { return types_js_1.isLangChainTool; } });
@@ -102,10 +102,10 @@ class StructuredTool extends base_js_1.BaseLangChain {
102
102
  // If arg is a ToolCall, use its args; otherwise, use arg directly.
103
103
  const inputForValidation = (0, utils_js_1._isToolCall)(arg) ? arg.args : arg;
104
104
  let parsed; // This will hold the successfully parsed input of the expected output type.
105
- if ((0, is_zod_schema_js_1.isZodSchema)(this.schema)) {
105
+ if ((0, zod_js_1.isInteropZodSchema)(this.schema)) {
106
106
  try {
107
107
  // Validate the inputForValidation - TS needs help here as it can't exclude ToolCall based on the check
108
- parsed = await this.schema.parseAsync(inputForValidation);
108
+ parsed = await (0, zod_js_1.interopParseAsync)(this.schema, inputForValidation);
109
109
  }
110
110
  catch (e) {
111
111
  let message = `Received tool input did not match expected schema`;
@@ -190,8 +190,8 @@ class Tool extends StructuredTool {
190
190
  enumerable: true,
191
191
  configurable: true,
192
192
  writable: true,
193
- value: zod_1.z
194
- .object({ input: zod_1.z.string().optional() })
193
+ value: v3_1.z
194
+ .object({ input: v3_1.z.string().optional() })
195
195
  .transform((obj) => obj.input)
196
196
  });
197
197
  }
@@ -342,12 +342,10 @@ class BaseToolkit {
342
342
  }
343
343
  exports.BaseToolkit = BaseToolkit;
344
344
  function tool(func, fields) {
345
- const isShapelessZodSchema = fields.schema &&
346
- (0, is_zod_schema_js_1.isZodSchema)(fields.schema) &&
347
- (!("shape" in fields.schema) || !fields.schema.shape);
345
+ const isSimpleStringSchema = (0, zod_js_1.isSimpleStringZodSchema)(fields.schema);
348
346
  const isStringJSONSchema = (0, json_schema_js_1.validatesOnlyStrings)(fields.schema);
349
- // If the schema is not provided, or it's a shapeless schema (e.g. a ZodString), create a DynamicTool
350
- if (!fields.schema || isShapelessZodSchema || isStringJSONSchema) {
347
+ // If the schema is not provided, or it's a simple string schema, create a DynamicTool
348
+ if (!fields.schema || isSimpleStringSchema || isStringJSONSchema) {
351
349
  return new DynamicTool({
352
350
  ...fields,
353
351
  description: fields.description ??
@@ -426,7 +424,7 @@ function _formatToolOutput(params) {
426
424
  }
427
425
  function _stringify(content) {
428
426
  try {
429
- return JSON.stringify(content, null, 2);
427
+ return JSON.stringify(content, null, 2) ?? "";
430
428
  }
431
429
  catch (_noOp) {
432
430
  return `${content}`;
@@ -1,11 +1,12 @@
1
- import { z } from "zod";
1
+ import { z } from "zod/v3";
2
2
  import { CallbackManagerForToolRun } from "../callbacks/manager.js";
3
3
  import { BaseLangChain } from "../language_models/base.js";
4
4
  import { type RunnableConfig } from "../runnables/config.js";
5
5
  import type { RunnableFunc } from "../runnables/base.js";
6
6
  import { ToolCall, ToolMessage } from "../messages/tool.js";
7
7
  import { ToolInputParsingException } from "./utils.js";
8
- import type { StructuredToolCallInput, ToolInputSchemaBase, ToolReturnType, ResponseFormat, ToolInputSchemaInputType, ToolInputSchemaOutputType, ToolParams, ToolRunnableConfig, StructuredToolInterface, DynamicToolInput, DynamicStructuredToolInput, ZodObjectAny, StringInputToolSchema, ToolInterface, ToolOutputType } from "./types.js";
8
+ import { type InferInteropZodInput, type InferInteropZodOutput, type ZodStringV3, type ZodStringV4, type ZodObjectV3, type ZodObjectV4 } from "../utils/types/zod.js";
9
+ import type { StructuredToolCallInput, ToolInputSchemaBase, ToolReturnType, ResponseFormat, ToolInputSchemaInputType, ToolInputSchemaOutputType, ToolParams, ToolRunnableConfig, StructuredToolInterface, DynamicToolInput, DynamicStructuredToolInput, StringInputToolSchema, ToolInterface, ToolOutputType } from "./types.js";
9
10
  import { type JSONSchema } from "../utils/json_schema.js";
10
11
  export type { BaseDynamicToolInput, ContentAndArtifact, DynamicToolInput, DynamicStructuredToolInput, ResponseFormat, StructuredToolCallInput, StructuredToolInterface, StructuredToolParams, ToolInterface, ToolParams, ToolReturnType, ToolRunnableConfig, ToolInputSchemaBase as ToolSchemaBase, } from "./types.js";
11
12
  export { isLangChainTool, isRunnableToolLike, isStructuredTool, isStructuredToolParams, } from "./types.js";
@@ -13,7 +14,7 @@ export { ToolInputParsingException };
13
14
  /**
14
15
  * Base class for Tools that accept input of any shape defined by a Zod schema.
15
16
  */
16
- export declare abstract class StructuredTool<SchemaT extends ToolInputSchemaBase = ToolInputSchemaBase, SchemaOutputT = ToolInputSchemaOutputType<SchemaT>, SchemaInputT = ToolInputSchemaInputType<SchemaT>, ToolOutputT = ToolOutputType> extends BaseLangChain<StructuredToolCallInput<SchemaT, SchemaInputT>, ToolOutputT | ToolMessage> implements StructuredToolInterface<SchemaT, SchemaInputT, ToolOutputT> {
17
+ export declare abstract class StructuredTool<SchemaT = ToolInputSchemaBase, SchemaOutputT = ToolInputSchemaOutputType<SchemaT>, SchemaInputT = ToolInputSchemaInputType<SchemaT>, ToolOutputT = ToolOutputType> extends BaseLangChain<StructuredToolCallInput<SchemaT, SchemaInputT>, ToolOutputT | ToolMessage> implements StructuredToolInterface<SchemaT, SchemaInputT, ToolOutputT> {
17
18
  abstract name: string;
18
19
  abstract description: string;
19
20
  abstract schema: SchemaT;
@@ -111,7 +112,7 @@ export declare class DynamicTool<ToolOutputT = ToolOutputType> extends Tool<Tool
111
112
  * Schema can be passed as Zod or JSON schema. The tool will not validate
112
113
  * input if JSON schema is passed.
113
114
  */
114
- export declare class DynamicStructuredTool<SchemaT extends ToolInputSchemaBase = ToolInputSchemaBase, SchemaOutputT = ToolInputSchemaOutputType<SchemaT>, SchemaInputT = ToolInputSchemaInputType<SchemaT>, ToolOutputT = ToolOutputType> extends StructuredTool<SchemaT, SchemaOutputT, SchemaInputT, ToolOutputT> {
115
+ export declare class DynamicStructuredTool<SchemaT = ToolInputSchemaBase, SchemaOutputT = ToolInputSchemaOutputType<SchemaT>, SchemaInputT = ToolInputSchemaInputType<SchemaT>, ToolOutputT = ToolOutputType> extends StructuredTool<SchemaT, SchemaOutputT, SchemaInputT, ToolOutputT> {
115
116
  static lc_name(): string;
116
117
  name: string;
117
118
  description: string;
@@ -141,7 +142,7 @@ export declare abstract class BaseToolkit {
141
142
  * Both schema types will be validated.
142
143
  * @template {ToolInputSchemaBase} RunInput The input schema for the tool.
143
144
  */
144
- interface ToolWrapperParams<RunInput extends ToolInputSchemaBase | undefined = ToolInputSchemaBase | undefined> extends ToolParams {
145
+ interface ToolWrapperParams<RunInput = ToolInputSchemaBase | undefined> extends ToolParams {
145
146
  /**
146
147
  * The name of the tool. If using with an LLM, this
147
148
  * will be passed as the tool name.
@@ -189,10 +190,12 @@ interface ToolWrapperParams<RunInput extends ToolInputSchemaBase | undefined = T
189
190
  * @param {ToolWrapperParams<SchemaT>} fields - An object containing the following properties:
190
191
  * @param {string} fields.name The name of the tool.
191
192
  * @param {string | undefined} fields.description The description of the tool. Defaults to either the description on the Zod schema, or `${fields.name} tool`.
192
- * @param {ZodObjectAny | z.ZodString | undefined} fields.schema The Zod schema defining the input for the tool. If undefined, it will default to a Zod string schema.
193
+ * @param {z.AnyZodObject | z.ZodString | undefined} fields.schema The Zod schema defining the input for the tool. If undefined, it will default to a Zod string schema.
193
194
  *
194
195
  * @returns {DynamicStructuredTool<SchemaT>} A new StructuredTool instance.
195
196
  */
196
- export declare function tool<SchemaT extends z.ZodString, ToolOutputT = ToolOutputType>(func: RunnableFunc<SchemaT extends z.ZodString ? z.output<SchemaT> : string, ToolOutputT, ToolRunnableConfig>, fields: ToolWrapperParams<SchemaT>): DynamicTool<ToolOutputT>;
197
- export declare function tool<SchemaT extends ZodObjectAny, SchemaOutputT = z.output<SchemaT>, SchemaInputT = z.input<SchemaT>, ToolOutputT = ToolOutputType>(func: RunnableFunc<SchemaOutputT, ToolOutputT, ToolRunnableConfig>, fields: ToolWrapperParams<SchemaT>): DynamicStructuredTool<SchemaT, SchemaOutputT, SchemaInputT, ToolOutputT>;
197
+ export declare function tool<SchemaT extends ZodStringV3, ToolOutputT = ToolOutputType>(func: RunnableFunc<InferInteropZodOutput<SchemaT>, ToolOutputT, ToolRunnableConfig>, fields: ToolWrapperParams<SchemaT>): DynamicTool<ToolOutputT>;
198
+ export declare function tool<SchemaT extends ZodStringV4, ToolOutputT = ToolOutputType>(func: RunnableFunc<InferInteropZodOutput<SchemaT>, ToolOutputT, ToolRunnableConfig>, fields: ToolWrapperParams<SchemaT>): DynamicTool<ToolOutputT>;
199
+ export declare function tool<SchemaT extends ZodObjectV3, SchemaOutputT = InferInteropZodOutput<SchemaT>, SchemaInputT = InferInteropZodInput<SchemaT>, ToolOutputT = ToolOutputType>(func: RunnableFunc<SchemaOutputT, ToolOutputT, ToolRunnableConfig>, fields: ToolWrapperParams<SchemaT>): DynamicStructuredTool<SchemaT, SchemaOutputT, SchemaInputT, ToolOutputT>;
200
+ export declare function tool<SchemaT extends ZodObjectV4, SchemaOutputT = InferInteropZodOutput<SchemaT>, SchemaInputT = InferInteropZodInput<SchemaT>, ToolOutputT = ToolOutputType>(func: RunnableFunc<SchemaOutputT, ToolOutputT, ToolRunnableConfig>, fields: ToolWrapperParams<SchemaT>): DynamicStructuredTool<SchemaT, SchemaOutputT, SchemaInputT, ToolOutputT>;
198
201
  export declare function tool<SchemaT extends JSONSchema, SchemaOutputT = ToolInputSchemaOutputType<SchemaT>, SchemaInputT = ToolInputSchemaInputType<SchemaT>, ToolOutputT = ToolOutputType>(func: RunnableFunc<Parameters<DynamicStructuredToolInput<SchemaT>["func"]>[0], ToolOutputT, ToolRunnableConfig>, fields: ToolWrapperParams<SchemaT>): DynamicStructuredTool<SchemaT, SchemaOutputT, SchemaInputT, ToolOutputT>;