@langchain/anthropic 0.3.20 → 0.3.22
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/dist/chat_models.cjs +9 -10
- package/dist/chat_models.d.ts +4 -5
- package/dist/chat_models.js +10 -11
- package/dist/experimental/tool_calling.cjs +5 -10
- package/dist/experimental/tool_calling.d.ts +3 -3
- package/dist/experimental/tool_calling.js +5 -10
- package/dist/experimental/utils/tool_calling.d.ts +1 -1
- package/dist/output_parsers.cjs +3 -2
- package/dist/output_parsers.d.ts +4 -4
- package/dist/output_parsers.js +3 -2
- package/dist/utils/message_inputs.cjs +4 -1
- package/dist/utils/message_inputs.js +4 -1
- package/package.json +5 -6
package/dist/chat_models.cjs
CHANGED
|
@@ -7,7 +7,7 @@ const outputs_1 = require("@langchain/core/outputs");
|
|
|
7
7
|
const env_1 = require("@langchain/core/utils/env");
|
|
8
8
|
const chat_models_1 = require("@langchain/core/language_models/chat_models");
|
|
9
9
|
const base_1 = require("@langchain/core/language_models/base");
|
|
10
|
-
const
|
|
10
|
+
const json_schema_1 = require("@langchain/core/utils/json_schema");
|
|
11
11
|
const runnables_1 = require("@langchain/core/runnables");
|
|
12
12
|
const types_1 = require("@langchain/core/utils/types");
|
|
13
13
|
const function_calling_1 = require("@langchain/core/utils/function_calling");
|
|
@@ -81,9 +81,8 @@ function extractToken(chunk) {
|
|
|
81
81
|
*
|
|
82
82
|
* ```typescript
|
|
83
83
|
* // When calling `.bind`, call options should be passed via the first argument
|
|
84
|
-
* const llmWithArgsBound = llm.
|
|
84
|
+
* const llmWithArgsBound = llm.bindTools([...]).withConfig({
|
|
85
85
|
* stop: ["\n"],
|
|
86
|
-
* tools: [...],
|
|
87
86
|
* });
|
|
88
87
|
*
|
|
89
88
|
* // When calling `.bindTools`, call options should be passed via the second argument
|
|
@@ -642,8 +641,8 @@ class ChatAnthropicMessages extends chat_models_1.BaseChatModel {
|
|
|
642
641
|
return {
|
|
643
642
|
name: tool.name,
|
|
644
643
|
description: tool.description,
|
|
645
|
-
input_schema: ((0, types_1.
|
|
646
|
-
? (0,
|
|
644
|
+
input_schema: ((0, types_1.isInteropZodSchema)(tool.schema)
|
|
645
|
+
? (0, json_schema_1.toJsonSchema)(tool.schema)
|
|
647
646
|
: tool.schema),
|
|
648
647
|
};
|
|
649
648
|
}
|
|
@@ -651,7 +650,7 @@ class ChatAnthropicMessages extends chat_models_1.BaseChatModel {
|
|
|
651
650
|
});
|
|
652
651
|
}
|
|
653
652
|
bindTools(tools, kwargs) {
|
|
654
|
-
return this.
|
|
653
|
+
return this.withConfig({
|
|
655
654
|
tools: this.formatStructuredToolToAnthropic(tools),
|
|
656
655
|
...kwargs,
|
|
657
656
|
});
|
|
@@ -880,8 +879,8 @@ class ChatAnthropicMessages extends chat_models_1.BaseChatModel {
|
|
|
880
879
|
let functionName = name ?? "extract";
|
|
881
880
|
let outputParser;
|
|
882
881
|
let tools;
|
|
883
|
-
if ((0, types_1.
|
|
884
|
-
const jsonSchema = (0,
|
|
882
|
+
if ((0, types_1.isInteropZodSchema)(schema)) {
|
|
883
|
+
const jsonSchema = (0, json_schema_1.toJsonSchema)(schema);
|
|
885
884
|
tools = [
|
|
886
885
|
{
|
|
887
886
|
name: functionName,
|
|
@@ -925,7 +924,7 @@ class ChatAnthropicMessages extends chat_models_1.BaseChatModel {
|
|
|
925
924
|
"generated. Consider disabling `thinking` or adjust your prompt to ensure " +
|
|
926
925
|
"the tool is called.";
|
|
927
926
|
console.warn(thinkingAdmonition);
|
|
928
|
-
llm = this.
|
|
927
|
+
llm = this.withConfig({
|
|
929
928
|
tools,
|
|
930
929
|
});
|
|
931
930
|
const raiseIfNoToolCalls = (message) => {
|
|
@@ -937,7 +936,7 @@ class ChatAnthropicMessages extends chat_models_1.BaseChatModel {
|
|
|
937
936
|
llm = llm.pipe(raiseIfNoToolCalls);
|
|
938
937
|
}
|
|
939
938
|
else {
|
|
940
|
-
llm = this.
|
|
939
|
+
llm = this.withConfig({
|
|
941
940
|
tools,
|
|
942
941
|
tool_choice: {
|
|
943
942
|
type: "tool",
|
package/dist/chat_models.d.ts
CHANGED
|
@@ -6,7 +6,7 @@ import { ChatGenerationChunk, type ChatResult } from "@langchain/core/outputs";
|
|
|
6
6
|
import { BaseChatModel, BaseChatModelCallOptions, LangSmithParams, type BaseChatModelParams } from "@langchain/core/language_models/chat_models";
|
|
7
7
|
import { type StructuredOutputMethodOptions, type BaseLanguageModelInput } from "@langchain/core/language_models/base";
|
|
8
8
|
import { Runnable } from "@langchain/core/runnables";
|
|
9
|
-
import {
|
|
9
|
+
import { InteropZodType } from "@langchain/core/utils/types";
|
|
10
10
|
import { AnthropicMessageCreateParams, AnthropicMessageStreamEvent, AnthropicRequestOptions, AnthropicStreamingMessageCreateParams, AnthropicThinkingConfigParam, AnthropicToolChoice, ChatAnthropicToolType } from "./types.js";
|
|
11
11
|
export interface ChatAnthropicCallOptions extends BaseChatModelCallOptions, Pick<AnthropicInput, "streamUsage"> {
|
|
12
12
|
tools?: ChatAnthropicToolType[];
|
|
@@ -117,9 +117,8 @@ type Kwargs = Record<string, any>;
|
|
|
117
117
|
*
|
|
118
118
|
* ```typescript
|
|
119
119
|
* // When calling `.bind`, call options should be passed via the first argument
|
|
120
|
-
* const llmWithArgsBound = llm.
|
|
120
|
+
* const llmWithArgsBound = llm.bindTools([...]).withConfig({
|
|
121
121
|
* stop: ["\n"],
|
|
122
|
-
* tools: [...],
|
|
123
122
|
* });
|
|
124
123
|
*
|
|
125
124
|
* // When calling `.bindTools`, call options should be passed via the second argument
|
|
@@ -576,8 +575,8 @@ export declare class ChatAnthropicMessages<CallOptions extends ChatAnthropicCall
|
|
|
576
575
|
/** @ignore */
|
|
577
576
|
protected completionWithRetry(request: AnthropicMessageCreateParams & Kwargs, options: AnthropicRequestOptions): Promise<Anthropic.Message>;
|
|
578
577
|
_llmType(): string;
|
|
579
|
-
withStructuredOutput<RunOutput extends Record<string, any> = Record<string, any>>(outputSchema:
|
|
580
|
-
withStructuredOutput<RunOutput extends Record<string, any> = Record<string, any>>(outputSchema:
|
|
578
|
+
withStructuredOutput<RunOutput extends Record<string, any> = Record<string, any>>(outputSchema: InteropZodType<RunOutput> | Record<string, any>, config?: StructuredOutputMethodOptions<false>): Runnable<BaseLanguageModelInput, RunOutput>;
|
|
579
|
+
withStructuredOutput<RunOutput extends Record<string, any> = Record<string, any>>(outputSchema: InteropZodType<RunOutput> | Record<string, any>, config?: StructuredOutputMethodOptions<true>): Runnable<BaseLanguageModelInput, {
|
|
581
580
|
raw: BaseMessage;
|
|
582
581
|
parsed: RunOutput;
|
|
583
582
|
}>;
|
package/dist/chat_models.js
CHANGED
|
@@ -4,9 +4,9 @@ import { ChatGenerationChunk } from "@langchain/core/outputs";
|
|
|
4
4
|
import { getEnvironmentVariable } from "@langchain/core/utils/env";
|
|
5
5
|
import { BaseChatModel, } from "@langchain/core/language_models/chat_models";
|
|
6
6
|
import { isOpenAITool, } from "@langchain/core/language_models/base";
|
|
7
|
-
import {
|
|
7
|
+
import { toJsonSchema } from "@langchain/core/utils/json_schema";
|
|
8
8
|
import { RunnablePassthrough, RunnableSequence, } from "@langchain/core/runnables";
|
|
9
|
-
import {
|
|
9
|
+
import { isInteropZodSchema, } from "@langchain/core/utils/types";
|
|
10
10
|
import { isLangChainTool } from "@langchain/core/utils/function_calling";
|
|
11
11
|
import { AnthropicToolsOutputParser } from "./output_parsers.js";
|
|
12
12
|
import { handleToolChoice } from "./utils/tools.js";
|
|
@@ -78,9 +78,8 @@ function extractToken(chunk) {
|
|
|
78
78
|
*
|
|
79
79
|
* ```typescript
|
|
80
80
|
* // When calling `.bind`, call options should be passed via the first argument
|
|
81
|
-
* const llmWithArgsBound = llm.
|
|
81
|
+
* const llmWithArgsBound = llm.bindTools([...]).withConfig({
|
|
82
82
|
* stop: ["\n"],
|
|
83
|
-
* tools: [...],
|
|
84
83
|
* });
|
|
85
84
|
*
|
|
86
85
|
* // When calling `.bindTools`, call options should be passed via the second argument
|
|
@@ -639,8 +638,8 @@ export class ChatAnthropicMessages extends BaseChatModel {
|
|
|
639
638
|
return {
|
|
640
639
|
name: tool.name,
|
|
641
640
|
description: tool.description,
|
|
642
|
-
input_schema: (
|
|
643
|
-
?
|
|
641
|
+
input_schema: (isInteropZodSchema(tool.schema)
|
|
642
|
+
? toJsonSchema(tool.schema)
|
|
644
643
|
: tool.schema),
|
|
645
644
|
};
|
|
646
645
|
}
|
|
@@ -648,7 +647,7 @@ export class ChatAnthropicMessages extends BaseChatModel {
|
|
|
648
647
|
});
|
|
649
648
|
}
|
|
650
649
|
bindTools(tools, kwargs) {
|
|
651
|
-
return this.
|
|
650
|
+
return this.withConfig({
|
|
652
651
|
tools: this.formatStructuredToolToAnthropic(tools),
|
|
653
652
|
...kwargs,
|
|
654
653
|
});
|
|
@@ -877,8 +876,8 @@ export class ChatAnthropicMessages extends BaseChatModel {
|
|
|
877
876
|
let functionName = name ?? "extract";
|
|
878
877
|
let outputParser;
|
|
879
878
|
let tools;
|
|
880
|
-
if (
|
|
881
|
-
const jsonSchema =
|
|
879
|
+
if (isInteropZodSchema(schema)) {
|
|
880
|
+
const jsonSchema = toJsonSchema(schema);
|
|
882
881
|
tools = [
|
|
883
882
|
{
|
|
884
883
|
name: functionName,
|
|
@@ -922,7 +921,7 @@ export class ChatAnthropicMessages extends BaseChatModel {
|
|
|
922
921
|
"generated. Consider disabling `thinking` or adjust your prompt to ensure " +
|
|
923
922
|
"the tool is called.";
|
|
924
923
|
console.warn(thinkingAdmonition);
|
|
925
|
-
llm = this.
|
|
924
|
+
llm = this.withConfig({
|
|
926
925
|
tools,
|
|
927
926
|
});
|
|
928
927
|
const raiseIfNoToolCalls = (message) => {
|
|
@@ -934,7 +933,7 @@ export class ChatAnthropicMessages extends BaseChatModel {
|
|
|
934
933
|
llm = llm.pipe(raiseIfNoToolCalls);
|
|
935
934
|
}
|
|
936
935
|
else {
|
|
937
|
-
llm = this.
|
|
936
|
+
llm = this.withConfig({
|
|
938
937
|
tools,
|
|
939
938
|
tool_choice: {
|
|
940
939
|
type: "tool",
|
|
@@ -6,7 +6,8 @@ const messages_1 = require("@langchain/core/messages");
|
|
|
6
6
|
const chat_models_1 = require("@langchain/core/language_models/chat_models");
|
|
7
7
|
const runnables_1 = require("@langchain/core/runnables");
|
|
8
8
|
const openai_tools_1 = require("@langchain/core/output_parsers/openai_tools");
|
|
9
|
-
const
|
|
9
|
+
const json_schema_1 = require("@langchain/core/utils/json_schema");
|
|
10
|
+
const types_1 = require("@langchain/core/utils/types");
|
|
10
11
|
const chat_models_js_1 = require("../chat_models.cjs");
|
|
11
12
|
const tool_calling_js_1 = require("./utils/tool_calling.cjs");
|
|
12
13
|
/**
|
|
@@ -226,8 +227,8 @@ class ChatAnthropicTools extends chat_models_1.BaseChatModel {
|
|
|
226
227
|
let functionName = name ?? "extract";
|
|
227
228
|
let outputParser;
|
|
228
229
|
let tools;
|
|
229
|
-
if (
|
|
230
|
-
const jsonSchema = (0,
|
|
230
|
+
if ((0, types_1.isInteropZodSchema)(schema)) {
|
|
231
|
+
const jsonSchema = (0, json_schema_1.toJsonSchema)(schema);
|
|
231
232
|
tools = [
|
|
232
233
|
{
|
|
233
234
|
type: "function",
|
|
@@ -270,7 +271,7 @@ class ChatAnthropicTools extends chat_models_1.BaseChatModel {
|
|
|
270
271
|
keyName: functionName,
|
|
271
272
|
});
|
|
272
273
|
}
|
|
273
|
-
const llm = this.
|
|
274
|
+
const llm = this.withConfig({
|
|
274
275
|
tools,
|
|
275
276
|
tool_choice: force
|
|
276
277
|
? {
|
|
@@ -307,12 +308,6 @@ class ChatAnthropicTools extends chat_models_1.BaseChatModel {
|
|
|
307
308
|
}
|
|
308
309
|
}
|
|
309
310
|
exports.ChatAnthropicTools = ChatAnthropicTools;
|
|
310
|
-
function isZodSchema(
|
|
311
|
-
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
312
|
-
input) {
|
|
313
|
-
// Check for a characteristic method of Zod schemas
|
|
314
|
-
return typeof input?.parse === "function";
|
|
315
|
-
}
|
|
316
311
|
function isStructuredOutputMethodParams(x
|
|
317
312
|
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
318
313
|
) {
|
|
@@ -5,7 +5,7 @@ import { CallbackManagerForLLMRun, Callbacks } from "@langchain/core/callbacks/m
|
|
|
5
5
|
import { BasePromptTemplate } from "@langchain/core/prompts";
|
|
6
6
|
import type { BaseLanguageModelCallOptions, BaseLanguageModelInput, StructuredOutputMethodParams, StructuredOutputMethodOptions, ToolDefinition } from "@langchain/core/language_models/base";
|
|
7
7
|
import { Runnable } from "@langchain/core/runnables";
|
|
8
|
-
import {
|
|
8
|
+
import { InteropZodType } from "@langchain/core/utils/types";
|
|
9
9
|
import { type AnthropicInput } from "../chat_models.js";
|
|
10
10
|
export interface ChatAnthropicToolsCallOptions extends BaseLanguageModelCallOptions {
|
|
11
11
|
tools?: ToolDefinition[];
|
|
@@ -45,10 +45,10 @@ export declare class ChatAnthropicTools extends BaseChatModel<ChatAnthropicTools
|
|
|
45
45
|
generate(messages: BaseMessageLike[][], parsedOptions?: ChatAnthropicToolsCallOptions, callbacks?: Callbacks): Promise<LLMResult>;
|
|
46
46
|
_generate(_messages: BaseMessage[], _options: this["ParsedCallOptions"], _runManager?: CallbackManagerForLLMRun | undefined): Promise<ChatResult>;
|
|
47
47
|
_llmType(): string;
|
|
48
|
-
withStructuredOutput<RunOutput extends Record<string, any> = Record<string, any>>(outputSchema: StructuredOutputMethodParams<RunOutput, false> |
|
|
48
|
+
withStructuredOutput<RunOutput extends Record<string, any> = Record<string, any>>(outputSchema: StructuredOutputMethodParams<RunOutput, false> | InteropZodType<RunOutput> | Record<string, any>, config?: StructuredOutputMethodOptions<false> & {
|
|
49
49
|
force?: boolean;
|
|
50
50
|
}): Runnable<BaseLanguageModelInput, RunOutput>;
|
|
51
|
-
withStructuredOutput<RunOutput extends Record<string, any> = Record<string, any>>(outputSchema: StructuredOutputMethodParams<RunOutput, true> |
|
|
51
|
+
withStructuredOutput<RunOutput extends Record<string, any> = Record<string, any>>(outputSchema: StructuredOutputMethodParams<RunOutput, true> | InteropZodType<RunOutput> | Record<string, any>, config?: StructuredOutputMethodOptions<true> & {
|
|
52
52
|
force?: boolean;
|
|
53
53
|
}): Runnable<BaseLanguageModelInput, {
|
|
54
54
|
raw: BaseMessage;
|
|
@@ -3,7 +3,8 @@ import { AIMessage, SystemMessage, coerceMessageLikeToMessage, } from "@langchai
|
|
|
3
3
|
import { BaseChatModel, } from "@langchain/core/language_models/chat_models";
|
|
4
4
|
import { RunnablePassthrough, RunnableSequence, } from "@langchain/core/runnables";
|
|
5
5
|
import { JsonOutputKeyToolsParser } from "@langchain/core/output_parsers/openai_tools";
|
|
6
|
-
import {
|
|
6
|
+
import { toJsonSchema, } from "@langchain/core/utils/json_schema";
|
|
7
|
+
import { isInteropZodSchema, } from "@langchain/core/utils/types";
|
|
7
8
|
import { ChatAnthropic } from "../chat_models.js";
|
|
8
9
|
import { DEFAULT_TOOL_SYSTEM_PROMPT, formatAsXMLRepresentation, fixArrayXMLParameters, } from "./utils/tool_calling.js";
|
|
9
10
|
/**
|
|
@@ -223,8 +224,8 @@ export class ChatAnthropicTools extends BaseChatModel {
|
|
|
223
224
|
let functionName = name ?? "extract";
|
|
224
225
|
let outputParser;
|
|
225
226
|
let tools;
|
|
226
|
-
if (
|
|
227
|
-
const jsonSchema =
|
|
227
|
+
if (isInteropZodSchema(schema)) {
|
|
228
|
+
const jsonSchema = toJsonSchema(schema);
|
|
228
229
|
tools = [
|
|
229
230
|
{
|
|
230
231
|
type: "function",
|
|
@@ -267,7 +268,7 @@ export class ChatAnthropicTools extends BaseChatModel {
|
|
|
267
268
|
keyName: functionName,
|
|
268
269
|
});
|
|
269
270
|
}
|
|
270
|
-
const llm = this.
|
|
271
|
+
const llm = this.withConfig({
|
|
271
272
|
tools,
|
|
272
273
|
tool_choice: force
|
|
273
274
|
? {
|
|
@@ -303,12 +304,6 @@ export class ChatAnthropicTools extends BaseChatModel {
|
|
|
303
304
|
});
|
|
304
305
|
}
|
|
305
306
|
}
|
|
306
|
-
function isZodSchema(
|
|
307
|
-
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
308
|
-
input) {
|
|
309
|
-
// Check for a characteristic method of Zod schemas
|
|
310
|
-
return typeof input?.parse === "function";
|
|
311
|
-
}
|
|
312
307
|
function isStructuredOutputMethodParams(x
|
|
313
308
|
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
314
309
|
) {
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { JsonSchema7ObjectType } from "
|
|
1
|
+
import { type JsonSchema7ObjectType } from "@langchain/core/utils/json_schema";
|
|
2
2
|
import { PromptTemplate } from "@langchain/core/prompts";
|
|
3
3
|
import { ToolDefinition } from "@langchain/core/language_models/base";
|
|
4
4
|
export declare const DEFAULT_TOOL_SYSTEM_PROMPT: PromptTemplate<import("@langchain/core/prompts").ParamsFromFString<"In this environment you have access to a set of tools you can use to answer the user's question.\n\nYou may call them like this:\n<function_calls>\n<invoke>\n<tool_name>$TOOL_NAME</tool_name>\n<parameters>\n<$PARAMETER_NAME>$PARAMETER_VALUE</$PARAMETER_NAME>\n...\n</parameters>\n</invoke>\n</function_calls>\n\nHere are the tools available:\n{tools}\n\nIf the schema above contains a property typed as an enum, you must only return values matching an allowed value for that enum.">, any>;
|
package/dist/output_parsers.cjs
CHANGED
|
@@ -2,6 +2,7 @@
|
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
3
|
exports.extractToolCalls = exports.AnthropicToolsOutputParser = void 0;
|
|
4
4
|
const output_parsers_1 = require("@langchain/core/output_parsers");
|
|
5
|
+
const types_1 = require("@langchain/core/utils/types");
|
|
5
6
|
class AnthropicToolsOutputParser extends output_parsers_1.BaseLLMOutputParser {
|
|
6
7
|
static lc_name() {
|
|
7
8
|
return "AnthropicToolsOutputParser";
|
|
@@ -61,12 +62,12 @@ class AnthropicToolsOutputParser extends output_parsers_1.BaseLLMOutputParser {
|
|
|
61
62
|
if (this.zodSchema === undefined) {
|
|
62
63
|
return parsedResult;
|
|
63
64
|
}
|
|
64
|
-
const zodParsedResult = await this.zodSchema
|
|
65
|
+
const zodParsedResult = await (0, types_1.interopSafeParseAsync)(this.zodSchema, parsedResult);
|
|
65
66
|
if (zodParsedResult.success) {
|
|
66
67
|
return zodParsedResult.data;
|
|
67
68
|
}
|
|
68
69
|
else {
|
|
69
|
-
throw new output_parsers_1.OutputParserException(`Failed to parse. Text: "${JSON.stringify(result, null, 2)}". Error: ${JSON.stringify(zodParsedResult.error.
|
|
70
|
+
throw new output_parsers_1.OutputParserException(`Failed to parse. Text: "${JSON.stringify(result, null, 2)}". Error: ${JSON.stringify(zodParsedResult.error.issues)}`, JSON.stringify(parsedResult, null, 2));
|
|
70
71
|
}
|
|
71
72
|
}
|
|
72
73
|
async parseResult(generations) {
|
package/dist/output_parsers.d.ts
CHANGED
|
@@ -1,9 +1,9 @@
|
|
|
1
|
-
import { z } from "zod";
|
|
2
1
|
import { BaseLLMOutputParser } from "@langchain/core/output_parsers";
|
|
3
|
-
import {
|
|
2
|
+
import { JsonOutputKeyToolsParserParamsInterop } from "@langchain/core/output_parsers/openai_tools";
|
|
4
3
|
import { ChatGeneration } from "@langchain/core/outputs";
|
|
5
4
|
import { ToolCall } from "@langchain/core/messages/tool";
|
|
6
|
-
|
|
5
|
+
import { InteropZodType } from "@langchain/core/utils/types";
|
|
6
|
+
interface AnthropicToolsOutputParserParams<T extends Record<string, any>> extends JsonOutputKeyToolsParserParamsInterop<T> {
|
|
7
7
|
}
|
|
8
8
|
export declare class AnthropicToolsOutputParser<T extends Record<string, any> = Record<string, any>> extends BaseLLMOutputParser<T> {
|
|
9
9
|
static lc_name(): string;
|
|
@@ -13,7 +13,7 @@ export declare class AnthropicToolsOutputParser<T extends Record<string, any> =
|
|
|
13
13
|
keyName: string;
|
|
14
14
|
/** Whether to return only the first tool call. */
|
|
15
15
|
returnSingle: boolean;
|
|
16
|
-
zodSchema?:
|
|
16
|
+
zodSchema?: InteropZodType<T>;
|
|
17
17
|
constructor(params: AnthropicToolsOutputParserParams<T>);
|
|
18
18
|
protected _validateResult(result: unknown): Promise<T>;
|
|
19
19
|
parseResult(generations: ChatGeneration[]): Promise<T>;
|
package/dist/output_parsers.js
CHANGED
|
@@ -1,4 +1,5 @@
|
|
|
1
1
|
import { BaseLLMOutputParser, OutputParserException, } from "@langchain/core/output_parsers";
|
|
2
|
+
import { interopSafeParseAsync, } from "@langchain/core/utils/types";
|
|
2
3
|
export class AnthropicToolsOutputParser extends BaseLLMOutputParser {
|
|
3
4
|
static lc_name() {
|
|
4
5
|
return "AnthropicToolsOutputParser";
|
|
@@ -58,12 +59,12 @@ export class AnthropicToolsOutputParser extends BaseLLMOutputParser {
|
|
|
58
59
|
if (this.zodSchema === undefined) {
|
|
59
60
|
return parsedResult;
|
|
60
61
|
}
|
|
61
|
-
const zodParsedResult = await this.zodSchema
|
|
62
|
+
const zodParsedResult = await interopSafeParseAsync(this.zodSchema, parsedResult);
|
|
62
63
|
if (zodParsedResult.success) {
|
|
63
64
|
return zodParsedResult.data;
|
|
64
65
|
}
|
|
65
66
|
else {
|
|
66
|
-
throw new OutputParserException(`Failed to parse. Text: "${JSON.stringify(result, null, 2)}". Error: ${JSON.stringify(zodParsedResult.error.
|
|
67
|
+
throw new OutputParserException(`Failed to parse. Text: "${JSON.stringify(result, null, 2)}". Error: ${JSON.stringify(zodParsedResult.error.issues)}`, JSON.stringify(parsedResult, null, 2));
|
|
67
68
|
}
|
|
68
69
|
}
|
|
69
70
|
async parseResult(generations) {
|
|
@@ -74,7 +74,10 @@ function _ensureMessageContents(messages) {
|
|
|
74
74
|
content: [
|
|
75
75
|
{
|
|
76
76
|
type: "tool_result",
|
|
77
|
-
|
|
77
|
+
// rare case: message.content could be undefined
|
|
78
|
+
...(message.content != null
|
|
79
|
+
? { content: _formatContent(message.content) }
|
|
80
|
+
: {}),
|
|
78
81
|
tool_use_id: message.tool_call_id,
|
|
79
82
|
},
|
|
80
83
|
],
|
|
@@ -71,7 +71,10 @@ function _ensureMessageContents(messages) {
|
|
|
71
71
|
content: [
|
|
72
72
|
{
|
|
73
73
|
type: "tool_result",
|
|
74
|
-
|
|
74
|
+
// rare case: message.content could be undefined
|
|
75
|
+
...(message.content != null
|
|
76
|
+
? { content: _formatContent(message.content) }
|
|
77
|
+
: {}),
|
|
75
78
|
tool_use_id: message.tool_call_id,
|
|
76
79
|
},
|
|
77
80
|
],
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@langchain/anthropic",
|
|
3
|
-
"version": "0.3.
|
|
3
|
+
"version": "0.3.22",
|
|
4
4
|
"description": "Anthropic integrations for LangChain.js",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"engines": {
|
|
@@ -36,12 +36,10 @@
|
|
|
36
36
|
"license": "MIT",
|
|
37
37
|
"dependencies": {
|
|
38
38
|
"@anthropic-ai/sdk": "^0.39.0",
|
|
39
|
-
"fast-xml-parser": "^4.4.1"
|
|
40
|
-
"zod": "^3.22.4",
|
|
41
|
-
"zod-to-json-schema": "^3.22.4"
|
|
39
|
+
"fast-xml-parser": "^4.4.1"
|
|
42
40
|
},
|
|
43
41
|
"peerDependencies": {
|
|
44
|
-
"@langchain/core": ">=0.3.
|
|
42
|
+
"@langchain/core": ">=0.3.58 <0.4.0"
|
|
45
43
|
},
|
|
46
44
|
"devDependencies": {
|
|
47
45
|
"@anthropic-ai/vertex-sdk": "^0.4.1",
|
|
@@ -65,7 +63,8 @@
|
|
|
65
63
|
"release-it": "^18.1.2",
|
|
66
64
|
"rimraf": "^5.0.1",
|
|
67
65
|
"ts-jest": "^29.1.0",
|
|
68
|
-
"typescript": "~5.1.6"
|
|
66
|
+
"typescript": "~5.1.6",
|
|
67
|
+
"zod": "^3.25.32"
|
|
69
68
|
},
|
|
70
69
|
"publishConfig": {
|
|
71
70
|
"access": "public"
|