@langchain/anthropic 0.3.27 → 0.3.29

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.
@@ -29,7 +29,7 @@ function _documentsInParams(params) {
29
29
  block != null &&
30
30
  block.type === "document" &&
31
31
  typeof block.citations === "object" &&
32
- block.citations.enabled) {
32
+ block.citations?.enabled) {
33
33
  return true;
34
34
  }
35
35
  }
@@ -44,21 +44,22 @@ function isAnthropicTool(tool) {
44
44
  return "input_schema" in tool;
45
45
  }
46
46
  function isBuiltinTool(tool) {
47
- const builtinTools = [
48
- "web_search",
49
- "bash",
50
- "code_execution",
51
- "computer",
52
- "str_replace_editor",
53
- "str_replace_based_edit_tool",
47
+ const builtInToolPrefixes = [
48
+ "text_editor_",
49
+ "computer_",
50
+ "bash_",
51
+ "web_search_",
52
+ "web_fetch_",
53
+ "str_replace_editor_",
54
+ "str_replace_based_edit_tool_",
55
+ "code_execution_",
56
+ "memory_",
54
57
  ];
55
58
  return (typeof tool === "object" &&
56
59
  tool !== null &&
57
60
  "type" in tool &&
58
61
  "name" in tool &&
59
- typeof tool.type === "string" &&
60
- typeof tool.name === "string" &&
61
- builtinTools.includes(tool.name));
62
+ builtInToolPrefixes.some((prefix) => typeof tool.type === "string" && tool.type.startsWith(prefix)));
62
63
  }
