@langchain/core 0.3.11 → 0.3.13

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.
@@ -0,0 +1,11 @@
1
+ "use strict";
2
+ /* eslint-disable @typescript-eslint/no-explicit-any */
3
+ /* eslint-disable no-param-reassign */
4
+ Object.defineProperty(exports, "__esModule", { value: true });
5
+ exports.addLangChainErrorFields = void 0;
6
+ function addLangChainErrorFields(error, lc_error_code) {
7
+ error.lc_error_code = lc_error_code;
8
+ error.message = `${error.message}\n\nTroubleshooting URL: https://js.langchain.com/docs/troubleshooting/errors/${lc_error_code}/\n`;
9
+ return error;
10
+ }
11
+ exports.addLangChainErrorFields = addLangChainErrorFields;
@@ -0,0 +1,2 @@
1
+ export type LangChainErrorCodes = "INVALID_PROMPT_INPUT" | "INVALID_TOOL_RESULTS" | "MESSAGE_COERCION_FAILURE" | "MODEL_AUTHENTICATION" | "MODEL_NOT_FOUND" | "MODEL_RATE_LIMIT" | "OUTPUT_PARSING_FAILURE";
2
+ export declare function addLangChainErrorFields(error: any, lc_error_code: LangChainErrorCodes): any;
@@ -0,0 +1,7 @@
1
+ /* eslint-disable @typescript-eslint/no-explicit-any */
2
+ /* eslint-disable no-param-reassign */
3
+ export function addLangChainErrorFields(error, lc_error_code) {
4
+ error.lc_error_code = lc_error_code;
5
+ error.message = `${error.message}\n\nTroubleshooting URL: https://js.langchain.com/docs/troubleshooting/errors/${lc_error_code}/\n`;
6
+ return error;
7
+ }
@@ -1,6 +1,7 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.convertToChunk = exports.mapChatMessagesToStoredMessages = exports.mapStoredMessagesToChatMessages = exports.mapStoredMessageToChatMessage = exports.getBufferString = exports.coerceMessageLikeToMessage = void 0;
4
+ const index_js_1 = require("../errors/index.cjs");
4
5
  const utils_js_1 = require("../tools/utils.cjs");
5
6
  const ai_js_1 = require("./ai.cjs");
6
7
  const base_js_1 = require("./base.cjs");
@@ -91,7 +92,8 @@ function _constructMessageFromParams(params) {
91
92
  });
92
93
  }
93
94
  else {
94
- throw new Error(`Unable to coerce message from array: only human, AI, or system message coercion is currently supported.\n\nReceived: ${JSON.stringify(params, null, 2)}`);
95
+ const error = (0, index_js_1.addLangChainErrorFields)(new Error(`Unable to coerce message from array: only human, AI, system, or tool message coercion is currently supported.\n\nReceived: ${JSON.stringify(params, null, 2)}`), "MESSAGE_COERCION_FAILURE");
96
+ throw error;
95
97
  }
96
98
  }
97
99
  function coerceMessageLikeToMessage(messageLike) {
@@ -1,3 +1,4 @@
1
+ import { addLangChainErrorFields } from "../errors/index.js";
1
2
  import { _isToolCall } from "../tools/utils.js";
2
3
  import { AIMessage, AIMessageChunk } from "./ai.js";
3
4
  import { isBaseMessage, _isMessageFieldWithRole, } from "./base.js";
@@ -88,7 +89,8 @@ function _constructMessageFromParams(params) {
88
89
  });
89
90
  }
90
91
  else {
91
- throw new Error(`Unable to coerce message from array: only human, AI, or system message coercion is currently supported.\n\nReceived: ${JSON.stringify(params, null, 2)}`);
92
+ const error = addLangChainErrorFields(new Error(`Unable to coerce message from array: only human, AI, system, or tool message coercion is currently supported.\n\nReceived: ${JSON.stringify(params, null, 2)}`), "MESSAGE_COERCION_FAILURE");
93
+ throw error;
92
94
  }
93
95
  }
