@ai-sdk/anthropic 4.0.110 → 4.0.112
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/CHANGELOG.md +13 -0
- package/dist/index.d.ts +1 -0
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +25 -4
- package/dist/index.js.map +1 -1
- package/dist/internal/index.d.ts +6 -2
- package/dist/internal/index.d.ts.map +1 -1
- package/dist/internal/index.js +24 -3
- package/dist/internal/index.js.map +1 -1
- package/docs/05-anthropic.mdx +23 -0
- package/package.json +2 -2
- package/src/anthropic-api.ts +2 -2
- package/src/anthropic-language-model-options.ts +10 -0
- package/src/anthropic-language-model.ts +2 -0
- package/src/anthropic-model-catalog.ts +3 -0
- package/src/convert-to-anthropic-prompt.ts +35 -4
package/docs/05-anthropic.mdx
CHANGED
|
@@ -439,6 +439,15 @@ The following optional provider options are available for Anthropic models:
|
|
|
439
439
|
Optional. Defaults to `false`. Emit typed `ping` parts during streaming without
|
|
440
440
|
enabling raw chunks. See [Streaming heartbeats](#streaming-heartbeats).
|
|
441
441
|
|
|
442
|
+
- `midConversationSystemMessages` _boolean_
|
|
443
|
+
|
|
444
|
+
Optional. Defaults to `false`. Keeps a system message that follows user or
|
|
445
|
+
assistant messages in `messages` even when no system message comes before
|
|
446
|
+
it. By default the first system message becomes the top-level `system`
|
|
447
|
+
prompt wherever it appears. Enable this only for models that support
|
|
448
|
+
mid-conversation system messages. See
|
|
449
|
+
[Mid-Conversation System Controls](#mid-conversation-system-controls).
|
|
450
|
+
|
|
442
451
|
- `disableParallelToolUse` _boolean_
|
|
443
452
|
|
|
444
453
|
Optional. Disables the use of parallel tool calls. Defaults to `false`.
|
|
@@ -2175,6 +2184,20 @@ follow Anthropic's [content placement rules](https://platform.claude.com/docs/en
|
|
|
2175
2184
|
Turn-scoped messages with `clearAt` support text only. Put effort updates in a
|
|
2176
2185
|
separate message, as in the example above.
|
|
2177
2186
|
|
|
2187
|
+
The first system message of a prompt becomes the top-level `system` field even
|
|
2188
|
+
when it follows other messages. To keep such a message in place, for example a
|
|
2189
|
+
reminder appended to a conversation that has no initial instructions, set
|
|
2190
|
+
`providerOptions.anthropic.midConversationSystemMessages: true` on the request.
|
|
2191
|
+
|
|
2192
|
+
Use the same value on every request of a conversation. Changing it moves the
|
|
2193
|
+
message between the top-level `system` field and `messages`, so the next request
|
|
2194
|
+
misses the prompt cache, and thinking blocks from earlier turns can fail
|
|
2195
|
+
Anthropic's [prefix check](https://platform.claude.com/docs/en/build-with-claude/preserved-thinking/prefix-checks).
|
|
2196
|
+
|
|
2197
|
+
A `cacheControl` provider option on a mid-conversation system message marks
|
|
2198
|
+
its text block. When the message has no text and only carries `toolChanges`,
|
|
2199
|
+
it marks the last tool change instead.
|
|
2200
|
+
|
|
2178
2201
|
<Note>
|
|
2179
2202
|
`clearAt` applies only to mid-conversation system messages. On initial system
|
|
2180
2203
|
messages, the provider ignores `clearAt` and effort attached to non-empty
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@ai-sdk/anthropic",
|
|
3
|
-
"version": "4.0.
|
|
3
|
+
"version": "4.0.112",
|
|
4
4
|
"type": "module",
|
|
5
5
|
"license": "Apache-2.0",
|
|
6
6
|
"sideEffects": false,
|
|
@@ -36,7 +36,7 @@
|
|
|
36
36
|
},
|
|
37
37
|
"dependencies": {
|
|
38
38
|
"@ai-sdk/provider": "4.0.39",
|
|
39
|
-
"@ai-sdk/provider-utils": "5.0.
|
|
39
|
+
"@ai-sdk/provider-utils": "5.0.86"
|
|
40
40
|
},
|
|
41
41
|
"devDependencies": {
|
|
42
42
|
"@ai-sdk/test-server": "2.0.4",
|
package/src/anthropic-api.ts
CHANGED
|
@@ -43,7 +43,7 @@ export type AnthropicToolChangeContent =
|
|
|
43
43
|
| {
|
|
44
44
|
type: 'tool_addition' | 'tool_removal';
|
|
45
45
|
tool: { type: 'tool_reference'; name: string };
|
|
46
|
-
cache_control?:
|
|
46
|
+
cache_control?: AnthropicCacheControl;
|
|
47
47
|
}
|
|
48
48
|
| {
|
|
49
49
|
type: 'tool_addition';
|
|
@@ -53,7 +53,7 @@ export type AnthropicToolChangeContent =
|
|
|
53
53
|
* from this point on.
|
|
54
54
|
*/
|
|
55
55
|
tool: { type: 'tool_definition'; definition: AnthropicTool };
|
|
56
|
-
cache_control?:
|
|
56
|
+
cache_control?: AnthropicCacheControl;
|
|
57
57
|
};
|
|
58
58
|
|
|
59
59
|
export interface AnthropicUserMessage {
|
|
@@ -179,6 +179,16 @@ export const anthropicLanguageModelOptions = lazySchema(() =>
|
|
|
179
179
|
/** Emit typed streaming heartbeat parts. Disabled by default. */
|
|
180
180
|
includePings: z.boolean().optional(),
|
|
181
181
|
|
|
182
|
+
/**
|
|
183
|
+
* Keep a system message that follows user or assistant messages in the
|
|
184
|
+
* `messages` array, even when no system message comes before it. By
|
|
185
|
+
* default the first system message becomes the top-level `system`
|
|
186
|
+
* prompt wherever it appears, which models without mid-conversation
|
|
187
|
+
* system message support require. Enable this only for models that
|
|
188
|
+
* support mid-conversation system messages.
|
|
189
|
+
*/
|
|
190
|
+
midConversationSystemMessages: z.boolean().optional(),
|
|
191
|
+
|
|
182
192
|
/**
|
|
183
193
|
* Configuration for enabling Claude's extended thinking.
|
|
184
194
|
*
|
|
@@ -759,6 +759,8 @@ export class AnthropicLanguageModel implements LanguageModelV4 {
|
|
|
759
759
|
toolNameMapping,
|
|
760
760
|
toolsetNames,
|
|
761
761
|
convertMediaPart: config.convertMediaPart,
|
|
762
|
+
keepMidConversationSystemMessages:
|
|
763
|
+
anthropicOptions?.midConversationSystemMessages ?? false,
|
|
762
764
|
convertInlineTool: async tool => {
|
|
763
765
|
const {
|
|
764
766
|
tools: definitions,
|
|
@@ -18,6 +18,9 @@ export const anthropicProviderOptions = {
|
|
|
18
18
|
thread: { description: 'Create or continue a short-lived message thread.' },
|
|
19
19
|
sendReasoning: { description: 'Whether to send reasoning to the model.' },
|
|
20
20
|
includePings: { description: 'Include transient ping events in streams.' },
|
|
21
|
+
midConversationSystemMessages: {
|
|
22
|
+
description: 'Keep later system messages in the conversation.',
|
|
23
|
+
},
|
|
21
24
|
structuredOutputMode: {
|
|
22
25
|
description: 'How structured outputs are generated.',
|
|
23
26
|
},
|
|
@@ -161,6 +161,7 @@ export async function convertToAnthropicPrompt({
|
|
|
161
161
|
toolsetNames = {},
|
|
162
162
|
convertMediaPart,
|
|
163
163
|
convertInlineTool,
|
|
164
|
+
keepMidConversationSystemMessages = false,
|
|
164
165
|
}: {
|
|
165
166
|
prompt: LanguageModelV4Prompt;
|
|
166
167
|
sendReasoning: boolean;
|
|
@@ -181,6 +182,13 @@ export async function convertToAnthropicPrompt({
|
|
|
181
182
|
* Required when the prompt contains such additions.
|
|
182
183
|
*/
|
|
183
184
|
convertInlineTool?: AnthropicInlineToolConverter;
|
|
185
|
+
|
|
186
|
+
/**
|
|
187
|
+
* Keeps a system message that follows other messages in `messages` even
|
|
188
|
+
* when no system message comes before it, instead of making it the
|
|
189
|
+
* top-level `system` prompt.
|
|
190
|
+
*/
|
|
191
|
+
keepMidConversationSystemMessages?: boolean;
|
|
184
192
|
}): Promise<{
|
|
185
193
|
prompt: AnthropicPrompt;
|
|
186
194
|
betas: Set<string>;
|
|
@@ -258,12 +266,12 @@ export async function convertToAnthropicPrompt({
|
|
|
258
266
|
|
|
259
267
|
// A system message that only carries message-level controls may have
|
|
260
268
|
// empty text; do not emit an empty text block for it.
|
|
261
|
-
|
|
269
|
+
const emitsText =
|
|
262
270
|
text !== '' ||
|
|
263
271
|
(toolChanges.length === 0 &&
|
|
264
272
|
systemMessageOptions?.clearAt == null &&
|
|
265
|
-
systemMessageOptions?.effort == null)
|
|
266
|
-
) {
|
|
273
|
+
systemMessageOptions?.effort == null);
|
|
274
|
+
if (emitsText) {
|
|
267
275
|
content.push({
|
|
268
276
|
type: 'text' as const,
|
|
269
277
|
text,
|
|
@@ -334,6 +342,26 @@ export async function convertToAnthropicPrompt({
|
|
|
334
342
|
toolChangeCount++;
|
|
335
343
|
}
|
|
336
344
|
|
|
345
|
+
// Without a text block, the message's cache control marks its last
|
|
346
|
+
// tool change, so the cached prefix ends where this message ends.
|
|
347
|
+
// A definition with its own cache control already ends the prefix
|
|
348
|
+
// there, and Anthropic rejects a marker in both places.
|
|
349
|
+
const lastToolChange = content.at(-1);
|
|
350
|
+
const definesOwnCacheControl =
|
|
351
|
+
lastToolChange?.type === 'tool_addition' &&
|
|
352
|
+
lastToolChange.tool.type === 'tool_definition' &&
|
|
353
|
+
'cache_control' in lastToolChange.tool.definition &&
|
|
354
|
+
lastToolChange.tool.definition.cache_control != null;
|
|
355
|
+
if (!emitsText && lastToolChange != null && !definesOwnCacheControl) {
|
|
356
|
+
const cacheControl = validator.getCacheControl(providerOptions, {
|
|
357
|
+
type: 'system message',
|
|
358
|
+
canCache: true,
|
|
359
|
+
});
|
|
360
|
+
if (cacheControl != null) {
|
|
361
|
+
lastToolChange.cache_control = cacheControl;
|
|
362
|
+
}
|
|
363
|
+
}
|
|
364
|
+
|
|
337
365
|
// Every change was ignored and nothing else is left to send.
|
|
338
366
|
if (
|
|
339
367
|
content.length === 0 &&
|
|
@@ -361,7 +389,10 @@ export async function convertToAnthropicPrompt({
|
|
|
361
389
|
);
|
|
362
390
|
if (
|
|
363
391
|
i === 0 ||
|
|
364
|
-
(
|
|
392
|
+
(!keepMidConversationSystemMessages &&
|
|
393
|
+
system == null &&
|
|
394
|
+
toolChangeCount === 0 &&
|
|
395
|
+
!hasInlineSystemOptions)
|
|
365
396
|
) {
|
|
366
397
|
if (toolChangeCount > 0) {
|
|
367
398
|
warnings.push({
|