@langchain/anthropic 0.3.4 → 0.3.5

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.
@@ -15,6 +15,7 @@ const output_parsers_js_1 = require("./output_parsers.cjs");
15
15
  const tools_js_1 = require("./utils/tools.cjs");
16
16
  const message_inputs_js_1 = require("./utils/message_inputs.cjs");
17
17
  const message_outputs_js_1 = require("./utils/message_outputs.cjs");
18
+ const errors_js_1 = require("./utils/errors.cjs");
18
19
  function _toolsInParams(params) {
19
20
  return !!(params.tools && params.tools.length > 0);
20
21
  }
@@ -773,11 +774,19 @@ class ChatAnthropicMessages extends chat_models_1.BaseChatModel {
773
774
  maxRetries: 0,
774
775
  });
775
776
  }
776
- const makeCompletionRequest = async () => this.streamingClient.messages.create({
777
- ...request,
778
- ...this.invocationKwargs,
779
- stream: true,
780
- }, options);
777
+ const makeCompletionRequest = async () => {
778
+ try {
779
+ return await this.streamingClient.messages.create({
780
+ ...request,
781
+ ...this.invocationKwargs,
782
+ stream: true,
783
+ }, options);
784
+ }
785
+ catch (e) {
786
+ const error = (0, errors_js_1.wrapAnthropicClientError)(e);
787
+ throw error;
788
+ }
789
+ };
781
790
  return this.caller.call(makeCompletionRequest);
782
791
  }
783
792
  /** @ignore */
@@ -792,10 +801,18 @@ class ChatAnthropicMessages extends chat_models_1.BaseChatModel {
792
801
  maxRetries: 0,
793
802
  });
794
803
  }
795
- const makeCompletionRequest = async () => this.batchClient.messages.create({
796
- ...request,
797
- ...this.invocationKwargs,
798
- }, options);
804
+ const makeCompletionRequest = async () => {
805
+ try {
806
+ return await this.batchClient.messages.create({
807
+ ...request,
808
+ ...this.invocationKwargs,
809
+ }, options);
810
+ }
811
+ catch (e) {
812
+ const error = (0, errors_js_1.wrapAnthropicClientError)(e);
813
+ throw error;
814
+ }
815
+ };
799
816
  return this.caller.callWithOptions({ signal: options.signal ?? undefined }, makeCompletionRequest);
800
817
  }
801
818
  _llmType() {
@@ -12,6 +12,7 @@ import { AnthropicToolsOutputParser } from "./output_parsers.js";
12
12
  import { extractToolCallChunk, handleToolChoice } from "./utils/tools.js";
13
13
  import { _convertMessagesToAnthropicPayload } from "./utils/message_inputs.js";
14
14
  import { _makeMessageChunkFromAnthropicEvent, anthropicResponseToChatMessages, } from "./utils/message_outputs.js";
15
+ import { wrapAnthropicClientError } from "./utils/errors.js";
15
16
  function _toolsInParams(params) {
16
17
  return !!(params.tools && params.tools.length > 0);
17
18
  }
@@ -770,11 +771,19 @@ export class ChatAnthropicMessages extends BaseChatModel {
770
771
  maxRetries: 0,
771
772
  });
772
773
  }
773
- const makeCompletionRequest = async () => this.streamingClient.messages.create({
774
- ...request,
775
- ...this.invocationKwargs,
776
- stream: true,
777
- }, options);
774
+ const makeCompletionRequest = async () => {
775
+ try {
776
+ return await this.streamingClient.messages.create({
777
+ ...request,
778
+ ...this.invocationKwargs,
779
+ stream: true,
780
+ }, options);
781
+ }
782
+ catch (e) {
783
+ const error = wrapAnthropicClientError(e);
784
+ throw error;
785
+ }
786
+ };
778
787
  return this.caller.call(makeCompletionRequest);
779
788
  }
780
789
  /** @ignore */
@@ -789,10 +798,18 @@ export class ChatAnthropicMessages extends BaseChatModel {
789
798
  maxRetries: 0,
790
799
  });
791
800
  }
792
- const makeCompletionRequest = async () => this.batchClient.messages.create({
793
- ...request,
794
- ...this.invocationKwargs,
795
- }, options);
801
+ const makeCompletionRequest = async () => {
802
+ try {
803
+ return await this.batchClient.messages.create({
804
+ ...request,
805
+ ...this.invocationKwargs,
806
+ }, options);
807
+ }
808
+ catch (e) {
809
+ const error = wrapAnthropicClientError(e);
810
+ throw error;
811
+ }
812
+ };
796
813
  return this.caller.callWithOptions({ signal: options.signal ?? undefined }, makeCompletionRequest);
797
814
  }
798
815
  _llmType() {
@@ -0,0 +1,32 @@
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.wrapAnthropicClientError = 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;
12
+ // eslint-disable-next-line @typescript-eslint/no-explicit-any
13
+ function wrapAnthropicClientError(e) {
14
+ let error;
15
+ if (e.status === 400 && e.message.includes("tool")) {
16
+ error = addLangChainErrorFields(e, "INVALID_TOOL_RESULTS");
17
+ }
18
+ else if (e.status === 401) {
19
+ error = addLangChainErrorFields(e, "MODEL_AUTHENTICATION");
20
+ }
21
+ else if (e.status === 404) {
22
+ error = addLangChainErrorFields(e, "MODEL_NOT_FOUND");
23
+ }
24
+ else if (e.status === 429) {
25
+ error = addLangChainErrorFields(e, "MODEL_RATE_LIMIT");
26
+ }
27
+ else {
28
+ error = e;
29
+ }
30
+ return error;
31
+ }
32
+ exports.wrapAnthropicClientError = wrapAnthropicClientError;
@@ -0,0 +1,3 @@
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;
3
+ export declare function wrapAnthropicClientError(e: any): any;
@@ -0,0 +1,27 @@
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
+ }
8
+ // eslint-disable-next-line @typescript-eslint/no-explicit-any
9
+ export function wrapAnthropicClientError(e) {
10
+ let error;
11
+ if (e.status === 400 && e.message.includes("tool")) {
12
+ error = addLangChainErrorFields(e, "INVALID_TOOL_RESULTS");
13
+ }
14
+ else if (e.status === 401) {
15
+ error = addLangChainErrorFields(e, "MODEL_AUTHENTICATION");
16
+ }
17
+ else if (e.status === 404) {
18
+ error = addLangChainErrorFields(e, "MODEL_NOT_FOUND");
19
+ }
20
+ else if (e.status === 429) {
21
+ error = addLangChainErrorFields(e, "MODEL_RATE_LIMIT");
22
+ }
23
+ else {
24
+ error = e;
25
+ }
26
+ return error;
27
+ }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@langchain/anthropic",
3
- "version": "0.3.4",
3
+ "version": "0.3.5",
4
4
  "description": "Anthropic integrations for LangChain.js",
5
5
  "type": "module",
6
6
  "engines": {