Skip to content

Commit 4ce2fc8

Browse files
authored
fix(anthropic): support Opus 4.1 with default params (#8620)
1 parent cc4ff80 commit 4ce2fc8

File tree

2 files changed

+36
-6
lines changed

2 files changed

+36
-6
lines changed

libs/langchain-anthropic/src/chat_models.ts

Lines changed: 24 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -138,8 +138,10 @@ export interface AnthropicInput {
138138
* from 0 to 1. Use temp closer to 0 for analytical /
139139
* multiple choice, and temp closer to 1 for creative
140140
* and generative tasks.
141+
* To not set this field, pass `null`. If `undefined` is passed,
142+
* the default (1) will be used.
141143
*/
142-
temperature?: number;
144+
temperature?: number | null;
143145

144146
/** Only sample from the top K options for each subsequent
145147
* token. Used to remove "long tail" low probability
@@ -154,8 +156,13 @@ export interface AnthropicInput {
154156
* specified by top_p. Defaults to -1, which disables it.
155157
* Note that you should either alter temperature or top_p,
156158
* but not both.
159+
*
160+
* To not set this field, pass `null`. If `undefined` is passed,
161+
* the default (-1) will be used.
162+
*
163+
* For Opus 4.1, this defaults to `null`.
157164
*/
158-
topP?: number;
165+
topP?: number | null;
159166

160167
/** A maximum number of tokens to generate before stopping. */
161168
maxTokens?: number;
@@ -652,11 +659,11 @@ export class ChatAnthropicMessages<
652659

653660
apiUrl?: string;
654661

655-
temperature = 1;
662+
temperature: number | undefined = 1;
656663

657664
topK = -1;
658665

659-
topP = -1;
666+
topP: number | undefined = -1;
660667

661668
maxTokens = 2048;
662669

@@ -713,9 +720,20 @@ export class ChatAnthropicMessages<
713720

714721
this.invocationKwargs = fields?.invocationKwargs ?? {};
715722

716-
this.temperature = fields?.temperature ?? this.temperature;
723+
if (this.model.includes("opus-4-1")) {
724+
// Default to `undefined` for `topP` for Opus 4.1 models
725+
this.topP = fields?.topP === null ? undefined : fields?.topP;
726+
} else {
727+
this.topP = fields?.topP ?? this.topP;
728+
}
729+
730+
// If the user passes `null`, set it to `undefined`. Otherwise, use their value or the default. We have to check for null, because
731+
// there's no way for us to know if they explicitly set it to `undefined`, or never passed a value
732+
this.temperature =
733+
fields?.temperature === null
734+
? undefined
735+
: fields?.temperature ?? this.temperature;
717736
this.topK = fields?.topK ?? this.topK;
718-
this.topP = fields?.topP ?? this.topP;
719737
this.maxTokens =
720738
fields?.maxTokensToSample ?? fields?.maxTokens ?? this.maxTokens;
721739
this.stopSequences = fields?.stopSequences ?? this.stopSequences;

libs/langchain-anthropic/src/tests/chat_models.int.test.ts

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1462,3 +1462,15 @@ test("Can handle google function calling blocks in content", async () => {
14621462
const res = await chat.invoke(messages);
14631463
expect(res.content.length).toBeGreaterThan(1);
14641464
});
1465+
1466+
test("Can handle opus 4.1 without passing any args", async () => {
1467+
const model = new ChatAnthropic({
1468+
model: "claude-opus-4-1",
1469+
});
1470+
1471+
const response = await model.invoke(
1472+
"Please respond to this message simply with: Hello"
1473+
);
1474+
1475+
expect(response.content.length).toBeGreaterThan(0);
1476+
});

0 commit comments

Comments
 (0)