@langchain/anthropic 0.3.25 → 0.3.27

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.
@@ -608,9 +608,20 @@ class ChatAnthropicMessages extends chat_models_1.BaseChatModel {
608
608
  this.modelName = fields?.model ?? fields?.modelName ?? this.model;
609
609
  this.model = this.modelName;
610
610
  this.invocationKwargs = fields?.invocationKwargs ?? {};
611
- this.temperature = fields?.temperature ?? this.temperature;
611
+ if (this.model.includes("opus-4-1")) {
612
+ // Default to `undefined` for `topP` for Opus 4.1 models
613
+ this.topP = fields?.topP === null ? undefined : fields?.topP;
614
+ }
615
+ else {
616
+ this.topP = fields?.topP ?? this.topP;
617
+ }
618
+ // If the user passes `null`, set it to `undefined`. Otherwise, use their value or the default. We have to check for null, because
619
+ // there's no way for us to know if they explicitly set it to `undefined`, or never passed a value
620
+ this.temperature =
621
+ fields?.temperature === null
622
+ ? undefined
623
+ : fields?.temperature ?? this.temperature;
612
624
  this.topK = fields?.topK ?? this.topK;
613
- this.topP = fields?.topP ?? this.topP;
614
625
  this.maxTokens =
615
626
  fields?.maxTokensToSample ?? fields?.maxTokens ?? this.maxTokens;
616
627
  this.stopSequences = fields?.stopSequences ?? this.stopSequences;
@@ -684,7 +695,9 @@ class ChatAnthropicMessages extends chat_models_1.BaseChatModel {
684
695
  if (this.topK !== -1) {
685
696
  throw new Error("topK is not supported when thinking is enabled");
686
697
  }
687
- if (this.topP !== -1) {
698
+ if (this.model.includes("opus-4-1")
699
+ ? this.topP !== undefined
700
+ : this.topP !== -1) {
688
701
  throw new Error("topP is not supported when thinking is enabled");
689
702
  }
690
703
  if (this.temperature !== 1) {
@@ -33,8 +33,10 @@ export interface AnthropicInput {
33
33
  * from 0 to 1. Use temp closer to 0 for analytical /
34
34
  * multiple choice, and temp closer to 1 for creative
35
35
  * and generative tasks.
36
+ * To not set this field, pass `null`. If `undefined` is passed,
37
+ * the default (1) will be used.
36
38
  */
37
- temperature?: number;
39
+ temperature?: number | null;
38
40
  /** Only sample from the top K options for each subsequent
39
41
  * token. Used to remove "long tail" low probability
40
42
  * responses. Defaults to -1, which disables it.
@@ -47,8 +49,13 @@ export interface AnthropicInput {
47
49
  * specified by top_p. Defaults to -1, which disables it.
48
50
  * Note that you should either alter temperature or top_p,
49
51
  * but not both.
52
+ *
53
+ * To not set this field, pass `null`. If `undefined` is passed,
54
+ * the default (-1) will be used.
55
+ *
56
+ * For Opus 4.1, this defaults to `null`.
50
57
  */
51
- topP?: number;
58
+ topP?: number | null;
52
59
  /** A maximum number of tokens to generate before stopping. */
53
60
  maxTokens?: number;
54
61
  /**
@@ -487,9 +494,9 @@ export declare class ChatAnthropicMessages<CallOptions extends ChatAnthropicCall
487
494
  anthropicApiKey?: string;
488
495
  apiKey?: string;
489
496
  apiUrl?: string;
490
- temperature: number;
497
+ temperature: number | undefined;
491
498
  topK: number;
492
- topP: number;
499
+ topP: number | undefined;
493
500
  maxTokens: number;
494
501
  modelName: string;
495
502
  model: string;
@@ -605,9 +605,20 @@ export class ChatAnthropicMessages extends BaseChatModel {
605
605
  this.modelName = fields?.model ?? fields?.modelName ?? this.model;
606
606
  this.model = this.modelName;
607
607
  this.invocationKwargs = fields?.invocationKwargs ?? {};
608
- this.temperature = fields?.temperature ?? this.temperature;
608
+ if (this.model.includes("opus-4-1")) {
609
+ // Default to `undefined` for `topP` for Opus 4.1 models
610
+ this.topP = fields?.topP === null ? undefined : fields?.topP;
611
+ }
612
+ else {
613
+ this.topP = fields?.topP ?? this.topP;
614
+ }
615
+ // If the user passes `null`, set it to `undefined`. Otherwise, use their value or the default. We have to check for null, because
616
+ // there's no way for us to know if they explicitly set it to `undefined`, or never passed a value
617
+ this.temperature =
618
+ fields?.temperature === null
619
+ ? undefined
620
+ : fields?.temperature ?? this.temperature;
609
621
  this.topK = fields?.topK ?? this.topK;
610
- this.topP = fields?.topP ?? this.topP;
611
622
  this.maxTokens =
612
623
  fields?.maxTokensToSample ?? fields?.maxTokens ?? this.maxTokens;
613
624
  this.stopSequences = fields?.stopSequences ?? this.stopSequences;
@@ -681,7 +692,9 @@ export class ChatAnthropicMessages extends BaseChatModel {
681
692
  if (this.topK !== -1) {
682
693
  throw new Error("topK is not supported when thinking is enabled");
683
694
  }
684
- if (this.topP !== -1) {
695
+ if (this.model.includes("opus-4-1")
696
+ ? this.topP !== undefined
697
+ : this.topP !== -1) {
685
698
  throw new Error("topP is not supported when thinking is enabled");
686
699
  }
687
700
  if (this.temperature !== 1) {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@langchain/anthropic",
3
- "version": "0.3.25",
3
+ "version": "0.3.27",
4
4
  "description": "Anthropic integrations for LangChain.js",
5
5
  "type": "module",
6
6
  "engines": {