langchain 0.3.5 → 0.3.6

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.
package/README.md CHANGED
@@ -34,7 +34,7 @@ LangChain is written in TypeScript and can be used in:
34
34
  - **Reason**: rely on a language model to reason (about how to answer based on provided context, what actions to take, etc.)
35
35
 
36
36
  This framework consists of several parts.
37
- - **Open-source libraries**: Build your applications using LangChain's open-source [building blocks](https://js.langchain.com/docs/concepts#langchain-expression-language), [components](https://js.langchain.com/docs/concepts), and [third-party integrations](https://js.langchain.com/docs/integrations/platforms/).
37
+ - **Open-source libraries**: Build your applications using LangChain's open-source [building blocks](https://js.langchain.com/docs/concepts/lcel), [components](https://js.langchain.com/docs/concepts), and [third-party integrations](https://js.langchain.com/docs/integrations/platforms/).
38
38
  Use [LangGraph.js](https://js.langchain.com/docs/concepts/#langgraphjs) to build stateful agents with first-class streaming and human-in-the-loop support.
39
39
  - **Productionization**: Use [LangSmith](https://docs.smith.langchain.com/) to inspect, monitor and evaluate your chains, so that you can continuously optimize and deploy with confidence.
40
40
  - **Deployment**: Turn your LangGraph applications into production-ready APIs and Assistants with [LangGraph Cloud](https://langchain-ai.github.io/langgraph/cloud/).
@@ -31,7 +31,7 @@ Use this if you want to respond directly and conversationally to the human. Mark
31
31
  \`\`\`json
32
32
  {{{{
33
33
  "action": "Final Answer",
34
- "action_input": string // You should put what you want to return to use here and make sure to use valid json newline characters.
34
+ "action_input": string // You should put what you want to return to user here and make sure to use valid json newline characters.
35
35
  }}}}
36
36
  \`\`\`
37
37
 
@@ -1,5 +1,5 @@
1
1
  export declare const DEFAULT_PREFIX = "Assistant is a large language model trained by OpenAI.\n\nAssistant is designed to be able to assist with a wide range of tasks, from answering simple questions to providing in-depth explanations and discussions on a wide range of topics. As a language model, Assistant is able to generate human-like text based on the input it receives, allowing it to engage in natural-sounding conversations and provide responses that are coherent and relevant to the topic at hand.\n\nAssistant is constantly learning and improving, and its capabilities are constantly evolving. It is able to process and understand large amounts of text, and can use this knowledge to provide accurate and informative responses to a wide range of questions. Additionally, Assistant is able to generate its own text based on the input it receives, allowing it to engage in discussions and provide explanations and descriptions on a wide range of topics.\n\nOverall, Assistant is a powerful system that can help with a wide range of tasks and provide valuable insights and information on a wide range of topics. Whether you need help with a specific question or just want to have a conversation about a particular topic, Assistant is here to assist.";
2
2
  export declare const PREFIX_END = " However, above all else, all responses must adhere to the format of RESPONSE FORMAT INSTRUCTIONS.";
3
- export declare const FORMAT_INSTRUCTIONS = "RESPONSE FORMAT INSTRUCTIONS\n----------------------------\n\nOutput a JSON markdown code snippet containing a valid JSON object in one of two formats:\n\n**Option 1:**\nUse this if you want the human to use a tool.\nMarkdown code snippet formatted in the following schema:\n\n```json\n{{{{\n \"action\": string, // The action to take. Must be one of [{tool_names}]\n \"action_input\": string // The input to the action. May be a stringified object.\n}}}}\n```\n\n**Option #2:**\nUse this if you want to respond directly and conversationally to the human. Markdown code snippet formatted in the following schema:\n\n```json\n{{{{\n \"action\": \"Final Answer\",\n \"action_input\": string // You should put what you want to return to use here and make sure to use valid json newline characters.\n}}}}\n```\n\nFor both options, remember to always include the surrounding markdown code snippet delimiters (begin with \"```json\" and end with \"```\")!\n";
3
+ export declare const FORMAT_INSTRUCTIONS = "RESPONSE FORMAT INSTRUCTIONS\n----------------------------\n\nOutput a JSON markdown code snippet containing a valid JSON object in one of two formats:\n\n**Option 1:**\nUse this if you want the human to use a tool.\nMarkdown code snippet formatted in the following schema:\n\n```json\n{{{{\n \"action\": string, // The action to take. Must be one of [{tool_names}]\n \"action_input\": string // The input to the action. May be a stringified object.\n}}}}\n```\n\n**Option #2:**\nUse this if you want to respond directly and conversationally to the human. Markdown code snippet formatted in the following schema:\n\n```json\n{{{{\n \"action\": \"Final Answer\",\n \"action_input\": string // You should put what you want to return to user here and make sure to use valid json newline characters.\n}}}}\n```\n\nFor both options, remember to always include the surrounding markdown code snippet delimiters (begin with \"```json\" and end with \"```\")!\n";
4
4
  export declare const DEFAULT_SUFFIX = "TOOLS\n------\nAssistant can ask the user to use tools to look up information that may be helpful in answering the users original question. The tools the human can use are:\n\n{tools}\n\n{format_instructions}\n\nUSER'S INPUT\n--------------------\nHere is the user's input (remember to respond with a markdown code snippet of a json blob with a single action, and NOTHING else):\n\n{{input}}";
5
5
  export declare const TEMPLATE_TOOL_RESPONSE = "TOOL RESPONSE:\n---------------------\n{observation}\n\nUSER'S INPUT\n--------------------\n\nOkay, so what is the response to my last comment? If using information obtained from the tools you must mention it explicitly without mentioning the tool names - I have forgotten all TOOL RESPONSES! Remember to respond with a markdown code snippet of a json blob with a single action, and NOTHING else.";
@@ -28,7 +28,7 @@ Use this if you want to respond directly and conversationally to the human. Mark
28
28
  \`\`\`json
29
29
  {{{{
30
30
  "action": "Final Answer",
31
- "action_input": string // You should put what you want to return to use here and make sure to use valid json newline characters.
31
+ "action_input": string // You should put what you want to return to user here and make sure to use valid json newline characters.
32
32
  }}}}
33
33
  \`\`\`
34
34
 
@@ -25,47 +25,48 @@ params = {}) {
25
25
  if (!modelProviderCopy) {
26
26
  throw new Error(`Unable to infer model provider for { model: ${model} }, please specify modelProvider directly.`);
27
27
  }
28
+ const { modelProvider: _unused, ...passedParams } = params;
28
29
  try {
29
30
  switch (modelProviderCopy) {
30
31
  case "openai": {
31
32
  const { ChatOpenAI } = await import("@langchain/openai");
32
- return new ChatOpenAI({ model, ...params });
33
+ return new ChatOpenAI({ model, ...passedParams });
33
34
  }
34
35
  case "anthropic": {
35
36
  const { ChatAnthropic } = await import("@langchain/anthropic");
36
- return new ChatAnthropic({ model, ...params });
37
+ return new ChatAnthropic({ model, ...passedParams });
37
38
  }
38
39
  case "azure_openai": {
39
40
  const { AzureChatOpenAI } = await import("@langchain/openai");
40
- return new AzureChatOpenAI({ model, ...params });
41
+ return new AzureChatOpenAI({ model, ...passedParams });
41
42
  }
42
43
  case "cohere": {
43
44
  const { ChatCohere } = await import("@langchain/cohere");
44
- return new ChatCohere({ model, ...params });
45
+ return new ChatCohere({ model, ...passedParams });
45
46
  }
46
47
  case "google-vertexai": {
47
48
  const { ChatVertexAI } = await import("@langchain/google-vertexai");
48
- return new ChatVertexAI({ model, ...params });
49
+ return new ChatVertexAI({ model, ...passedParams });
49
50
  }
50
51
  case "google-genai": {
51
52
  const { ChatGoogleGenerativeAI } = await import("@langchain/google-genai");
52
- return new ChatGoogleGenerativeAI({ model, ...params });
53
+ return new ChatGoogleGenerativeAI({ model, ...passedParams });
53
54
  }
54
55
  case "ollama": {
55
56
  const { ChatOllama } = await import("@langchain/ollama");
56
- return new ChatOllama({ model, ...params });
57
+ return new ChatOllama({ model, ...passedParams });
57
58
  }
58
59
  case "mistralai": {
59
60
  const { ChatMistralAI } = await import("@langchain/mistralai");
60
- return new ChatMistralAI({ model, ...params });
61
+ return new ChatMistralAI({ model, ...passedParams });
61
62
  }
62
63
  case "groq": {
63
64
  const { ChatGroq } = await import("@langchain/groq");
64
- return new ChatGroq({ model, ...params });
65
+ return new ChatGroq({ model, ...passedParams });
65
66
  }
66
67
  case "bedrock": {
67
68
  const { ChatBedrockConverse } = await import("@langchain/aws");
68
- return new ChatBedrockConverse({ model, ...params });
69
+ return new ChatBedrockConverse({ model, ...passedParams });
69
70
  }
70
71
  case "fireworks": {
71
72
  const { ChatFireworks } = await import(
@@ -75,7 +76,7 @@ params = {}) {
75
76
  // eslint-disable-next-line @typescript-eslint/ban-ts-comment
76
77
  // @ts-ignore - Can not install as a proper dependency due to circular dependency
77
78
  "@langchain/community/chat_models/fireworks");
78
- return new ChatFireworks({ model, ...params });
79
+ return new ChatFireworks({ model, ...passedParams });
79
80
  }
80
81
  case "together": {
81
82
  const { ChatTogetherAI } = await import(
@@ -85,7 +86,7 @@ params = {}) {
85
86
  // eslint-disable-next-line @typescript-eslint/ban-ts-comment
86
87
  // @ts-ignore - Can not install as a proper dependency due to circular dependency
87
88
  "@langchain/community/chat_models/togetherai");
88
- return new ChatTogetherAI({ model, ...params });
89
+ return new ChatTogetherAI({ model, ...passedParams });
89
90
  }
90
91
  default: {
91
92
  const supported = _SUPPORTED_PROVIDERS.join(", ");
@@ -208,7 +209,10 @@ class _ConfigurableModel extends chat_models_1.BaseChatModel {
208
209
  this._configurableFields = "any";
209
210
  }
210
211
  else {
211
- this._configurableFields = fields.configurableFields ?? "any";
212
+ this._configurableFields = fields.configurableFields ?? [
213
+ "model",
214
+ "modelProvider",
215
+ ];
212
216
  }
213
217
  if (fields.configPrefix) {
214
218
  this._configPrefix = fields.configPrefix.endsWith("_")
@@ -563,18 +567,20 @@ fields) {
563
567
  configPrefix: "",
564
568
  ...(fields ?? {}),
565
569
  };
566
- let configurableFieldsCopy = configurableFields;
567
- if (!model && !configurableFieldsCopy) {
570
+ let configurableFieldsCopy = Array.isArray(configurableFields)
571
+ ? [...configurableFields]
572
+ : configurableFields;
573
+ if (!model && configurableFieldsCopy === undefined) {
568
574
  configurableFieldsCopy = ["model", "modelProvider"];
569
575
  }
570
- if (configPrefix && !configurableFieldsCopy) {
576
+ if (configPrefix && configurableFieldsCopy === undefined) {
571
577
  console.warn(`{ configPrefix: ${configPrefix} } has been set but no fields are configurable. Set ` +
572
578
  `{ configurableFields: [...] } to specify the model params that are ` +
573
579
  `configurable.`);
574
580
  }
575
581
  // eslint-disable-next-line @typescript-eslint/no-explicit-any
576
582
  const paramsCopy = { ...params };
577
- if (!configurableFieldsCopy) {
583
+ if (configurableFieldsCopy === undefined) {
578
584
  return new _ConfigurableModel({
579
585
  defaultConfig: {
580
586
  ...paramsCopy,
@@ -22,47 +22,48 @@ params = {}) {
22
22
  if (!modelProviderCopy) {
23
23
  throw new Error(`Unable to infer model provider for { model: ${model} }, please specify modelProvider directly.`);
24
24
  }
25
+ const { modelProvider: _unused, ...passedParams } = params;
25
26
  try {
26
27
  switch (modelProviderCopy) {
27
28
  case "openai": {
28
29
  const { ChatOpenAI } = await import("@langchain/openai");
29
- return new ChatOpenAI({ model, ...params });
30
+ return new ChatOpenAI({ model, ...passedParams });
30
31
  }
31
32
  case "anthropic": {
32
33
  const { ChatAnthropic } = await import("@langchain/anthropic");
33
- return new ChatAnthropic({ model, ...params });
34
+ return new ChatAnthropic({ model, ...passedParams });
34
35
  }
35
36
  case "azure_openai": {
36
37
  const { AzureChatOpenAI } = await import("@langchain/openai");
37
- return new AzureChatOpenAI({ model, ...params });
38
+ return new AzureChatOpenAI({ model, ...passedParams });
38
39
  }
39
40
  case "cohere": {
40
41
  const { ChatCohere } = await import("@langchain/cohere");
41
- return new ChatCohere({ model, ...params });
42
+ return new ChatCohere({ model, ...passedParams });
42
43
  }
43
44
  case "google-vertexai": {
44
45
  const { ChatVertexAI } = await import("@langchain/google-vertexai");
45
- return new ChatVertexAI({ model, ...params });
46
+ return new ChatVertexAI({ model, ...passedParams });
46
47
  }
47
48
  case "google-genai": {
48
49
  const { ChatGoogleGenerativeAI } = await import("@langchain/google-genai");
49
- return new ChatGoogleGenerativeAI({ model, ...params });
50
+ return new ChatGoogleGenerativeAI({ model, ...passedParams });
50
51
  }
51
52
  case "ollama": {
52
53
  const { ChatOllama } = await import("@langchain/ollama");
53
- return new ChatOllama({ model, ...params });
54
+ return new ChatOllama({ model, ...passedParams });
54
55
  }
55
56
  case "mistralai": {
56
57
  const { ChatMistralAI } = await import("@langchain/mistralai");
57
- return new ChatMistralAI({ model, ...params });
58
+ return new ChatMistralAI({ model, ...passedParams });
58
59
  }
59
60
  case "groq": {
60
61
  const { ChatGroq } = await import("@langchain/groq");
61
- return new ChatGroq({ model, ...params });
62
+ return new ChatGroq({ model, ...passedParams });
62
63
  }
63
64
  case "bedrock": {
64
65
  const { ChatBedrockConverse } = await import("@langchain/aws");
65
- return new ChatBedrockConverse({ model, ...params });
66
+ return new ChatBedrockConverse({ model, ...passedParams });
66
67
  }
67
68
  case "fireworks": {
68
69
  const { ChatFireworks } = await import(
@@ -72,7 +73,7 @@ params = {}) {
72
73
  // eslint-disable-next-line @typescript-eslint/ban-ts-comment
73
74
  // @ts-ignore - Can not install as a proper dependency due to circular dependency
74
75
  "@langchain/community/chat_models/fireworks");
75
- return new ChatFireworks({ model, ...params });
76
+ return new ChatFireworks({ model, ...passedParams });
76
77
  }
77
78
  case "together": {
78
79
  const { ChatTogetherAI } = await import(
@@ -82,7 +83,7 @@ params = {}) {
82
83
  // eslint-disable-next-line @typescript-eslint/ban-ts-comment
83
84
  // @ts-ignore - Can not install as a proper dependency due to circular dependency
84
85
  "@langchain/community/chat_models/togetherai");
85
- return new ChatTogetherAI({ model, ...params });
86
+ return new ChatTogetherAI({ model, ...passedParams });
86
87
  }
87
88
  default: {
88
89
  const supported = _SUPPORTED_PROVIDERS.join(", ");
@@ -204,7 +205,10 @@ class _ConfigurableModel extends BaseChatModel {
204
205
  this._configurableFields = "any";
205
206
  }
206
207
  else {
207
- this._configurableFields = fields.configurableFields ?? "any";
208
+ this._configurableFields = fields.configurableFields ?? [
209
+ "model",
210
+ "modelProvider",
211
+ ];
208
212
  }
209
213
  if (fields.configPrefix) {
210
214
  this._configPrefix = fields.configPrefix.endsWith("_")
@@ -559,18 +563,20 @@ fields) {
559
563
  configPrefix: "",
560
564
  ...(fields ?? {}),
561
565
  };
562
- let configurableFieldsCopy = configurableFields;
563
- if (!model && !configurableFieldsCopy) {
566
+ let configurableFieldsCopy = Array.isArray(configurableFields)
567
+ ? [...configurableFields]
568
+ : configurableFields;
569
+ if (!model && configurableFieldsCopy === undefined) {
564
570
  configurableFieldsCopy = ["model", "modelProvider"];
565
571
  }
566
- if (configPrefix && !configurableFieldsCopy) {
572
+ if (configPrefix && configurableFieldsCopy === undefined) {
567
573
  console.warn(`{ configPrefix: ${configPrefix} } has been set but no fields are configurable. Set ` +
568
574
  `{ configurableFields: [...] } to specify the model params that are ` +
569
575
  `configurable.`);
570
576
  }
571
577
  // eslint-disable-next-line @typescript-eslint/no-explicit-any
572
578
  const paramsCopy = { ...params };
573
- if (!configurableFieldsCopy) {
579
+ if (configurableFieldsCopy === undefined) {
574
580
  return new _ConfigurableModel({
575
581
  defaultConfig: {
576
582
  ...paramsCopy,
@@ -137,12 +137,17 @@ class JsonOutputFunctionsParser extends output_parsers_1.BaseCumulativeTransform
137
137
  return this.parse(result);
138
138
  }
139
139
  async parse(text) {
140
- const parsedResult = JSON.parse(text);
141
- if (this.argsOnly) {
140
+ try {
141
+ const parsedResult = JSON.parse(text);
142
+ if (this.argsOnly) {
143
+ return parsedResult;
144
+ }
145
+ parsedResult.arguments = JSON.parse(parsedResult.arguments);
142
146
  return parsedResult;
143
147
  }
144
- parsedResult.arguments = JSON.parse(parsedResult.arguments);
145
- return parsedResult;
148
+ catch (e) {
149
+ throw new output_parsers_1.OutputParserException(`Failed to parse. Text: "${text}". Error: ${e}`);
150
+ }
146
151
  }
147
152
  getFormatInstructions() {
148
153
  return "";
@@ -1,5 +1,5 @@
1
1
  import { compare, } from "@langchain/core/utils/json_patch";
2
- import { BaseCumulativeTransformOutputParser, BaseLLMOutputParser, } from "@langchain/core/output_parsers";
2
+ import { BaseCumulativeTransformOutputParser, BaseLLMOutputParser, OutputParserException, } from "@langchain/core/output_parsers";
3
3
  import { parsePartialJson } from "@langchain/core/output_parsers";
4
4
  /**
5
5
  * Class for parsing the output of an LLM. Can be configured to return
@@ -133,12 +133,17 @@ export class JsonOutputFunctionsParser extends BaseCumulativeTransformOutputPars
133
133
  return this.parse(result);
134
134
  }
135
135
  async parse(text) {
136
- const parsedResult = JSON.parse(text);
137
- if (this.argsOnly) {
136
+ try {
137
+ const parsedResult = JSON.parse(text);
138
+ if (this.argsOnly) {
139
+ return parsedResult;
140
+ }
141
+ parsedResult.arguments = JSON.parse(parsedResult.arguments);
138
142
  return parsedResult;
139
143
  }
140
- parsedResult.arguments = JSON.parse(parsedResult.arguments);
141
- return parsedResult;
144
+ catch (e) {
145
+ throw new OutputParserException(`Failed to parse. Text: "${text}". Error: ${e}`);
146
+ }
142
147
  }
143
148
  getFormatInstructions() {
144
149
  return "";
@@ -80,7 +80,12 @@ ${JSON.stringify((0, zod_to_json_schema_1.zodToJsonSchema)(this.schema))}
80
80
  return await this.schema.parseAsync(JSON.parse(json));
81
81
  }
82
82
  catch (e) {
83
- throw new output_parsers_1.OutputParserException(`Failed to parse. Text: "${text}". Error: ${e}`, text);
83
+ try {
84
+ return await this.schema.parseAsync(JSON.parse(text.trim()));
85
+ }
86
+ catch (e2) {
87
+ throw new output_parsers_1.OutputParserException(`Failed to parse. Text: "${text}". Error: ${e2}`, text);
88
+ }
84
89
  }
85
90
  }
86
91
  }
@@ -77,7 +77,12 @@ ${JSON.stringify(zodToJsonSchema(this.schema))}
77
77
  return await this.schema.parseAsync(JSON.parse(json));
78
78
  }
79
79
  catch (e) {
80
- throw new OutputParserException(`Failed to parse. Text: "${text}". Error: ${e}`, text);
80
+ try {
81
+ return await this.schema.parseAsync(JSON.parse(text.trim()));
82
+ }
83
+ catch (e2) {
84
+ throw new OutputParserException(`Failed to parse. Text: "${text}". Error: ${e2}`, text);
85
+ }
81
86
  }
82
87
  }
83
88
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "langchain",
3
- "version": "0.3.5",
3
+ "version": "0.3.6",
4
4
  "description": "Typescript bindings for langchain",
5
5
  "type": "module",
6
6
  "engines": {