94
96
  export function coerceMessageLikeToMessage(messageLike) {
@@ -2,6 +2,7 @@
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.OutputParserException = exports.BaseOutputParser = exports.BaseLLMOutputParser = void 0;
4
4
  const index_js_1 = require("../runnables/index.cjs");
5
+ const index_js_2 = require("../errors/index.cjs");
5
6
  /**
6
7
  * Abstract base class for parsing the output of a Large Language Model
7
8
  * (LLM) call. It provides methods for parsing the result of an LLM call
@@ -117,6 +118,7 @@ class OutputParserException extends Error {
117
118
  throw new Error("Arguments 'observation' & 'llmOutput' are required if 'sendToLlm' is true");
118
119
  }
119
120
  }
121
+ (0, index_js_2.addLangChainErrorFields)(this, "OUTPUT_PARSING_FAILURE");
120
122
  }
121
123
  }
122
124
  exports.OutputParserException = OutputParserException;
@@ -1,4 +1,5 @@
1
1
  import { Runnable } from "../runnables/index.js";
2
+ import { addLangChainErrorFields } from "../errors/index.js";
2
3
  /**
3
4
  * Abstract base class for parsing the output of a Large Language Model
4
5
  * (LLM) call. It provides methods for parsing the result of an LLM call
@@ -112,5 +113,6 @@ export class OutputParserException extends Error {
112
113
  throw new Error("Arguments 'observation' & 'llmOutput' are required if 'sendToLlm' is true");
113
114
  }
114
115
  }
116
+ addLangChainErrorFields(this, "OUTPUT_PARSING_FAILURE");
115
117
  }
116
118
  }
@@ -11,6 +11,7 @@ const base_js_2 = require("./base.cjs");
11
11
  const prompt_js_1 = require("./prompt.cjs");
12
12
  const image_js_1 = require("./image.cjs");
13
13
  const template_js_1 = require("./template.cjs");
14
+ const index_js_2 = require("../errors/index.cjs");
14
15
  /**
15
16
  * Abstract class that serves as a base for creating message prompt
16
17
  * templates. It defines how to format messages for different roles in a
@@ -103,6 +104,8 @@ class MessagesPlaceholder extends BaseMessagePromptTemplate {
103
104
  `Additional message: ${e.message}`,
104
105
  ].join("\n\n"));
105
106
  error.name = "InputFormatError";
107
+ // eslint-disable-next-line @typescript-eslint/no-explicit-any
108
+ error.lc_error_code = e.lc_error_code;
106
109
  throw error;
107
110
  }
108
111
  return formattedMessages;
@@ -674,7 +677,8 @@ class ChatPromptTemplate extends BaseChatPromptTemplate {
674
677
  const inputValues = promptMessage.inputVariables.reduce((acc, inputVariable) => {
675
678
  if (!(inputVariable in allValues) &&
676
679
  !(isMessagesPlaceholder(promptMessage) && promptMessage.optional)) {
677
- throw new Error(`Missing value for input variable \`${inputVariable.toString()}\``);
680
+ const error = (0, index_js_2.addLangChainErrorFields)(new Error(`Missing value for input variable \`${inputVariable.toString()}\``), "INVALID_PROMPT_INPUT");
681
+ throw error;
678
682
  }
679
683
  acc[inputVariable] = allValues[inputVariable];
680
684
  return acc;
@@ -8,6 +8,7 @@ import { BasePromptTemplate, } from "./base.js";
8
8
  import { PromptTemplate, } from "./prompt.js";
9
9
  import { ImagePromptTemplate } from "./image.js";
10
10
  import { parseFString, parseMustache, } from "./template.js";
11
+ import { addLangChainErrorFields } from "../errors/index.js";
11
12
  /**
12
13
  * Abstract class that serves as a base for creating message prompt
13
14
  * templates. It defines how to format messages for different roles in a
@@ -99,6 +100,8 @@ export class MessagesPlaceholder extends BaseMessagePromptTemplate {
99
100
  `Additional message: ${e.message}`,
100
101
  ].join("\n\n"));
101
102
  error.name = "InputFormatError";
103
+ // eslint-disable-next-line @typescript-eslint/no-explicit-any
104
+ error.lc_error_code = e.lc_error_code;
102
105
  throw error;
103
106
  }
104
107
  return formattedMessages;
@@ -663,7 +666,8 @@ export class ChatPromptTemplate extends BaseChatPromptTemplate {
663
666
  const inputValues = promptMessage.inputVariables.reduce((acc, inputVariable) => {
664
667
  if (!(inputVariable in allValues) &&
665
668
  !(isMessagesPlaceholder(promptMessage) && promptMessage.optional)) {
666
- throw new Error(`Missing value for input variable \`${inputVariable.toString()}\``);
669
+ const error = addLangChainErrorFields(new Error(`Missing value for input variable \`${inputVariable.toString()}\``), "INVALID_PROMPT_INPUT");
670
+ throw error;
667
671
  }
668
672
  acc[inputVariable] = allValues[inputVariable];
669
673
  return acc;
@@ -5,6 +5,7 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
5
5
  Object.defineProperty(exports, "__esModule", { value: true });
6
6
  exports.checkValidTemplate = exports.parseTemplate = exports.renderTemplate = exports.DEFAULT_PARSER_MAPPING = exports.DEFAULT_FORMATTER_MAPPING = exports.interpolateMustache = exports.interpolateFString = exports.parseMustache = exports.parseFString = void 0;
7
7
  const mustache_1 = __importDefault(require("mustache"));
8
+ const index_js_1 = require("../errors/index.cjs");
8
9
  function configureMustache() {
9
10
  // Use unescaped HTML
10
11
  // https://github.com/janl/mustache.js?tab=readme-ov-file#variables
@@ -114,7 +115,15 @@ exports.DEFAULT_PARSER_MAPPING = {
114
115
  "f-string": exports.parseFString,
115
116
  mustache: exports.parseMustache,
116
117
  };
117
- const renderTemplate = (template, templateFormat, inputValues) => exports.DEFAULT_FORMATTER_MAPPING[templateFormat](template, inputValues);
118
+ const renderTemplate = (template, templateFormat, inputValues) => {
119
+ try {
120
+ return exports.DEFAULT_FORMATTER_MAPPING[templateFormat](template, inputValues);
121
+ }
122
+ catch (e) {
123
+ const error = (0, index_js_1.addLangChainErrorFields)(e, "INVALID_PROMPT_INPUT");
124
+ throw error;
125
+ }
126
+ };
118
127
  exports.renderTemplate = renderTemplate;
119
128
  const parseTemplate = (template, templateFormat) => exports.DEFAULT_PARSER_MAPPING[templateFormat](template);
120
129
  exports.parseTemplate = parseTemplate;
@@ -1,4 +1,5 @@
1
1
  import mustache from "mustache";
2
+ import { addLangChainErrorFields } from "../errors/index.js";
2
3
  function configureMustache() {
3
4
  // Use unescaped HTML
4
5
  // https://github.com/janl/mustache.js?tab=readme-ov-file#variables
@@ -104,7 +105,15 @@ export const DEFAULT_PARSER_MAPPING = {
104
105
  "f-string": parseFString,
105
106
  mustache: parseMustache,
106
107
  };
107
- export const renderTemplate = (template, templateFormat, inputValues) => DEFAULT_FORMATTER_MAPPING[templateFormat](template, inputValues);
108
+ export const renderTemplate = (template, templateFormat, inputValues) => {
109
+ try {
110
+ return DEFAULT_FORMATTER_MAPPING[templateFormat](template, inputValues);
111
+ }
112
+ catch (e) {
113
+ const error = addLangChainErrorFields(e, "INVALID_PROMPT_INPUT");
114
+ throw error;
115
+ }
116
+ };
108
117
  export const parseTemplate = (template, templateFormat) => DEFAULT_PARSER_MAPPING[templateFormat](template);
109
118
  export const checkValidTemplate = (template, templateFormat, inputVariables) => {
110
119
  if (!(templateFormat in DEFAULT_FORMATTER_MAPPING)) {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@langchain/core",
3
- "version": "0.3.11",
3
+ "version": "0.3.13",
4
4
  "description": "Core LangChain.js abstractions and schemas",
5
5
  "type": "module",
6
6
  "engines": {