@langchain/core 0.3.66 → 0.3.68

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.
@@ -144,10 +144,11 @@ class AIMessageChunk extends base_js_1.BaseMessageChunk {
144
144
  }
145
145
  else {
146
146
  const groupedToolCallChunk = fields.tool_call_chunks.reduce((acc, chunk) => {
147
- if (!chunk.id)
148
- return acc;
149
- acc[chunk.id] = acc[chunk.id] ?? [];
150
- acc[chunk.id].push(chunk);
147
+ // Assign a fallback ID if the chunk doesn't have one
148
+ // This can happen with tools that have empty schemas
149
+ const chunkId = chunk.id || `fallback-${chunk.index || 0}`;
150
+ acc[chunkId] = acc[chunkId] ?? [];
151
+ acc[chunkId].push(chunk);
151
152
  return acc;
152
153
  }, {});
153
154
  const toolCalls = [];
@@ -157,6 +158,8 @@ class AIMessageChunk extends base_js_1.BaseMessageChunk {
157
158
  const name = chunks[0]?.name ?? "";
158
159
  const joinedArgs = chunks.map((c) => c.args || "").join("");
159
160
  const argsStr = joinedArgs.length ? joinedArgs : "{}";
161
+ // Use the original ID from the first chunk if it exists, otherwise use the grouped ID
162
+ const originalId = chunks[0]?.id || id;
160
163
  try {
161
164
  parsedArgs = (0, json_js_1.parsePartialJson)(argsStr);
162
165
  if (parsedArgs === null ||
@@ -167,7 +170,7 @@ class AIMessageChunk extends base_js_1.BaseMessageChunk {
167
170
  toolCalls.push({
168
171
  name,
169
172
  args: parsedArgs,
170
- id,
173
+ id: originalId,
171
174
  type: "tool_call",
172
175
  });
173
176
  }
@@ -175,7 +178,7 @@ class AIMessageChunk extends base_js_1.BaseMessageChunk {
175
178
  invalidToolCalls.push({
176
179
  name,
177
180
  args: argsStr,
178
- id,
181
+ id: originalId,
179
182
  error: "Malformed args.",
180
183
  type: "invalid_tool_call",
181
184
  });
@@ -138,10 +138,11 @@ export class AIMessageChunk extends BaseMessageChunk {
138
138
  }
139
139
  else {
140
140
  const groupedToolCallChunk = fields.tool_call_chunks.reduce((acc, chunk) => {
141
- if (!chunk.id)
142
- return acc;
143
- acc[chunk.id] = acc[chunk.id] ?? [];
144
- acc[chunk.id].push(chunk);
141
+ // Assign a fallback ID if the chunk doesn't have one
142
+ // This can happen with tools that have empty schemas
143
+ const chunkId = chunk.id || `fallback-${chunk.index || 0}`;
144
+ acc[chunkId] = acc[chunkId] ?? [];
145
+ acc[chunkId].push(chunk);
145
146
  return acc;
146
147
  }, {});
147
148
  const toolCalls = [];
@@ -151,6 +152,8 @@ export class AIMessageChunk extends BaseMessageChunk {
151
152
  const name = chunks[0]?.name ?? "";
152
153
  const joinedArgs = chunks.map((c) => c.args || "").join("");
153
154
  const argsStr = joinedArgs.length ? joinedArgs : "{}";
155
+ // Use the original ID from the first chunk if it exists, otherwise use the grouped ID
156
+ const originalId = chunks[0]?.id || id;
154
157
  try {
155
158
  parsedArgs = parsePartialJson(argsStr);
156
159
  if (parsedArgs === null ||
@@ -161,7 +164,7 @@ export class AIMessageChunk extends BaseMessageChunk {
161
164
  toolCalls.push({
162
165
  name,
163
166
  args: parsedArgs,
164
- id,
167
+ id: originalId,
165
168
  type: "tool_call",
166
169
  });
167
170
  }
@@ -169,7 +172,7 @@ export class AIMessageChunk extends BaseMessageChunk {
169
172
  invalidToolCalls.push({
170
173
  name,
171
174
  args: argsStr,
172
- id,
175
+ id: originalId,
173
176
  error: "Malformed args.",
174
177
  type: "invalid_tool_call",
175
178
  });
@@ -51,6 +51,12 @@ class ToolMessage extends base_js_1.BaseMessage {
51
51
  writable: true,
52
52
  value: void 0
53
53
  });
54
+ Object.defineProperty(this, "metadata", {
55
+ enumerable: true,
56
+ configurable: true,
57
+ writable: true,
58
+ value: void 0
59
+ });
54
60
  /**
55
61
  * Artifact of the Tool execution which is not meant to be sent to the model.
56
62
  *
@@ -68,6 +74,7 @@ class ToolMessage extends base_js_1.BaseMessage {
68
74
  this.tool_call_id = fields.tool_call_id;
69
75
  this.artifact = fields.artifact;
70
76
  this.status = fields.status;
77
+ this.metadata = fields.metadata;
71
78
  }
72
79
  _getType() {
73
80
  return "tool";
@@ -18,6 +18,7 @@ export interface ToolMessageFieldsWithToolCallId extends ToolMessageFields {
18
18
  * @version 0.2.19
19
19
  */
20
20
  status?: "success" | "error";
21
+ metadata?: Record<string, unknown>;
21
22
  }
22
23
  /**
23
24
  * Marker parameter for objects that tools can return directly.
@@ -44,6 +45,7 @@ export declare class ToolMessage extends BaseMessage implements DirectToolOutput
44
45
  */
45
46
  status?: "success" | "error";
46
47
  tool_call_id: string;
48
+ metadata?: Record<string, unknown>;
47
49
  /**
48
50
  * Artifact of the Tool execution which is not meant to be sent to the model.
49
51
  *
@@ -44,6 +44,12 @@ export class ToolMessage extends BaseMessage {
44
44
  writable: true,
45
45
  value: void 0
46
46
  });
47
+ Object.defineProperty(this, "metadata", {
48
+ enumerable: true,
49
+ configurable: true,
50
+ writable: true,
51
+ value: void 0
52
+ });
47
53
  /**
48
54
  * Artifact of the Tool execution which is not meant to be sent to the model.
49
55
  *
@@ -61,6 +67,7 @@ export class ToolMessage extends BaseMessage {
61
67
  this.tool_call_id = fields.tool_call_id;
62
68
  this.artifact = fields.artifact;
63
69
  this.status = fields.status;
70
+ this.metadata = fields.metadata;
64
71
  }
65
72
  _getType() {
66
73
  return "tool";
@@ -67,20 +67,29 @@ exports.parseFString = parseFString;
67
67
  * @param {mustache.TemplateSpans} template The result of parsing a mustache template with the mustache.js library.
68
68
  * @returns {ParsedTemplateNode[]}
69
69
  */
70
- const mustacheTemplateToNodes = (template) => template.map((temp) => {
71
- if (temp[0] === "name") {
72
- const name = temp[1].includes(".") ? temp[1].split(".")[0] : temp[1];
73
- return { type: "variable", name };
74
- }
75
- else if (["#", "&", "^", ">"].includes(temp[0])) {
76
- // # represents a section, "&" represents an unescaped variable.
77
- // These should both be considered variables.
78
- return { type: "variable", name: temp[1] };
79
- }
80
- else {
81
- return { type: "literal", text: temp[1] };
70
+ const mustacheTemplateToNodes = (template) => {
71
+ const nodes = [];
72
+ for (const temp of template) {
73
+ if (temp[0] === "name") {
74
+ const name = temp[1].includes(".") ? temp[1].split(".")[0] : temp[1];
75
+ nodes.push({ type: "variable", name });
76
+ }
77
+ else if (["#", "&", "^", ">"].includes(temp[0])) {
78
+ // # represents a section, "&" represents an unescaped variable.
79
+ // These should both be considered variables.
80
+ nodes.push({ type: "variable", name: temp[1] });
81
+ // If this is a section with nested content, recursively process it
82
+ if (temp[0] === "#" && temp.length > 4 && Array.isArray(temp[4])) {
83
+ const nestedNodes = mustacheTemplateToNodes(temp[4]);
84
+ nodes.push(...nestedNodes);
85
+ }
86
+ }
87
+ else {
88
+ nodes.push({ type: "literal", text: temp[1] });
89
+ }
82
90
  }
83
- });
91
+ return nodes;
92
+ };
84
93
  const parseMustache = (template) => {
85
94
  configureMustache();
86
95
  const parsed = mustache_1.default.parse(template);
@@ -60,20 +60,29 @@ export const parseFString = (template) => {
60
60
  * @param {mustache.TemplateSpans} template The result of parsing a mustache template with the mustache.js library.
61
61
  * @returns {ParsedTemplateNode[]}
62
62
  */
63
- const mustacheTemplateToNodes = (template) => template.map((temp) => {
64
- if (temp[0] === "name") {
65
- const name = temp[1].includes(".") ? temp[1].split(".")[0] : temp[1];
66
- return { type: "variable", name };
67
- }
68
- else if (["#", "&", "^", ">"].includes(temp[0])) {
69
- // # represents a section, "&" represents an unescaped variable.
70
- // These should both be considered variables.
71
- return { type: "variable", name: temp[1] };
72
- }
73
- else {
74
- return { type: "literal", text: temp[1] };
63
+ const mustacheTemplateToNodes = (template) => {
64
+ const nodes = [];
65
+ for (const temp of template) {
66
+ if (temp[0] === "name") {
67
+ const name = temp[1].includes(".") ? temp[1].split(".")[0] : temp[1];
68
+ nodes.push({ type: "variable", name });
69
+ }
70
+ else if (["#", "&", "^", ">"].includes(temp[0])) {
71
+ // # represents a section, "&" represents an unescaped variable.
72
+ // These should both be considered variables.
73
+ nodes.push({ type: "variable", name: temp[1] });
74
+ // If this is a section with nested content, recursively process it
75
+ if (temp[0] === "#" && temp.length > 4 && Array.isArray(temp[4])) {
76
+ const nestedNodes = mustacheTemplateToNodes(temp[4]);
77
+ nodes.push(...nestedNodes);
78
+ }
79
+ }
80
+ else {
81
+ nodes.push({ type: "literal", text: temp[1] });
82
+ }
75
83
  }
76
- });
84
+ return nodes;
85
+ };
77
86
  export const parseMustache = (template) => {
78
87
  configureMustache();
79
88
  const parsed = mustache.parse(template);
@@ -1881,9 +1881,9 @@ exports.RunnableLambda = RunnableLambda;
1881
1881
  * );
1882
1882
  *
1883
1883
  * // Invoke the sequence with a single age input
1884
- * const res = sequence.invoke(25);
1884
+ * const res = await sequence.invoke(25);
1885
1885
  *
1886
- * // { years_to_fifty: 25, years_to_hundred: 75 }
1886
+ * // { years_to_fifty: 20, years_to_hundred: 70 }
1887
1887
  * ```
1888
1888
  */
1889
1889
  class RunnableParallel extends RunnableMap {
@@ -729,9 +729,9 @@ export declare class RunnableLambda<RunInput, RunOutput, CallOptions extends Run
729
729
  * );
730
730
  *
731
731
  * // Invoke the sequence with a single age input
732
- * const res = sequence.invoke(25);
732
+ * const res = await sequence.invoke(25);
733
733
  *
734
- * // { years_to_fifty: 25, years_to_hundred: 75 }
734
+ * // { years_to_fifty: 20, years_to_hundred: 70 }
735
735
  * ```
736
736
  */
737
737
  export declare class RunnableParallel<RunInput> extends RunnableMap<RunInput> {
@@ -1864,9 +1864,9 @@ export class RunnableLambda extends Runnable {
1864
1864
  * );
1865
1865
  *
1866
1866
  * // Invoke the sequence with a single age input
1867
- * const res = sequence.invoke(25);
1867
+ * const res = await sequence.invoke(25);
1868
1868
  *
1869
- * // { years_to_fifty: 25, years_to_hundred: 75 }
1869
+ * // { years_to_fifty: 20, years_to_hundred: 70 }
1870
1870
  * ```
1871
1871
  */
1872
1872
  export class RunnableParallel extends RunnableMap {
@@ -73,6 +73,7 @@ class StructuredTool extends base_js_1.BaseLangChain {
73
73
  fields?.verboseParsingErrors ?? this.verboseParsingErrors;
74
74
  this.responseFormat = fields?.responseFormat ?? this.responseFormat;
75
75
  this.defaultConfig = fields?.defaultConfig ?? this.defaultConfig;
76
+ this.metadata = fields?.metadata ?? this.metadata;
76
77
  }
77
78
  /**
78
79
  * Invokes the tool with the provided input and configuration.
@@ -185,6 +186,7 @@ class StructuredTool extends base_js_1.BaseLangChain {
185
186
  artifact,
186
187
  toolCallId,
187
188
  name: this.name,
189
+ metadata: this.metadata,
188
190
  });
189
191
  await runManager?.handleToolEnd(formattedOutput);
190
192
  return formattedOutput;
@@ -360,7 +362,7 @@ function tool(func, fields) {
360
362
  return new DynamicTool({
361
363
  ...fields,
362
364
  description: fields.description ??
363
- fields.schema?.description ??
365
+ (fields.schema && (0, zod_js_1.getSchemaDescription)(fields.schema)) ??
364
366
  `${fields.name} tool`,
365
367
  func: async (input, runManager, config) => {
366
368
  return new Promise((resolve, reject) => {
@@ -407,7 +409,7 @@ function tool(func, fields) {
407
409
  });
408
410
  }
409
411
  function _formatToolOutput(params) {
410
- const { content, artifact, toolCallId } = params;
412
+ const { content, artifact, toolCallId, metadata } = params;
411
413
  if (toolCallId && !(0, tool_js_1.isDirectToolOutput)(content)) {
412
414
  if (typeof content === "string" ||
413
415
  (Array.isArray(content) &&
@@ -417,6 +419,7 @@ function _formatToolOutput(params) {
417
419
  artifact,
418
420
  tool_call_id: toolCallId,
419
421
  name: params.name,
422
+ metadata,
420
423
  });
421
424
  }
422
425
  else {
@@ -425,6 +428,7 @@ function _formatToolOutput(params) {
425
428
  artifact,
426
429
  tool_call_id: toolCallId,
427
430
  name: params.name,
431
+ metadata,
428
432
  });
429
433
  }
430
434
  }
@@ -6,7 +6,7 @@ import { mergeConfigs, ensureConfig, patchConfig, pickRunnableConfigKeys, } from
6
6
  import { isDirectToolOutput, ToolMessage } from "../messages/tool.js";
7
7
  import { AsyncLocalStorageProviderSingleton } from "../singletons/index.js";
8
8
  import { _configHasToolCallId, _isToolCall, ToolInputParsingException, } from "./utils.js";
9
- import { interopParseAsync, isSimpleStringZodSchema, isInteropZodSchema, } from "../utils/types/zod.js";
9
+ import { interopParseAsync, isSimpleStringZodSchema, isInteropZodSchema, getSchemaDescription, } from "../utils/types/zod.js";
10
10
  import { validatesOnlyStrings } from "../utils/json_schema.js";
11
11
  export { isLangChainTool, isRunnableToolLike, isStructuredTool, isStructuredToolParams, } from "./types.js";
12
12
  export { ToolInputParsingException };
@@ -65,6 +65,7 @@ export class StructuredTool extends BaseLangChain {
65
65
  fields?.verboseParsingErrors ?? this.verboseParsingErrors;
66
66
  this.responseFormat = fields?.responseFormat ?? this.responseFormat;
67
67
  this.defaultConfig = fields?.defaultConfig ?? this.defaultConfig;
68
+ this.metadata = fields?.metadata ?? this.metadata;
68
69
  }
69
70
  /**
70
71
  * Invokes the tool with the provided input and configuration.
@@ -177,6 +178,7 @@ export class StructuredTool extends BaseLangChain {
177
178
  artifact,
178
179
  toolCallId,
179
180
  name: this.name,
181
+ metadata: this.metadata,
180
182
  });
181
183
  await runManager?.handleToolEnd(formattedOutput);
182
184
  return formattedOutput;
@@ -347,7 +349,7 @@ export function tool(func, fields) {
347
349
  return new DynamicTool({
348
350
  ...fields,
349
351
  description: fields.description ??
350
- fields.schema?.description ??
352
+ (fields.schema && getSchemaDescription(fields.schema)) ??
351
353
  `${fields.name} tool`,
352
354
  func: async (input, runManager, config) => {
353
355
  return new Promise((resolve, reject) => {
@@ -394,7 +396,7 @@ export function tool(func, fields) {
394
396
  });
395
397
  }
396
398
  function _formatToolOutput(params) {
397
- const { content, artifact, toolCallId } = params;
399
+ const { content, artifact, toolCallId, metadata } = params;
398
400
  if (toolCallId && !isDirectToolOutput(content)) {
399
401
  if (typeof content === "string" ||
400
402
  (Array.isArray(content) &&
@@ -404,6 +406,7 @@ function _formatToolOutput(params) {
404
406
  artifact,
405
407
  tool_call_id: toolCallId,
406
408
  name: params.name,
409
+ metadata,
407
410
  });
408
411
  }
409
412
  else {
@@ -412,6 +415,7 @@ function _formatToolOutput(params) {
412
415
  artifact,
413
416
  tool_call_id: toolCallId,
414
417
  name: params.name,
418
+ metadata,
415
419
  });
416
420
  }
417
421
  }
@@ -58,6 +58,10 @@ export interface ToolParams extends BaseLangChainParams {
58
58
  * @default false
59
59
  */
60
60
  verboseParsingErrors?: boolean;
61
+ /**
62
+ * Metadata for the tool.
63
+ */
64
+ metadata?: Record<string, unknown>;
61
65
  }
62
66
  export type ToolRunnableConfig<ConfigurableFieldType extends Record<string, any> = Record<string, any>> = RunnableConfig<ConfigurableFieldType> & {
63
67
  toolCall?: ToolCall;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@langchain/core",
3
- "version": "0.3.66",
3
+ "version": "0.3.68",
4
4
  "description": "Core LangChain.js abstractions and schemas",
5
5
  "type": "module",
6
6
  "engines": {