63
64
  function extractToken(chunk) {
64
65
  if (typeof chunk.content === "string") {
@@ -561,6 +562,12 @@ class ChatAnthropicMessages extends chat_models_1.BaseChatModel {
561
562
  writable: true,
562
563
  value: { type: "disabled" }
563
564
  });
565
+ Object.defineProperty(this, "contextManagement", {
566
+ enumerable: true,
567
+ configurable: true,
568
+ writable: true,
569
+ value: void 0
570
+ });
564
571
  // Used for non-streaming requests
565
572
  Object.defineProperty(this, "batchClient", {
566
573
  enumerable: true,
@@ -608,8 +615,8 @@ class ChatAnthropicMessages extends chat_models_1.BaseChatModel {
608
615
  this.modelName = fields?.model ?? fields?.modelName ?? this.model;
609
616
  this.model = this.modelName;
610
617
  this.invocationKwargs = fields?.invocationKwargs ?? {};
611
- if (this.model.includes("opus-4-1")) {
612
- // Default to `undefined` for `topP` for Opus 4.1 models
618
+ // Default to `undefined` for `topP` for Opus 4.1 and Sonnet 4.5 models
619
+ if (this.model.includes("opus-4-1") || this.model.includes("sonnet-4-5")) {
613
620
  this.topP = fields?.topP === null ? undefined : fields?.topP;
614
621
  }
615
622
  else {
@@ -725,6 +732,7 @@ class ChatAnthropicMessages extends chat_models_1.BaseChatModel {
725
732
  tools: this.formatStructuredToolToAnthropic(options?.tools),
726
733
  tool_choice,
727
734
  thinking: this.thinking,
735
+ context_management: this.contextManagement,
728
736
  ...this.invocationKwargs,
729
737
  };
730
738
  }
@@ -7,7 +7,7 @@ import { BaseChatModel, BaseChatModelCallOptions, LangSmithParams, type BaseChat
7
7
  import { type StructuredOutputMethodOptions, type BaseLanguageModelInput } from "@langchain/core/language_models/base";
8
8
  import { Runnable } from "@langchain/core/runnables";
9
9
  import { InteropZodType } from "@langchain/core/utils/types";
10
- import { AnthropicMessageCreateParams, AnthropicMessageStreamEvent, AnthropicRequestOptions, AnthropicStreamingMessageCreateParams, AnthropicThinkingConfigParam, AnthropicToolChoice, ChatAnthropicToolType } from "./types.js";
10
+ import { AnthropicContextManagementConfigParam, AnthropicMessageCreateParams, AnthropicMessageStreamEvent, AnthropicRequestOptions, AnthropicStreamingMessageCreateParams, AnthropicThinkingConfigParam, AnthropicToolChoice, ChatAnthropicToolType } from "./types.js";
11
11
  export interface ChatAnthropicCallOptions extends BaseChatModelCallOptions, Pick<AnthropicInput, "streamUsage"> {
12
12
  tools?: ChatAnthropicToolType[];
13
13
  /**
@@ -53,7 +53,7 @@ export interface AnthropicInput {
53
53
  * To not set this field, pass `null`. If `undefined` is passed,
54
54
  * the default (-1) will be used.
55
55
  *
56
- * For Opus 4.1, this defaults to `null`.
56
+ * For Opus 4.1 and Sonnet 4.5, this defaults to `null`.
57
57
  */
58
58
  topP?: number | null;
59
59
  /** A maximum number of tokens to generate before stopping. */
@@ -102,6 +102,10 @@ export interface AnthropicInput {
102
102
  * Options for extended thinking.
103
103
  */
104
104
  thinking?: AnthropicThinkingConfigParam;
105
+ /**
106
+ * Configuration for context management. See https://docs.claude.com/en/docs/build-with-claude/context-editing
107
+ */
108
+ contextManagement?: AnthropicContextManagementConfigParam;
105
109
  }
106
110
  /**
107
111
  * A type representing additional parameters that can be passed to the
@@ -505,6 +509,7 @@ export declare class ChatAnthropicMessages<CallOptions extends ChatAnthropicCall
505
509
  streaming: boolean;
506
510
  clientOptions: ClientOptions;
507
511
  thinking: AnthropicThinkingConfigParam;
512
+ contextManagement?: AnthropicContextManagementConfigParam;
508
513
  protected batchClient: Anthropic;
509
514
  protected streamingClient: Anthropic;
510
515
  streamUsage: boolean;
@@ -26,7 +26,7 @@ function _documentsInParams(params) {
26
26
  block != null &&
27
27
  block.type === "document" &&
28
28
  typeof block.citations === "object" &&
29
- block.citations.enabled) {
29
+ block.citations?.enabled) {
30
30
  return true;
31
31
  }
32
32
  }
@@ -41,21 +41,22 @@ function isAnthropicTool(tool) {
41
41
  return "input_schema" in tool;
42
42
  }
43
43
  function isBuiltinTool(tool) {
44
- const builtinTools = [
45
- "web_search",
46
- "bash",
47
- "code_execution",
48
- "computer",
49
- "str_replace_editor",
50
- "str_replace_based_edit_tool",
44
+ const builtInToolPrefixes = [
45
+ "text_editor_",
46
+ "computer_",
47
+ "bash_",
48
+ "web_search_",
49
+ "web_fetch_",
50
+ "str_replace_editor_",
51
+ "str_replace_based_edit_tool_",
52
+ "code_execution_",
53
+ "memory_",
51
54
  ];
52
55
  return (typeof tool === "object" &&
53
56
  tool !== null &&
54
57
  "type" in tool &&
55
58
  "name" in tool &&
56
- typeof tool.type === "string" &&
57
- typeof tool.name === "string" &&
58
- builtinTools.includes(tool.name));
59
+ builtInToolPrefixes.some((prefix) => typeof tool.type === "string" && tool.type.startsWith(prefix)));
59
60
  }
60
61
  function extractToken(chunk) {
61
62
  if (typeof chunk.content === "string") {
@@ -558,6 +559,12 @@ export class ChatAnthropicMessages extends BaseChatModel {
558
559
  writable: true,
559
560
  value: { type: "disabled" }
560
561
  });
562
+ Object.defineProperty(this, "contextManagement", {
563
+ enumerable: true,
564
+ configurable: true,
565
+ writable: true,
566
+ value: void 0
567
+ });
561
568
  // Used for non-streaming requests
562
569
  Object.defineProperty(this, "batchClient", {
563
570
  enumerable: true,
@@ -605,8 +612,8 @@ export class ChatAnthropicMessages extends BaseChatModel {
605
612
  this.modelName = fields?.model ?? fields?.modelName ?? this.model;
606
613
  this.model = this.modelName;
607
614
  this.invocationKwargs = fields?.invocationKwargs ?? {};
608
- if (this.model.includes("opus-4-1")) {
609
- // Default to `undefined` for `topP` for Opus 4.1 models
615
+ // Default to `undefined` for `topP` for Opus 4.1 and Sonnet 4.5 models
616
+ if (this.model.includes("opus-4-1") || this.model.includes("sonnet-4-5")) {
610
617
  this.topP = fields?.topP === null ? undefined : fields?.topP;
611
618
  }
612
619
  else {
@@ -722,6 +729,7 @@ export class ChatAnthropicMessages extends BaseChatModel {
722
729
  tools: this.formatStructuredToolToAnthropic(options?.tools),
723
730
  tool_choice,
724
731
  thinking: this.thinking,
732
+ context_management: this.contextManagement,
725
733
  ...this.invocationKwargs,
726
734
  };
727
735
  }
package/dist/types.d.ts CHANGED
@@ -11,6 +11,7 @@ export type AnthropicMessageResponse = Anthropic.ContentBlock | AnthropicToolRes
11
11
  export type AnthropicMessageCreateParams = Anthropic.MessageCreateParamsNonStreaming;
12
12
  export type AnthropicStreamingMessageCreateParams = Anthropic.MessageCreateParamsStreaming;
13
13
  export type AnthropicThinkingConfigParam = Anthropic.ThinkingConfigParam;
14
+ export type AnthropicContextManagementConfigParam = Anthropic.Beta.BetaContextManagementConfig;
14
15
  export type AnthropicMessageStreamEvent = Anthropic.MessageStreamEvent;
15
16
  export type AnthropicRequestOptions = Anthropic.RequestOptions;
16
17
  export type AnthropicToolChoice = {
@@ -28,7 +29,7 @@ export type AnthropicRedactedThinkingBlockParam = Anthropic.Messages.RedactedThi
28
29
  export type AnthropicServerToolUseBlockParam = Anthropic.Messages.ServerToolUseBlockParam;
29
30
  export type AnthropicWebSearchToolResultBlockParam = Anthropic.Messages.WebSearchToolResultBlockParam;
30
31
  export type AnthropicWebSearchResultBlockParam = Anthropic.Messages.WebSearchResultBlockParam;
31
- export type AnthropicSearchResultBlockParam = Anthropic.Beta.BetaSearchResultBlockParam;
32
+ export type AnthropicSearchResultBlockParam = Anthropic.SearchResultBlockParam;
32
33
  export type ChatAnthropicContentBlock = AnthropicTextBlockParam | AnthropicImageBlockParam | AnthropicToolUseBlockParam | AnthropicToolResultBlockParam | AnthropicDocumentBlockParam | AnthropicThinkingBlockParam | AnthropicRedactedThinkingBlockParam | AnthropicServerToolUseBlockParam | AnthropicWebSearchToolResultBlockParam | AnthropicWebSearchResultBlockParam | AnthropicSearchResultBlockParam;
33
34
  export declare function isAnthropicImageBlockParam(block: unknown): block is AnthropicImageBlockParam;
34
35
  export type AnthropicBuiltInToolUnion = Exclude<Anthropic.Messages.ToolUnion, Anthropic.Messages.Tool>;
@@ -141,7 +141,6 @@ const standardContentBlockConverter = {
141
141
  source: {
142
142
  type: "url",
143
143
  url: block.url,
144
- media_type: block.mime_type ?? "",
145
144
  },
146
145
  ...("cache_control" in (block.metadata ?? {})
147
146
  ? { cache_control: block.metadata.cache_control }
@@ -137,7 +137,6 @@ const standardContentBlockConverter = {
137
137
  source: {
138
138
  type: "url",
139
139
  url: block.url,
140
- media_type: block.mime_type ?? "",
141
140
  },
142
141
  ...("cache_control" in (block.metadata ?? {})
143
142
  ? { cache_control: block.metadata.cache_control }
@@ -52,11 +52,15 @@ function _makeMessageChunkFromAnthropicEvent(data, fields) {
52
52
  cache_read: data.usage.cache_read_input_tokens,
53
53
  },
54
54
  };
55
+ const responseMetadata = "context_management" in data.delta
56
+ ? { context_management: data.delta.context_management }
57
+ : undefined;
55
58
  return {
56
59
  chunk: new messages_1.AIMessageChunk({
57
60
  content: fields.coerceContentToString ? "" : [],
58
61
  additional_kwargs: { ...data.delta },
59
62
  usage_metadata: fields.streamUsage ? usageMetadata : undefined,
63
+ response_metadata: responseMetadata,
60
64
  }),
61
65
  };
62
66
  }
@@ -48,11 +48,15 @@ export function _makeMessageChunkFromAnthropicEvent(data, fields) {
48
48
  cache_read: data.usage.cache_read_input_tokens,
49
49
  },
50
50
  };
51
+ const responseMetadata = "context_management" in data.delta
52
+ ? { context_management: data.delta.context_management }
53
+ : undefined;
51
54
  return {
52
55
  chunk: new AIMessageChunk({
53
56
  content: fields.coerceContentToString ? "" : [],
54
57
  additional_kwargs: { ...data.delta },
55
58
  usage_metadata: fields.streamUsage ? usageMetadata : undefined,
59
+ response_metadata: responseMetadata,
56
60
  }),
57
61
  };
58
62
  }
@@ -15,6 +15,11 @@ function handleToolChoice(toolChoice) {
15
15
  type: "auto",
16
16
  };
17
17
  }
18
+ else if (toolChoice === "none") {
19
+ return {
20
+ type: "none",
21
+ };
22
+ }
18
23
  else if (typeof toolChoice === "string") {
19
24
  return {
20
25
  type: "tool",
@@ -1,3 +1,3 @@
1
1
  import type { Anthropic } from "@anthropic-ai/sdk";
2
2
  import { AnthropicToolChoice } from "../types.js";
3
- export declare function handleToolChoice(toolChoice?: AnthropicToolChoice): Anthropic.Messages.ToolChoiceAuto | Anthropic.Messages.ToolChoiceAny | Anthropic.Messages.ToolChoiceTool | undefined;
3
+ export declare function handleToolChoice(toolChoice?: AnthropicToolChoice): Anthropic.Messages.ToolChoiceAuto | Anthropic.Messages.ToolChoiceAny | Anthropic.Messages.ToolChoiceTool | Anthropic.Messages.ToolChoiceNone | undefined;
@@ -12,6 +12,11 @@ export function handleToolChoice(toolChoice) {
12
12
  type: "auto",
13
13
  };
14
14
  }
15
+ else if (toolChoice === "none") {
16
+ return {
17
+ type: "none",
18
+ };
19
+ }
15
20
  else if (typeof toolChoice === "string") {
16
21
  return {
17
22
  type: "tool",
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@langchain/anthropic",
3
- "version": "0.3.27",
3
+ "version": "0.3.29",
4
4
  "description": "Anthropic integrations for LangChain.js",
5
5
  "type": "module",
6
6
  "engines": {
@@ -35,7 +35,7 @@
35
35
  "author": "LangChain",
36
36
  "license": "MIT",
37
37
  "dependencies": {
38
- "@anthropic-ai/sdk": "^0.56.0",
38
+ "@anthropic-ai/sdk": "^0.65.0",
39
39
  "fast-xml-parser": "^4.4.1"
40
40
  },
41
41
  "peerDependencies": {