From 5665543a3b2bdccb91f7fa1110a26aa390c61020 Mon Sep 17 00:00:00 2001 From: ellispinsky Date: Sun, 12 Oct 2025 16:44:39 -0700 Subject: [PATCH 01/36] feat(anthropic): update code execution tool to version 20250825 with enhanced Bash support and add new result types for text editor and Bash execution --- .changeset/nine-eggs-retire.md | 5 ++ .../01-ai-sdk-providers/05-anthropic.mdx | 13 +++- .../anthropic-code-execution-latest.ts | 23 ++++++ .../anthropic-code-execution-latest.ts | 53 ++++++++++++++ package.json | 2 +- .../anthropic/src/anthropic-messages-api.ts | 65 +++++++++++++++++ .../src/anthropic-messages-language-model.ts | 71 ++++++++++++++++++- .../anthropic/src/anthropic-prepare-tools.ts | 8 +++ packages/anthropic/src/anthropic-tools.ts | 15 ++++ .../convert-to-anthropic-messages-prompt.ts | 1 + .../src/tool/code-execution_20250825.ts | 52 ++++++++++++++ 11 files changed, 304 insertions(+), 4 deletions(-) create mode 100644 .changeset/nine-eggs-retire.md create mode 100644 examples/ai-core/src/generate-text/anthropic-code-execution-latest.ts create mode 100644 examples/ai-core/src/stream-text/anthropic-code-execution-latest.ts create mode 100644 packages/anthropic/src/tool/code-execution_20250825.ts diff --git a/.changeset/nine-eggs-retire.md b/.changeset/nine-eggs-retire.md new file mode 100644 index 000000000000..c4236de798b0 --- /dev/null +++ b/.changeset/nine-eggs-retire.md @@ -0,0 +1,5 @@ +--- +'@ai-sdk/anthropic': minor +--- + +Add support for 2025-08-25 code execution tool diff --git a/content/providers/01-ai-sdk-providers/05-anthropic.mdx b/content/providers/01-ai-sdk-providers/05-anthropic.mdx index 6343a12cac55..f37ef007956f 100644 --- a/content/providers/01-ai-sdk-providers/05-anthropic.mdx +++ b/content/providers/01-ai-sdk-providers/05-anthropic.mdx @@ -547,7 +547,13 @@ You can enable code execution using the provider-defined code execution tool: ```ts import { anthropic } from '@ai-sdk/anthropic'; import { generateText } from 'ai'; -const codeExecutionTool = anthropic.tools.codeExecution_20250522(); + +// Latest version with enhanced Bash support and file operations +const codeExecutionTool = anthropic.tools.codeExecution_20250825(); + +// Or use the previous version if needed +// const codeExecutionTool = anthropic.tools.codeExecution_20250522(); + const result = await generateText({ model: anthropic('claude-opus-4-20250514'), prompt: @@ -558,6 +564,11 @@ const result = await generateText({ }); ``` +### Available Versions + +- **`codeExecution_20250825()`** - Latest version with enhanced Bash command support and file operations (recommended) +- **`codeExecution_20250522()`** - Previous version + #### Error Handling Code execution errors are handled differently depending on whether you're using streaming or non-streaming: diff --git a/examples/ai-core/src/generate-text/anthropic-code-execution-latest.ts b/examples/ai-core/src/generate-text/anthropic-code-execution-latest.ts new file mode 100644 index 000000000000..bf0bab1a06e0 --- /dev/null +++ b/examples/ai-core/src/generate-text/anthropic-code-execution-latest.ts @@ -0,0 +1,23 @@ +import { anthropic } from '@ai-sdk/anthropic'; +import { generateText } from 'ai'; +import 'dotenv/config'; + +async function main() { + // Using the latest code execution tool with enhanced Bash and file operation support + const codeExecutionTool = anthropic.tools.codeExecution_20250825(); + + const result = await generateText({ + model: anthropic('claude-opus-4-20250514'), + prompt: + 'Calculate the mean and standard deviation of [1, 2, 3, 4, 5, 6, 7, 8, 9, 10] using Python', + tools: { + code_execution: codeExecutionTool, + }, + }); + + console.log('Result:', result.text); + console.log('\nTool Calls:', JSON.stringify(result.toolCalls, null, 2)); + console.log('\nTool Results:', JSON.stringify(result.toolResults, null, 2)); +} + +main().catch(console.error); diff --git a/examples/ai-core/src/stream-text/anthropic-code-execution-latest.ts b/examples/ai-core/src/stream-text/anthropic-code-execution-latest.ts new file mode 100644 index 000000000000..8fa51564300a --- /dev/null +++ b/examples/ai-core/src/stream-text/anthropic-code-execution-latest.ts @@ -0,0 +1,53 @@ +import { anthropic } from '@ai-sdk/anthropic'; +import { streamText } from 'ai'; +import 'dotenv/config'; + +async function main() { + // Using the latest code execution tool with enhanced Bash and file operation support + const codeExecutionTool = anthropic.tools.codeExecution_20250825(); + + const result = streamText({ + model: anthropic('claude-opus-4-20250514'), + prompt: + 'Write a Python script to calculate fibonacci numbers and then execute it to find the 10th fibonacci number', + tools: { + code_execution: codeExecutionTool, + }, + }); + + process.stdout.write('\nAssistant: '); + + for await (const part of result.fullStream) { + switch (part.type) { + case 'text-delta': { + process.stdout.write(part.text); + break; + } + + case 'tool-call': { + process.stdout.write( + `\n\nTool call: '${part.toolName}'\nInput: ${JSON.stringify(part.input, null, 2)}\n`, + ); + break; + } + + case 'tool-result': { + process.stdout.write( + `\nTool result: '${part.toolName}'\nOutput: ${JSON.stringify(part.output, null, 2)}\n`, + ); + break; + } + + case 'error': { + console.error('\n\nCode execution error:', part.error); + break; + } + } + } + + process.stdout.write('\n\n'); + console.log('Finish reason:', await result.finishReason); + console.log('Usage:', await result.usage); +} + +main().catch(console.error); diff --git a/package.json b/package.json index ec8bc57a8110..2e1aa15a9a0a 100644 --- a/package.json +++ b/package.json @@ -7,7 +7,7 @@ "build:packages": "turbo build --filter=@ai-sdk/* --filter=ai", "changeset": "changeset", "clean": "turbo clean", - "dev": "turbo dev --no-cache --concurrency 16 --continue", + "dev": "turbo dev --no-cache --concurrency 20 --continue", "lint": "turbo lint", "prepare": "husky install", "update-references": "update-ts-references", diff --git a/packages/anthropic/src/anthropic-messages-api.ts b/packages/anthropic/src/anthropic-messages-api.ts index bad29cec3b62..9e7247e100d4 100644 --- a/packages/anthropic/src/anthropic-messages-api.ts +++ b/packages/anthropic/src/anthropic-messages-api.ts @@ -140,6 +140,29 @@ export interface AnthropicCodeExecutionToolResultContent { cache_control: AnthropicCacheControl | undefined; } +export interface AnthropicTextEditorCodeExecutionToolResultContent { + type: 'text_editor_code_execution_tool_result'; + tool_use_id: string; + content: { + type: 'text_editor_code_execution_create_result'; + is_file_update: boolean; + }; + cache_control: AnthropicCacheControl | undefined; +} + +export interface AnthropicBashCodeExecutionToolResultContent { + type: 'bash_code_execution_tool_result'; + tool_use_id: string; + content: { + type: 'bash_code_execution_result'; + stdout: string; + stderr: string; + return_code: number; + content: unknown[]; + }; + cache_control: AnthropicCacheControl | undefined; +} + export interface AnthropicWebFetchToolResultContent { type: 'web_fetch_tool_result'; tool_use_id: string; @@ -170,6 +193,10 @@ export type AnthropicTool = type: 'code_execution_20250522'; name: string; } + | { + type: 'code_execution_20250825'; + name: string; + } | { name: string; type: 'computer_20250124' | 'computer_20241022'; @@ -345,6 +372,25 @@ export const anthropicMessagesResponseSchema = lazySchema(() => }), ]), }), + z.object({ + type: z.literal('text_editor_code_execution_tool_result'), + tool_use_id: z.string(), + content: z.object({ + type: z.literal('text_editor_code_execution_create_result'), + is_file_update: z.boolean(), + }), + }), + z.object({ + type: z.literal('bash_code_execution_tool_result'), + tool_use_id: z.string(), + content: z.object({ + type: z.literal('bash_code_execution_result'), + stdout: z.string(), + stderr: z.string(), + return_code: z.number(), + content: z.array(z.unknown()), + }), + }), ]), ), stop_reason: z.string().nullish(), @@ -463,6 +509,25 @@ export const anthropicMessagesChunkSchema = lazySchema(() => }), ]), }), + z.object({ + type: z.literal('text_editor_code_execution_tool_result'), + tool_use_id: z.string(), + content: z.object({ + type: z.literal('text_editor_code_execution_create_result'), + is_file_update: z.boolean(), + }), + }), + z.object({ + type: z.literal('bash_code_execution_tool_result'), + tool_use_id: z.string(), + content: z.object({ + type: z.literal('bash_code_execution_result'), + stdout: z.string(), + stderr: z.string(), + return_code: z.number(), + content: z.array(z.unknown()), + }), + }), ]), }), z.object({ diff --git a/packages/anthropic/src/anthropic-messages-language-model.ts b/packages/anthropic/src/anthropic-messages-language-model.ts index c3bcf4a80bd7..c0772f1b0c4e 100644 --- a/packages/anthropic/src/anthropic-messages-language-model.ts +++ b/packages/anthropic/src/anthropic-messages-language-model.ts @@ -464,7 +464,9 @@ export class AnthropicMessagesLanguageModel implements LanguageModelV3 { if ( part.name === 'web_search' || part.name === 'code_execution' || - part.name === 'web_fetch' + part.name === 'web_fetch' || + part.name === 'text_editor_code_execution' || + part.name === 'bash_code_execution' ) { content.push({ type: 'tool-call', @@ -589,6 +591,34 @@ export class AnthropicMessagesLanguageModel implements LanguageModelV3 { } break; } + case 'text_editor_code_execution_tool_result': { + content.push({ + type: 'tool-result', + toolCallId: part.tool_use_id, + toolName: 'text_editor_code_execution', + result: { + type: part.content.type, + is_file_update: part.content.is_file_update, + }, + providerExecuted: true, + }); + break; + } + case 'bash_code_execution_tool_result': { + content.push({ + type: 'tool-result', + toolCallId: part.tool_use_id, + toolName: 'bash_code_execution', + result: { + type: part.content.type, + stdout: part.content.stdout, + stderr: part.content.stderr, + return_code: part.content.return_code, + }, + providerExecuted: true, + }); + break; + } } } @@ -678,6 +708,8 @@ export class AnthropicMessagesLanguageModel implements LanguageModelV3 { | 'web_fetch_tool_result' | 'web_search_tool_result' | 'code_execution_tool_result' + | 'text_editor_code_execution_tool_result' + | 'bash_code_execution_tool_result' | undefined = undefined; const generateId = this.generateId; @@ -773,7 +805,10 @@ export class AnthropicMessagesLanguageModel implements LanguageModelV3 { if ( value.content_block.name === 'web_fetch' || value.content_block.name === 'web_search' || - value.content_block.name === 'code_execution' + value.content_block.name === 'code_execution' || + value.content_block.name === + 'text_editor_code_execution' || + value.content_block.name === 'bash_code_execution' ) { contentBlocks[value.index] = { type: 'tool-call', @@ -919,6 +954,38 @@ export class AnthropicMessagesLanguageModel implements LanguageModelV3 { return; } + case 'text_editor_code_execution_tool_result': { + const part = value.content_block; + controller.enqueue({ + type: 'tool-result', + toolCallId: part.tool_use_id, + toolName: 'text_editor_code_execution', + result: { + type: part.content.type, + is_file_update: part.content.is_file_update, + }, + providerExecuted: true, + }); + return; + } + + case 'bash_code_execution_tool_result': { + const part = value.content_block; + controller.enqueue({ + type: 'tool-result', + toolCallId: part.tool_use_id, + toolName: 'bash_code_execution', + result: { + type: part.content.type, + stdout: part.content.stdout, + stderr: part.content.stderr, + return_code: part.content.return_code, + }, + providerExecuted: true, + }); + return; + } + default: { const _exhaustiveCheck: never = contentBlockType; throw new Error( diff --git a/packages/anthropic/src/anthropic-prepare-tools.ts b/packages/anthropic/src/anthropic-prepare-tools.ts index 130de75aa970..d5a71e323fd0 100644 --- a/packages/anthropic/src/anthropic-prepare-tools.ts +++ b/packages/anthropic/src/anthropic-prepare-tools.ts @@ -60,6 +60,14 @@ export async function prepareTools({ }); break; } + case 'anthropic.code_execution_20250825': { + betas.add('code-execution-2025-08-25'); + anthropicTools.push({ + type: 'code_execution_20250825', + name: 'code_execution', + }); + break; + } case 'anthropic.computer_20250124': { betas.add('computer-use-2025-01-24'); anthropicTools.push({ diff --git a/packages/anthropic/src/anthropic-tools.ts b/packages/anthropic/src/anthropic-tools.ts index 75cd8abc27c9..2b578d06f4bd 100644 --- a/packages/anthropic/src/anthropic-tools.ts +++ b/packages/anthropic/src/anthropic-tools.ts @@ -1,6 +1,7 @@ import { bash_20241022 } from './tool/bash_20241022'; import { bash_20250124 } from './tool/bash_20250124'; import { codeExecution_20250522 } from './tool/code-execution_20250522'; +import { codeExecution_20250825 } from './tool/code-execution_20250825'; import { computer_20241022 } from './tool/computer_20241022'; import { computer_20250124 } from './tool/computer_20250124'; import { textEditor_20241022 } from './tool/text-editor_20241022'; @@ -43,6 +44,20 @@ export const anthropicTools = { */ codeExecution_20250522, + /** + * Claude can analyze data, create visualizations, perform complex calculations, + * run system commands, create and edit files, and process uploaded files directly within + * the API conversation. + * + * The code execution tool allows Claude to run both Python and Bash commands and manipulate files, + * including writing code, in a secure, sandboxed environment. + * + * This is the latest version with enhanced Bash support and file operations. + * + * Tool name must be `code_execution`. + */ + codeExecution_20250825, + /** * Claude can interact with computer environments through the computer use tool, which * provides screenshot capabilities and mouse/keyboard control for autonomous desktop interaction. diff --git a/packages/anthropic/src/convert-to-anthropic-messages-prompt.ts b/packages/anthropic/src/convert-to-anthropic-messages-prompt.ts index ab69ea82e426..3cc795b08e19 100644 --- a/packages/anthropic/src/convert-to-anthropic-messages-prompt.ts +++ b/packages/anthropic/src/convert-to-anthropic-messages-prompt.ts @@ -22,6 +22,7 @@ import { import { anthropicFilePartProviderOptions } from './anthropic-messages-options'; import { getCacheControl } from './get-cache-control'; import { codeExecution_20250522OutputSchema } from './tool/code-execution_20250522'; +import { codeExecution_20250825OutputSchema } from './tool/code-execution_20250825'; import { webFetch_20250910OutputSchema } from './tool/web-fetch-20250910'; import { webSearch_20250305OutputSchema } from './tool/web-search_20250305'; diff --git a/packages/anthropic/src/tool/code-execution_20250825.ts b/packages/anthropic/src/tool/code-execution_20250825.ts new file mode 100644 index 000000000000..c594b59ad411 --- /dev/null +++ b/packages/anthropic/src/tool/code-execution_20250825.ts @@ -0,0 +1,52 @@ +import { + createProviderDefinedToolFactoryWithOutputSchema, + lazySchema, + zodSchema, +} from '@ai-sdk/provider-utils'; +import { z } from 'zod/v4'; + +export const codeExecution_20250825OutputSchema = lazySchema(() => + zodSchema( + z.object({ + type: z.literal('code_execution_result'), + stdout: z.string(), + stderr: z.string(), + return_code: z.number(), + }), + ), +); + +const codeExecution_20250825InputSchema = lazySchema(() => + zodSchema( + z.object({ + code: z.string(), + }), + ), +); + +const factory = createProviderDefinedToolFactoryWithOutputSchema< + { + /** + * The code to execute. Supports both Python and Bash commands. + */ + code: string; + }, + { + type: 'code_execution_result'; + stdout: string; + stderr: string; + return_code: number; + }, + {} +>({ + id: 'anthropic.code_execution_20250825', + name: 'code_execution', + inputSchema: codeExecution_20250825InputSchema, + outputSchema: codeExecution_20250825OutputSchema, +}); + +export const codeExecution_20250825 = ( + args: Parameters[0] = {}, +) => { + return factory(args); +}; From 17cb21bc4f544ce0fe4779f26908ad585cba9645 Mon Sep 17 00:00:00 2001 From: ellispinsky Date: Sun, 12 Oct 2025 22:47:49 -0700 Subject: [PATCH 02/36] fix(anthropic): update version from minor to patch for code execution tool support --- .changeset/nine-eggs-retire.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.changeset/nine-eggs-retire.md b/.changeset/nine-eggs-retire.md index c4236de798b0..7777feedaf19 100644 --- a/.changeset/nine-eggs-retire.md +++ b/.changeset/nine-eggs-retire.md @@ -1,5 +1,5 @@ --- -'@ai-sdk/anthropic': minor +'@ai-sdk/anthropic': patch --- Add support for 2025-08-25 code execution tool From d83114b8f0c3304d0a3718942d13aa7bbc841a45 Mon Sep 17 00:00:00 2001 From: Lars Grammel Date: Tue, 14 Oct 2025 09:39:36 +0200 Subject: [PATCH 03/36] revert --- package.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/package.json b/package.json index 2e1aa15a9a0a..ec8bc57a8110 100644 --- a/package.json +++ b/package.json @@ -7,7 +7,7 @@ "build:packages": "turbo build --filter=@ai-sdk/* --filter=ai", "changeset": "changeset", "clean": "turbo clean", - "dev": "turbo dev --no-cache --concurrency 20 --continue", + "dev": "turbo dev --no-cache --concurrency 16 --continue", "lint": "turbo lint", "prepare": "husky install", "update-references": "update-ts-references", From 85574487e2a103148b35db3384cded10a65f64e3 Mon Sep 17 00:00:00 2001 From: Lars Grammel Date: Tue, 14 Oct 2025 09:40:49 +0200 Subject: [PATCH 04/36] docs --- content/providers/01-ai-sdk-providers/05-anthropic.mdx | 4 ---- 1 file changed, 4 deletions(-) diff --git a/content/providers/01-ai-sdk-providers/05-anthropic.mdx b/content/providers/01-ai-sdk-providers/05-anthropic.mdx index f37ef007956f..e59d813a04ee 100644 --- a/content/providers/01-ai-sdk-providers/05-anthropic.mdx +++ b/content/providers/01-ai-sdk-providers/05-anthropic.mdx @@ -548,12 +548,8 @@ You can enable code execution using the provider-defined code execution tool: import { anthropic } from '@ai-sdk/anthropic'; import { generateText } from 'ai'; -// Latest version with enhanced Bash support and file operations const codeExecutionTool = anthropic.tools.codeExecution_20250825(); -// Or use the previous version if needed -// const codeExecutionTool = anthropic.tools.codeExecution_20250522(); - const result = await generateText({ model: anthropic('claude-opus-4-20250514'), prompt: From ffcd948a398926af8350c61ac3e58c12604414b1 Mon Sep 17 00:00:00 2001 From: Lars Grammel Date: Tue, 14 Oct 2025 09:41:14 +0200 Subject: [PATCH 05/36] docs --- content/providers/01-ai-sdk-providers/05-anthropic.mdx | 9 ++------- 1 file changed, 2 insertions(+), 7 deletions(-) diff --git a/content/providers/01-ai-sdk-providers/05-anthropic.mdx b/content/providers/01-ai-sdk-providers/05-anthropic.mdx index e59d813a04ee..c821f72e5002 100644 --- a/content/providers/01-ai-sdk-providers/05-anthropic.mdx +++ b/content/providers/01-ai-sdk-providers/05-anthropic.mdx @@ -544,7 +544,7 @@ Anthropic provides a provider-defined code execution tool that gives Claude dire You can enable code execution using the provider-defined code execution tool: -```ts +````ts import { anthropic } from '@ai-sdk/anthropic'; import { generateText } from 'ai'; @@ -560,11 +560,6 @@ const result = await generateText({ }); ``` -### Available Versions - -- **`codeExecution_20250825()`** - Latest version with enhanced Bash command support and file operations (recommended) -- **`codeExecution_20250522()`** - Previous version - #### Error Handling Code execution errors are handled differently depending on whether you're using streaming or non-streaming: @@ -592,7 +587,7 @@ toolErrors?.forEach(error => { error: error.error, }); }); -``` +```` **Streaming (`streamText`, `streamObject`):** Code execution errors are delivered as error parts in the stream: From c7e941445e3895b8ae828daeda8cbb2312ab3bcb Mon Sep 17 00:00:00 2001 From: Lars Grammel Date: Tue, 14 Oct 2025 09:41:47 +0200 Subject: [PATCH 06/36] fx --- content/providers/01-ai-sdk-providers/05-anthropic.mdx | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/content/providers/01-ai-sdk-providers/05-anthropic.mdx b/content/providers/01-ai-sdk-providers/05-anthropic.mdx index c821f72e5002..c7ada79b52e2 100644 --- a/content/providers/01-ai-sdk-providers/05-anthropic.mdx +++ b/content/providers/01-ai-sdk-providers/05-anthropic.mdx @@ -544,7 +544,7 @@ Anthropic provides a provider-defined code execution tool that gives Claude dire You can enable code execution using the provider-defined code execution tool: -````ts +```ts import { anthropic } from '@ai-sdk/anthropic'; import { generateText } from 'ai'; @@ -587,7 +587,7 @@ toolErrors?.forEach(error => { error: error.error, }); }); -```` +``` **Streaming (`streamText`, `streamObject`):** Code execution errors are delivered as error parts in the stream: From 50659920bc853c61579d219274cba2fac79c55a1 Mon Sep 17 00:00:00 2001 From: Lars Grammel Date: Tue, 14 Oct 2025 10:24:58 +0200 Subject: [PATCH 07/36] tool spec1 --- .../src/tool/code-execution_20250825.ts | 94 ++++++++++++++----- 1 file changed, 73 insertions(+), 21 deletions(-) diff --git a/packages/anthropic/src/tool/code-execution_20250825.ts b/packages/anthropic/src/tool/code-execution_20250825.ts index c594b59ad411..0e352ba63f84 100644 --- a/packages/anthropic/src/tool/code-execution_20250825.ts +++ b/packages/anthropic/src/tool/code-execution_20250825.ts @@ -7,37 +7,89 @@ import { z } from 'zod/v4'; export const codeExecution_20250825OutputSchema = lazySchema(() => zodSchema( - z.object({ - type: z.literal('code_execution_result'), - stdout: z.string(), - stderr: z.string(), - return_code: z.number(), - }), + z.discriminatedUnion('type', [ + z.object({ + type: z.literal('bash_code_execution_tool_result'), + content: z.object({ + type: z.literal('bash_code_execution_output'), + fileId: z.string(), + }), + stdout: z.string(), + stderr: z.string(), + returnCode: z.number(), + }), + z.object({ + type: z.literal('bash_code_execution_tool_result_error'), + errorCode: z.string(), + }), + z.object({ + type: z.literal('text_editor_code_execution_result'), + // TODO additional fields + }), + ]), ), ); const codeExecution_20250825InputSchema = lazySchema(() => zodSchema( - z.object({ - code: z.string(), - }), + z.discriminatedUnion('type', [ + z.object({ + type: z.literal('bash_code_execution'), + command: z.string(), + }), + z.object({ + type: z.literal('text_editor_code_execution'), + // TODO what is supported here? + }), + ]), ), ); const factory = createProviderDefinedToolFactoryWithOutputSchema< + | { + type: 'bash_code_execution'; + + /** + * Shell command to execute. + */ + command: string; + } + | { + type: 'text_editor_code_execution'; + }, + | { + type: 'bash_code_execution_tool_result'; + + /** + * Output from successful execution + */ + stdout: string; + + /** + * Error messages if execution fails + */ + stderr: string; + + /** + * 0 for success, non-zero for failure + */ + returnCode: number; + } + | { + type: 'bash_code_execution_tool_result_error'; + + /** + * Error code + */ + errorCode: string; + } + | { + type: 'text_editor_code_execution_result'; + // TODO + }, { - /** - * The code to execute. Supports both Python and Bash commands. - */ - code: string; - }, - { - type: 'code_execution_result'; - stdout: string; - stderr: string; - return_code: number; - }, - {} + // no arguments + } >({ id: 'anthropic.code_execution_20250825', name: 'code_execution', From 49f616a8750c3380875db25d8730440341f62f17 Mon Sep 17 00:00:00 2001 From: Lars Grammel Date: Tue, 14 Oct 2025 10:31:44 +0200 Subject: [PATCH 08/36] spec2 --- .../src/tool/code-execution_20250825.ts | 64 +++++++++++++++++-- 1 file changed, 59 insertions(+), 5 deletions(-) diff --git a/packages/anthropic/src/tool/code-execution_20250825.ts b/packages/anthropic/src/tool/code-execution_20250825.ts index 0e352ba63f84..c688555ec550 100644 --- a/packages/anthropic/src/tool/code-execution_20250825.ts +++ b/packages/anthropic/src/tool/code-execution_20250825.ts @@ -23,8 +23,28 @@ export const codeExecution_20250825OutputSchema = lazySchema(() => errorCode: z.string(), }), z.object({ - type: z.literal('text_editor_code_execution_result'), - // TODO additional fields + type: z.literal('text_editor_code_execution_tool_result_error'), + errorCode: z.string(), + }), + z.object({ + type: z.literal('text_editor_code_execution_view_result'), + content: z.string(), + fileType: z.string(), + numLines: z.number().nullable(), + startLine: z.number().nullable(), + totalLines: z.number().nullable(), + }), + z.object({ + type: z.literal('text_editor_code_execution_create_result'), + isFileUpdate: z.boolean(), + }), + z.object({ + type: z.literal('text_editor_code_execution_str_replace_result'), + lines: z.array(z.string()).nullable(), + newLines: z.number().nullable(), + newStart: z.number().nullable(), + oldLines: z.number().nullable(), + oldStart: z.number().nullable(), }), ]), ), @@ -79,13 +99,47 @@ const factory = createProviderDefinedToolFactoryWithOutputSchema< type: 'bash_code_execution_tool_result_error'; /** - * Error code + * Available options: invalid_tool_input, unavailable, too_many_requests, + * execution_time_exceeded, output_file_too_large. + */ + errorCode: string; + } + | { + type: 'text_editor_code_execution_tool_result_error'; + + /** + * Available options: invalid_tool_input, unavailable, too_many_requests, + * execution_time_exceeded, file_not_found. */ errorCode: string; } | { - type: 'text_editor_code_execution_result'; - // TODO + type: 'text_editor_code_execution_view_result'; + + content: string; + + /** + * The type of the file. Available options: text, image, pdf. + */ + fileType: string; + + numLines: number | null; + startLine: number | null; + totalLines: number | null; + } + | { + type: 'text_editor_code_execution_create_result'; + + isFileUpdate: boolean; + } + | { + type: 'text_editor_code_execution_str_replace_result'; + + lines: string[] | null; + newLines: number | null; + newStart: number | null; + oldLines: number | null; + oldStart: number | null; }, { // no arguments From 51a5af9bce0f54d0847b5fe1e2f23c9f3e6715d4 Mon Sep 17 00:00:00 2001 From: Lars Grammel Date: Tue, 14 Oct 2025 10:38:02 +0200 Subject: [PATCH 09/36] types1 --- .../anthropic/src/anthropic-messages-api.ts | 33 ++++++++++++------- 1 file changed, 22 insertions(+), 11 deletions(-) diff --git a/packages/anthropic/src/anthropic-messages-api.ts b/packages/anthropic/src/anthropic-messages-api.ts index 9e7247e100d4..084b6b21ce37 100644 --- a/packages/anthropic/src/anthropic-messages-api.ts +++ b/packages/anthropic/src/anthropic-messages-api.ts @@ -493,6 +493,28 @@ export const anthropicMessagesChunkSchema = lazySchema(() => }), ]), }), + z.object({ + type: z.literal('bash_code_execution_tool_result'), + tool_use_id: z.string(), + content: z.discriminatedUnion('type', [ + z.object({ + type: z.literal('bash_code_execution_result'), + content: z.object({ + type: z.literal('bash_code_execution_output'), + file_id: z.string(), + }), + stdout: z.string(), + stderr: z.string(), + return_code: z.number(), + }), + z.object({ + type: z.literal('bash_code_execution_tool_result_error'), + error_code: z.string(), + }), + ]), + }), + + // todo rework z.object({ type: z.literal('code_execution_tool_result'), tool_use_id: z.string(), @@ -517,17 +539,6 @@ export const anthropicMessagesChunkSchema = lazySchema(() => is_file_update: z.boolean(), }), }), - z.object({ - type: z.literal('bash_code_execution_tool_result'), - tool_use_id: z.string(), - content: z.object({ - type: z.literal('bash_code_execution_result'), - stdout: z.string(), - stderr: z.string(), - return_code: z.number(), - content: z.array(z.unknown()), - }), - }), ]), }), z.object({ From 8c75b93183e5ba521edd8576352f17b2cb1a7db0 Mon Sep 17 00:00:00 2001 From: Lars Grammel Date: Tue, 14 Oct 2025 10:48:53 +0200 Subject: [PATCH 10/36] types2 --- .../anthropic/src/anthropic-messages-api.ts | 61 +++++++++++++------ 1 file changed, 43 insertions(+), 18 deletions(-) diff --git a/packages/anthropic/src/anthropic-messages-api.ts b/packages/anthropic/src/anthropic-messages-api.ts index 084b6b21ce37..05773237d25a 100644 --- a/packages/anthropic/src/anthropic-messages-api.ts +++ b/packages/anthropic/src/anthropic-messages-api.ts @@ -493,51 +493,76 @@ export const anthropicMessagesChunkSchema = lazySchema(() => }), ]), }), + // code execution results for code_execution_20250522 tool: z.object({ - type: z.literal('bash_code_execution_tool_result'), + type: z.literal('code_execution_tool_result'), tool_use_id: z.string(), - content: z.discriminatedUnion('type', [ + content: z.union([ z.object({ - type: z.literal('bash_code_execution_result'), - content: z.object({ - type: z.literal('bash_code_execution_output'), - file_id: z.string(), - }), + type: z.literal('code_execution_result'), stdout: z.string(), stderr: z.string(), return_code: z.number(), }), z.object({ - type: z.literal('bash_code_execution_tool_result_error'), + type: z.literal('code_execution_tool_result_error'), error_code: z.string(), }), ]), }), - - // todo rework + // bash code execution results for code_execution_20250825 tool: z.object({ - type: z.literal('code_execution_tool_result'), + type: z.literal('bash_code_execution_tool_result'), tool_use_id: z.string(), - content: z.union([ + content: z.discriminatedUnion('type', [ z.object({ - type: z.literal('code_execution_result'), + type: z.literal('bash_code_execution_result'), + content: z.object({ + type: z.literal('bash_code_execution_output'), + file_id: z.string(), + }), stdout: z.string(), stderr: z.string(), return_code: z.number(), }), z.object({ - type: z.literal('code_execution_tool_result_error'), + type: z.literal('bash_code_execution_tool_result_error'), error_code: z.string(), }), ]), }), + // text editor code execution results for code_execution_20250825 tool: z.object({ type: z.literal('text_editor_code_execution_tool_result'), tool_use_id: z.string(), - content: z.object({ - type: z.literal('text_editor_code_execution_create_result'), - is_file_update: z.boolean(), - }), + content: z.discriminatedUnion('type', [ + z.object({ + type: z.literal('text_editor_code_execution_tool_result_error'), + error_code: z.string(), + }), + z.object({ + type: z.literal('text_editor_code_execution_view_result'), + content: z.string(), + file_type: z.string(), + num_lines: z.number().nullable(), + start_line: z.number().nullable(), + total_lines: z.number().nullable(), + }), + z.object({ + type: z.literal('text_editor_code_execution_create_result'), + is_file_update: z.boolean(), + }), + z.object({ + type: z.literal( + 'text_editor_code_execution_str_replace_result', + ), + lines: z.array(z.string()).nullable(), + new_lines: z.number().nullable(), + new_start: z.number().nullable(), + old_lines: z.number().nullable(), + old_start: z.number().nullable(), + }), + ]), }), ]), }), From e067773b1a2e577ff12f01875ba3f1c4444bd0ad Mon Sep 17 00:00:00 2001 From: Lars Grammel Date: Tue, 14 Oct 2025 10:50:29 +0200 Subject: [PATCH 11/36] types3 --- .../anthropic/src/anthropic-messages-api.ts | 62 +++++++++++++++---- 1 file changed, 49 insertions(+), 13 deletions(-) diff --git a/packages/anthropic/src/anthropic-messages-api.ts b/packages/anthropic/src/anthropic-messages-api.ts index 05773237d25a..697fdf5257c8 100644 --- a/packages/anthropic/src/anthropic-messages-api.ts +++ b/packages/anthropic/src/anthropic-messages-api.ts @@ -356,6 +356,7 @@ export const anthropicMessagesResponseSchema = lazySchema(() => }), ]), }), + // code execution results for code_execution_20250522 tool: z.object({ type: z.literal('code_execution_tool_result'), tool_use_id: z.string(), @@ -372,24 +373,59 @@ export const anthropicMessagesResponseSchema = lazySchema(() => }), ]), }), + // bash code execution results for code_execution_20250825 tool: z.object({ - type: z.literal('text_editor_code_execution_tool_result'), + type: z.literal('bash_code_execution_tool_result'), tool_use_id: z.string(), - content: z.object({ - type: z.literal('text_editor_code_execution_create_result'), - is_file_update: z.boolean(), - }), + content: z.discriminatedUnion('type', [ + z.object({ + type: z.literal('bash_code_execution_result'), + content: z.object({ + type: z.literal('bash_code_execution_output'), + file_id: z.string(), + }), + stdout: z.string(), + stderr: z.string(), + return_code: z.number(), + }), + z.object({ + type: z.literal('bash_code_execution_tool_result_error'), + error_code: z.string(), + }), + ]), }), + // text editor code execution results for code_execution_20250825 tool: z.object({ - type: z.literal('bash_code_execution_tool_result'), + type: z.literal('text_editor_code_execution_tool_result'), tool_use_id: z.string(), - content: z.object({ - type: z.literal('bash_code_execution_result'), - stdout: z.string(), - stderr: z.string(), - return_code: z.number(), - content: z.array(z.unknown()), - }), + content: z.discriminatedUnion('type', [ + z.object({ + type: z.literal('text_editor_code_execution_tool_result_error'), + error_code: z.string(), + }), + z.object({ + type: z.literal('text_editor_code_execution_view_result'), + content: z.string(), + file_type: z.string(), + num_lines: z.number().nullable(), + start_line: z.number().nullable(), + total_lines: z.number().nullable(), + }), + z.object({ + type: z.literal('text_editor_code_execution_create_result'), + is_file_update: z.boolean(), + }), + z.object({ + type: z.literal( + 'text_editor_code_execution_str_replace_result', + ), + lines: z.array(z.string()).nullable(), + new_lines: z.number().nullable(), + new_start: z.number().nullable(), + old_lines: z.number().nullable(), + old_start: z.number().nullable(), + }), + ]), }), ]), ), From c37ef5d3f228ba285c315259cc30d61ee49fff19 Mon Sep 17 00:00:00 2001 From: Lars Grammel Date: Tue, 14 Oct 2025 10:52:21 +0200 Subject: [PATCH 12/36] types4 --- .../anthropic/src/anthropic-messages-api.ts | 54 +++++++++++++++---- 1 file changed, 43 insertions(+), 11 deletions(-) diff --git a/packages/anthropic/src/anthropic-messages-api.ts b/packages/anthropic/src/anthropic-messages-api.ts index 697fdf5257c8..aaad59d7c4b8 100644 --- a/packages/anthropic/src/anthropic-messages-api.ts +++ b/packages/anthropic/src/anthropic-messages-api.ts @@ -128,6 +128,7 @@ export interface AnthropicWebSearchToolResultContent { cache_control: AnthropicCacheControl | undefined; } +// code execution results for code_execution_20250522 tool: export interface AnthropicCodeExecutionToolResultContent { type: 'code_execution_tool_result'; tool_use_id: string; @@ -140,26 +141,57 @@ export interface AnthropicCodeExecutionToolResultContent { cache_control: AnthropicCacheControl | undefined; } +// text editor code execution results for code_execution_20250825 tool: export interface AnthropicTextEditorCodeExecutionToolResultContent { type: 'text_editor_code_execution_tool_result'; tool_use_id: string; - content: { - type: 'text_editor_code_execution_create_result'; - is_file_update: boolean; - }; + content: + | { + type: 'text_editor_code_execution_tool_result_error'; + error_code: string; + } + | { + type: 'text_editor_code_execution_create_result'; + is_file_update: boolean; + } + | { + type: 'text_editor_code_execution_view_result'; + content: string; + file_type: string; + num_lines: number | null; + start_line: number | null; + total_lines: number | null; + } + | { + type: 'text_editor_code_execution_str_replace_result'; + lines: string[] | null; + new_lines: number | null; + new_start: number | null; + old_lines: number | null; + old_start: number | null; + }; cache_control: AnthropicCacheControl | undefined; } +// bash code execution results for code_execution_20250825 tool: export interface AnthropicBashCodeExecutionToolResultContent { type: 'bash_code_execution_tool_result'; tool_use_id: string; - content: { - type: 'bash_code_execution_result'; - stdout: string; - stderr: string; - return_code: number; - content: unknown[]; - }; + content: + | { + type: 'bash_code_execution_result'; + stdout: string; + stderr: string; + return_code: number; + content: { + type: 'bash_code_execution_output'; + file_id: string; + }[]; + } + | { + type: 'bash_code_execution_tool_result_error'; + error_code: string; + }; cache_control: AnthropicCacheControl | undefined; } From c2d5f080658dc0c9767a0c9ed4cd397c37c36198 Mon Sep 17 00:00:00 2001 From: Lars Grammel Date: Tue, 14 Oct 2025 10:56:52 +0200 Subject: [PATCH 13/36] ex1 --- ...s => anthropic-code-execution-20250825.ts} | 22 ++++++------------- 1 file changed, 7 insertions(+), 15 deletions(-) rename examples/ai-core/src/stream-text/{anthropic-code-execution-latest.ts => anthropic-code-execution-20250825.ts} (58%) diff --git a/examples/ai-core/src/stream-text/anthropic-code-execution-latest.ts b/examples/ai-core/src/stream-text/anthropic-code-execution-20250825.ts similarity index 58% rename from examples/ai-core/src/stream-text/anthropic-code-execution-latest.ts rename to examples/ai-core/src/stream-text/anthropic-code-execution-20250825.ts index 8fa51564300a..3898ce5f344f 100644 --- a/examples/ai-core/src/stream-text/anthropic-code-execution-latest.ts +++ b/examples/ai-core/src/stream-text/anthropic-code-execution-20250825.ts @@ -1,22 +1,18 @@ import { anthropic } from '@ai-sdk/anthropic'; import { streamText } from 'ai'; -import 'dotenv/config'; - -async function main() { - // Using the latest code execution tool with enhanced Bash and file operation support - const codeExecutionTool = anthropic.tools.codeExecution_20250825(); +import { run } from '../lib/run'; +run(async () => { const result = streamText({ - model: anthropic('claude-opus-4-20250514'), + model: anthropic('claude-sonnet-4-5'), prompt: - 'Write a Python script to calculate fibonacci numbers and then execute it to find the 10th fibonacci number', + 'Write a Python script to calculate fibonacci number' + + ' and then execute it to find the 10th fibonacci number', tools: { - code_execution: codeExecutionTool, + code_execution: anthropic.tools.codeExecution_20250825(), }, }); - process.stdout.write('\nAssistant: '); - for await (const part of result.fullStream) { switch (part.type) { case 'text-delta': { @@ -46,8 +42,4 @@ async function main() { } process.stdout.write('\n\n'); - console.log('Finish reason:', await result.finishReason); - console.log('Usage:', await result.usage); -} - -main().catch(console.error); +}); From 8187dc93dd8387bf474a5d45ca8cc02e6c070509 Mon Sep 17 00:00:00 2001 From: Lars Grammel Date: Tue, 14 Oct 2025 11:05:49 +0200 Subject: [PATCH 14/36] test1 --- .../anthropic-code-execution-20250825.ts | 35 +- ...ropic-code-execution-20250825.1.chunks.txt | 248 ++++ ...ropic-messages-language-model.test.ts.snap | 1279 +++++++++++++++++ .../anthropic-messages-language-model.test.ts | 21 + 4 files changed, 1554 insertions(+), 29 deletions(-) create mode 100644 packages/anthropic/src/__fixtures__/anthropic-code-execution-20250825.1.chunks.txt diff --git a/examples/ai-core/src/stream-text/anthropic-code-execution-20250825.ts b/examples/ai-core/src/stream-text/anthropic-code-execution-20250825.ts index 3898ce5f344f..9afa0ec070fa 100644 --- a/examples/ai-core/src/stream-text/anthropic-code-execution-20250825.ts +++ b/examples/ai-core/src/stream-text/anthropic-code-execution-20250825.ts @@ -1,6 +1,7 @@ import { anthropic } from '@ai-sdk/anthropic'; import { streamText } from 'ai'; import { run } from '../lib/run'; +import { saveRawChunks } from '../lib/save-raw-chunks'; run(async () => { const result = streamText({ @@ -11,35 +12,11 @@ run(async () => { tools: { code_execution: anthropic.tools.codeExecution_20250825(), }, + includeRawChunks: true, }); - for await (const part of result.fullStream) { - switch (part.type) { - case 'text-delta': { - process.stdout.write(part.text); - break; - } - - case 'tool-call': { - process.stdout.write( - `\n\nTool call: '${part.toolName}'\nInput: ${JSON.stringify(part.input, null, 2)}\n`, - ); - break; - } - - case 'tool-result': { - process.stdout.write( - `\nTool result: '${part.toolName}'\nOutput: ${JSON.stringify(part.output, null, 2)}\n`, - ); - break; - } - - case 'error': { - console.error('\n\nCode execution error:', part.error); - break; - } - } - } - - process.stdout.write('\n\n'); + await saveRawChunks({ + result, + filename: 'anthropic-code-execution-20250825', + }); }); diff --git a/packages/anthropic/src/__fixtures__/anthropic-code-execution-20250825.1.chunks.txt b/packages/anthropic/src/__fixtures__/anthropic-code-execution-20250825.1.chunks.txt new file mode 100644 index 000000000000..c7173b597278 --- /dev/null +++ b/packages/anthropic/src/__fixtures__/anthropic-code-execution-20250825.1.chunks.txt @@ -0,0 +1,248 @@ +{"type":"message_start","message":{"model":"claude-sonnet-4-5-20250929","id":"msg_01LEsrXVCLpf7xHaFdFTZNEJ","type":"message","role":"assistant","content":[],"stop_reason":null,"stop_sequence":null,"usage":{"input_tokens":2263,"cache_creation_input_tokens":0,"cache_read_input_tokens":0,"cache_creation":{"ephemeral_5m_input_tokens":0,"ephemeral_1h_input_tokens":0},"output_tokens":8,"service_tier":"standard"}}} +{"type":"content_block_start","index":0,"content_block":{"type":"text","text":""}} +{"type":"content_block_delta","index":0,"delta":{"type":"text_delta","text":"I'll create a Python script to calculate"}} +{"type":"content_block_delta","index":0,"delta":{"type":"text_delta","text":" Fibonacci numbers and then execute it to fin"}} +{"type":"ping"} +{"type":"content_block_delta","index":0,"delta":{"type":"text_delta","text":"d the 10th Fibonacci number."}} +{"type":"content_block_stop","index":0} +{"type":"content_block_start","index":1,"content_block":{"type":"server_tool_use","id":"srvtoolu_0112cP8RpnKv67t2cscmN4ia","name":"text_editor_code_execution","input":{}}} +{"type":"content_block_delta","index":1,"delta":{"type":"input_json_delta","partial_json":""}} +{"type":"content_block_delta","index":1,"delta":{"type":"input_json_delta","partial_json":"{\""}} +{"type":"content_block_delta","index":1,"delta":{"type":"input_json_delta","partial_json":"comma"}} +{"type":"content_block_delta","index":1,"delta":{"type":"input_json_delta","partial_json":"nd\": \"c"}} +{"type":"content_block_delta","index":1,"delta":{"type":"input_json_delta","partial_json":"reate\""}} +{"type":"content_block_delta","index":1,"delta":{"type":"input_json_delta","partial_json":", \""}} +{"type":"content_block_delta","index":1,"delta":{"type":"input_json_delta","partial_json":"path\": "}} +{"type":"content_block_delta","index":1,"delta":{"type":"input_json_delta","partial_json":"\"/"}} +{"type":"content_block_delta","index":1,"delta":{"type":"input_json_delta","partial_json":"tmp/fi"}} +{"type":"content_block_delta","index":1,"delta":{"type":"input_json_delta","partial_json":"bonacci."}} +{"type":"content_block_delta","index":1,"delta":{"type":"input_json_delta","partial_json":"py\""}} +{"type":"content_block_delta","index":1,"delta":{"type":"input_json_delta","partial_json":", \"file_"}} +{"type":"content_block_delta","index":1,"delta":{"type":"input_json_delta","partial_json":"text\":"}} +{"type":"content_block_delta","index":1,"delta":{"type":"input_json_delta","partial_json":" \"def fib"}} +{"type":"content_block_delta","index":1,"delta":{"type":"input_json_delta","partial_json":"onacci(n):"}} +{"type":"content_block_delta","index":1,"delta":{"type":"input_json_delta","partial_json":"\\n \\\"\\\"\\"}} +{"type":"content_block_delta","index":1,"delta":{"type":"input_json_delta","partial_json":"\"\\n Calc"}} +{"type":"content_block_delta","index":1,"delta":{"type":"input_json_delta","partial_json":"ulate th"}} +{"type":"content_block_delta","index":1,"delta":{"type":"input_json_delta","partial_json":"e nth Fibo"}} +{"type":"content_block_delta","index":1,"delta":{"type":"input_json_delta","partial_json":"nacc"}} +{"type":"content_block_delta","index":1,"delta":{"type":"input_json_delta","partial_json":"i "}} +{"type":"content_block_delta","index":1,"delta":{"type":"input_json_delta","partial_json":"numb"}} +{"type":"content_block_delta","index":1,"delta":{"type":"input_json_delta","partial_json":"er.\\n"}} +{"type":"content_block_delta","index":1,"delta":{"type":"input_json_delta","partial_json":" \\"}} +{"type":"content_block_delta","index":1,"delta":{"type":"input_json_delta","partial_json":"n Ar"}} +{"type":"content_block_delta","index":1,"delta":{"type":"input_json_delta","partial_json":"gs:"}} +{"type":"content_block_delta","index":1,"delta":{"type":"input_json_delta","partial_json":"\\n "}} +{"type":"content_block_delta","index":1,"delta":{"type":"input_json_delta","partial_json":" n: T"}} +{"type":"content_block_delta","index":1,"delta":{"type":"input_json_delta","partial_json":"he "}} +{"type":"content_block_delta","index":1,"delta":{"type":"input_json_delta","partial_json":"positio"}} +{"type":"content_block_delta","index":1,"delta":{"type":"input_json_delta","partial_json":"n i"}} +{"type":"content_block_delta","index":1,"delta":{"type":"input_json_delta","partial_json":"n the Fibon"}} +{"type":"content_block_delta","index":1,"delta":{"type":"input_json_delta","partial_json":"acci s"}} +{"type":"content_block_delta","index":1,"delta":{"type":"input_json_delta","partial_json":"eq"}} +{"type":"content_block_delta","index":1,"delta":{"type":"input_json_delta","partial_json":"uence (1-"}} +{"type":"content_block_delta","index":1,"delta":{"type":"input_json_delta","partial_json":"indexe"}} +{"type":"content_block_delta","index":1,"delta":{"type":"input_json_delta","partial_json":"d)\\n "}} +{"type":"content_block_delta","index":1,"delta":{"type":"input_json_delta","partial_json":" \\"}} +{"type":"content_block_delta","index":1,"delta":{"type":"input_json_delta","partial_json":"n "}} +{"type":"content_block_delta","index":1,"delta":{"type":"input_json_delta","partial_json":" Returns:\\"}} +{"type":"content_block_delta","index":1,"delta":{"type":"input_json_delta","partial_json":"n "}} +{"type":"content_block_delta","index":1,"delta":{"type":"input_json_delta","partial_json":" The n"}} +{"type":"content_block_delta","index":1,"delta":{"type":"input_json_delta","partial_json":"th "}} +{"type":"content_block_delta","index":1,"delta":{"type":"input_json_delta","partial_json":"Fibonac"}} +{"type":"content_block_delta","index":1,"delta":{"type":"input_json_delta","partial_json":"ci num"}} +{"type":"content_block_delta","index":1,"delta":{"type":"input_json_delta","partial_json":"be"}} +{"type":"content_block_delta","index":1,"delta":{"type":"input_json_delta","partial_json":"r\\n \\\"\\\"\\"}} +{"type":"content_block_delta","index":1,"delta":{"type":"input_json_delta","partial_json":"\"\\n "}} +{"type":"content_block_delta","index":1,"delta":{"type":"input_json_delta","partial_json":" if"}} +{"type":"content_block_delta","index":1,"delta":{"type":"input_json_delta","partial_json":" n <= 0"}} +{"type":"content_block_delta","index":1,"delta":{"type":"input_json_delta","partial_json":":\\n "}} +{"type":"content_block_delta","index":1,"delta":{"type":"input_json_delta","partial_json":" return \\"}} +{"type":"content_block_delta","index":1,"delta":{"type":"input_json_delta","partial_json":"\"Pleas"}} +{"type":"content_block_delta","index":1,"delta":{"type":"input_json_delta","partial_json":"e enter a "}} +{"type":"content_block_delta","index":1,"delta":{"type":"input_json_delta","partial_json":"positive int"}} +{"type":"content_block_delta","index":1,"delta":{"type":"input_json_delta","partial_json":"eg"}} +{"type":"content_block_delta","index":1,"delta":{"type":"input_json_delta","partial_json":"er"}} +{"type":"content_block_delta","index":1,"delta":{"type":"input_json_delta","partial_json":"\\\"\\n "}} +{"type":"content_block_delta","index":1,"delta":{"type":"input_json_delta","partial_json":" elif n =="}} +{"type":"content_block_delta","index":1,"delta":{"type":"input_json_delta","partial_json":" 1:\\n"}} +{"type":"content_block_delta","index":1,"delta":{"type":"input_json_delta","partial_json":" retu"}} +{"type":"content_block_delta","index":1,"delta":{"type":"input_json_delta","partial_json":"rn "}} +{"type":"content_block_delta","index":1,"delta":{"type":"input_json_delta","partial_json":"0\\n el"}} +{"type":"content_block_delta","index":1,"delta":{"type":"input_json_delta","partial_json":"if n == 2:\\n"}} +{"type":"content_block_delta","index":1,"delta":{"type":"input_json_delta","partial_json":" "}} +{"type":"content_block_delta","index":1,"delta":{"type":"input_json_delta","partial_json":" return "}} +{"type":"content_block_delta","index":1,"delta":{"type":"input_json_delta","partial_json":"1\\n "}} +{"type":"content_block_delta","index":1,"delta":{"type":"input_json_delta","partial_json":" else:\\n "}} +{"type":"content_block_delta","index":1,"delta":{"type":"input_json_delta","partial_json":" # Usi"}} +{"type":"content_block_delta","index":1,"delta":{"type":"input_json_delta","partial_json":"ng"}} +{"type":"content_block_delta","index":1,"delta":{"type":"input_json_delta","partial_json":" itera"}} +{"type":"content_block_delta","index":1,"delta":{"type":"input_json_delta","partial_json":"tive ap"}} +{"type":"content_block_delta","index":1,"delta":{"type":"input_json_delta","partial_json":"proa"}} +{"type":"content_block_delta","index":1,"delta":{"type":"input_json_delta","partial_json":"ch "}} +{"type":"content_block_delta","index":1,"delta":{"type":"input_json_delta","partial_json":"for effi"}} +{"type":"content_block_delta","index":1,"delta":{"type":"input_json_delta","partial_json":"ciency\\n"}} +{"type":"content_block_delta","index":1,"delta":{"type":"input_json_delta","partial_json":" a,"}} +{"type":"content_block_delta","index":1,"delta":{"type":"input_json_delta","partial_json":" b"}} +{"type":"content_block_delta","index":1,"delta":{"type":"input_json_delta","partial_json":" = 0"}} +{"type":"content_block_delta","index":1,"delta":{"type":"input_json_delta","partial_json":", 1"}} +{"type":"content_block_delta","index":1,"delta":{"type":"input_json_delta","partial_json":"\\n "}} +{"type":"content_block_delta","index":1,"delta":{"type":"input_json_delta","partial_json":"for _ in ra"}} +{"type":"content_block_delta","index":1,"delta":{"type":"input_json_delta","partial_json":"nge(2, n):\\"}} +{"type":"content_block_delta","index":1,"delta":{"type":"input_json_delta","partial_json":"n "}} +{"type":"content_block_delta","index":1,"delta":{"type":"input_json_delta","partial_json":" a"}} +{"type":"content_block_delta","index":1,"delta":{"type":"input_json_delta","partial_json":", b = b, "}} +{"type":"content_block_delta","index":1,"delta":{"type":"input_json_delta","partial_json":"a + b\\n "}} +{"type":"content_block_delta","index":1,"delta":{"type":"input_json_delta","partial_json":" ret"}} +{"type":"content_block_delta","index":1,"delta":{"type":"input_json_delta","partial_json":"urn "}} +{"type":"content_block_delta","index":1,"delta":{"type":"input_json_delta","partial_json":"b\\n"}} +{"type":"content_block_delta","index":1,"delta":{"type":"input_json_delta","partial_json":"\\ndef fi"}} +{"type":"content_block_delta","index":1,"delta":{"type":"input_json_delta","partial_json":"bonacci_rec"}} +{"type":"content_block_delta","index":1,"delta":{"type":"input_json_delta","partial_json":"ursive(n"}} +{"type":"content_block_delta","index":1,"delta":{"type":"input_json_delta","partial_json":"):\\n "}} +{"type":"content_block_delta","index":1,"delta":{"type":"input_json_delta","partial_json":" \\\"\\\"\\\"\\n"}} +{"type":"content_block_delta","index":1,"delta":{"type":"input_json_delta","partial_json":" Ca"}} +{"type":"content_block_delta","index":1,"delta":{"type":"input_json_delta","partial_json":"lcula"}} +{"type":"content_block_delta","index":1,"delta":{"type":"input_json_delta","partial_json":"te the nth"}} +{"type":"content_block_delta","index":1,"delta":{"type":"input_json_delta","partial_json":" Fibonacci"}} +{"type":"content_block_delta","index":1,"delta":{"type":"input_json_delta","partial_json":" numb"}} +{"type":"content_block_delta","index":1,"delta":{"type":"input_json_delta","partial_json":"er using r"}} +{"type":"content_block_delta","index":1,"delta":{"type":"input_json_delta","partial_json":"ecursion.\\n"}} +{"type":"content_block_delta","index":1,"delta":{"type":"input_json_delta","partial_json":" \\n"}} +{"type":"content_block_delta","index":1,"delta":{"type":"input_json_delta","partial_json":" Arg"}} +{"type":"content_block_delta","index":1,"delta":{"type":"input_json_delta","partial_json":"s:\\n "}} +{"type":"content_block_delta","index":1,"delta":{"type":"input_json_delta","partial_json":" "}} +{"type":"content_block_delta","index":1,"delta":{"type":"input_json_delta","partial_json":" n:"}} +{"type":"content_block_delta","index":1,"delta":{"type":"input_json_delta","partial_json":" The po"}} +{"type":"content_block_delta","index":1,"delta":{"type":"input_json_delta","partial_json":"si"}} +{"type":"content_block_delta","index":1,"delta":{"type":"input_json_delta","partial_json":"tion in "}} +{"type":"content_block_delta","index":1,"delta":{"type":"input_json_delta","partial_json":"the Fibona"}} +{"type":"content_block_delta","index":1,"delta":{"type":"input_json_delta","partial_json":"cci sequen"}} +{"type":"content_block_delta","index":1,"delta":{"type":"input_json_delta","partial_json":"ce (1-index"}} +{"type":"content_block_delta","index":1,"delta":{"type":"input_json_delta","partial_json":"ed)\\n "}} +{"type":"content_block_delta","index":1,"delta":{"type":"input_json_delta","partial_json":" \\n "}} +{"type":"content_block_delta","index":1,"delta":{"type":"input_json_delta","partial_json":" Returns:\\n"}} +{"type":"content_block_delta","index":1,"delta":{"type":"input_json_delta","partial_json":" "}} +{"type":"content_block_delta","index":1,"delta":{"type":"input_json_delta","partial_json":" The nth F"}} +{"type":"content_block_delta","index":1,"delta":{"type":"input_json_delta","partial_json":"ibonacci n"}} +{"type":"content_block_delta","index":1,"delta":{"type":"input_json_delta","partial_json":"umber\\n \\"}} +{"type":"content_block_delta","index":1,"delta":{"type":"input_json_delta","partial_json":"\"\\\"\\\"\\n "}} +{"type":"content_block_delta","index":1,"delta":{"type":"input_json_delta","partial_json":"if n <"}} +{"type":"content_block_delta","index":1,"delta":{"type":"input_json_delta","partial_json":"= 0:\\n "}} +{"type":"content_block_delta","index":1,"delta":{"type":"input_json_delta","partial_json":" retu"}} +{"type":"content_block_delta","index":1,"delta":{"type":"input_json_delta","partial_json":"rn \\\"Plea"}} +{"type":"content_block_delta","index":1,"delta":{"type":"input_json_delta","partial_json":"se enter a"}} +{"type":"content_block_delta","index":1,"delta":{"type":"input_json_delta","partial_json":" positive in"}} +{"type":"content_block_delta","index":1,"delta":{"type":"input_json_delta","partial_json":"teger\\\"\\n "}} +{"type":"content_block_delta","index":1,"delta":{"type":"input_json_delta","partial_json":" elif n "}} +{"type":"content_block_delta","index":1,"delta":{"type":"input_json_delta","partial_json":"== 1"}} +{"type":"content_block_delta","index":1,"delta":{"type":"input_json_delta","partial_json":":\\n r"}} +{"type":"content_block_delta","index":1,"delta":{"type":"input_json_delta","partial_json":"et"}} +{"type":"content_block_delta","index":1,"delta":{"type":"input_json_delta","partial_json":"ur"}} +{"type":"content_block_delta","index":1,"delta":{"type":"input_json_delta","partial_json":"n 0\\n"}} +{"type":"content_block_delta","index":1,"delta":{"type":"input_json_delta","partial_json":" elif n"}} +{"type":"content_block_delta","index":1,"delta":{"type":"input_json_delta","partial_json":" == 2:\\n"}} +{"type":"content_block_delta","index":1,"delta":{"type":"input_json_delta","partial_json":" "}} +{"type":"content_block_delta","index":1,"delta":{"type":"input_json_delta","partial_json":" return"}} +{"type":"content_block_delta","index":1,"delta":{"type":"input_json_delta","partial_json":" 1\\n els"}} +{"type":"content_block_delta","index":1,"delta":{"type":"input_json_delta","partial_json":"e:\\n "}} +{"type":"content_block_delta","index":1,"delta":{"type":"input_json_delta","partial_json":"return fib"}} +{"type":"content_block_delta","index":1,"delta":{"type":"input_json_delta","partial_json":"onacci_recur"}} +{"type":"content_block_delta","index":1,"delta":{"type":"input_json_delta","partial_json":"sive(n-1) +"}} +{"type":"content_block_delta","index":1,"delta":{"type":"input_json_delta","partial_json":" fib"}} +{"type":"content_block_delta","index":1,"delta":{"type":"input_json_delta","partial_json":"onacci_re"}} +{"type":"content_block_delta","index":1,"delta":{"type":"input_json_delta","partial_json":"cursive(n"}} +{"type":"content_block_delta","index":1,"delta":{"type":"input_json_delta","partial_json":"-2)\\n\\nif "}} +{"type":"content_block_delta","index":1,"delta":{"type":"input_json_delta","partial_json":"__name__ =="}} +{"type":"content_block_delta","index":1,"delta":{"type":"input_json_delta","partial_json":" \\\"_"}} +{"type":"content_block_delta","index":1,"delta":{"type":"input_json_delta","partial_json":"_main_"}} +{"type":"content_block_delta","index":1,"delta":{"type":"input_json_delta","partial_json":"_\\\":\\n "}} +{"type":"content_block_delta","index":1,"delta":{"type":"input_json_delta","partial_json":" # "}} +{"type":"content_block_delta","index":1,"delta":{"type":"input_json_delta","partial_json":"Calcula"}} +{"type":"content_block_delta","index":1,"delta":{"type":"input_json_delta","partial_json":"te "}} +{"type":"content_block_delta","index":1,"delta":{"type":"input_json_delta","partial_json":"the 1"}} +{"type":"content_block_delta","index":1,"delta":{"type":"input_json_delta","partial_json":"0th Fibonacc"}} +{"type":"content_block_delta","index":1,"delta":{"type":"input_json_delta","partial_json":"i numb"}} +{"type":"content_block_delta","index":1,"delta":{"type":"input_json_delta","partial_json":"er\\n n = "}} +{"type":"content_block_delta","index":1,"delta":{"type":"input_json_delta","partial_json":"10\\n "}} +{"type":"content_block_delta","index":1,"delta":{"type":"input_json_delta","partial_json":"result = fi"}} +{"type":"content_block_delta","index":1,"delta":{"type":"input_json_delta","partial_json":"bonacc"}} +{"type":"content_block_delta","index":1,"delta":{"type":"input_json_delta","partial_json":"i(n"}} +{"type":"content_block_delta","index":1,"delta":{"type":"input_json_delta","partial_json":")\\n \\n"}} +{"type":"content_block_delta","index":1,"delta":{"type":"input_json_delta","partial_json":" print("}} +{"type":"content_block_delta","index":1,"delta":{"type":"input_json_delta","partial_json":"f\\\"The"}} +{"type":"content_block_delta","index":1,"delta":{"type":"input_json_delta","partial_json":" {n}t"}} +{"type":"content_block_delta","index":1,"delta":{"type":"input_json_delta","partial_json":"h Fibonac"}} +{"type":"content_block_delta","index":1,"delta":{"type":"input_json_delta","partial_json":"ci numb"}} +{"type":"content_block_delta","index":1,"delta":{"type":"input_json_delta","partial_json":"er"}} +{"type":"content_block_delta","index":1,"delta":{"type":"input_json_delta","partial_json":" is: {resul"}} +{"type":"content_block_delta","index":1,"delta":{"type":"input_json_delta","partial_json":"t}"}} +{"type":"content_block_delta","index":1,"delta":{"type":"input_json_delta","partial_json":"\\\")\\"}} +{"type":"content_block_delta","index":1,"delta":{"type":"input_json_delta","partial_json":"n \\n "}} +{"type":"content_block_delta","index":1,"delta":{"type":"input_json_delta","partial_json":" # Sho"}} +{"type":"content_block_delta","index":1,"delta":{"type":"input_json_delta","partial_json":"w the "}} +{"type":"content_block_delta","index":1,"delta":{"type":"input_json_delta","partial_json":"first 10 "}} +{"type":"content_block_delta","index":1,"delta":{"type":"input_json_delta","partial_json":"Fibona"}} +{"type":"content_block_delta","index":1,"delta":{"type":"input_json_delta","partial_json":"cci numb"}} +{"type":"content_block_delta","index":1,"delta":{"type":"input_json_delta","partial_json":"ers\\n p"}} +{"type":"content_block_delta","index":1,"delta":{"type":"input_json_delta","partial_json":"ri"}} +{"type":"content_block_delta","index":1,"delta":{"type":"input_json_delta","partial_json":"nt(f\\"}} +{"type":"content_block_delta","index":1,"delta":{"type":"input_json_delta","partial_json":"\"\\\\nFirst "}} +{"type":"content_block_delta","index":1,"delta":{"type":"input_json_delta","partial_json":"{n}"}} +{"type":"content_block_delta","index":1,"delta":{"type":"input_json_delta","partial_json":" Fibon"}} +{"type":"content_block_delta","index":1,"delta":{"type":"input_json_delta","partial_json":"acci numbe"}} +{"type":"content_block_delta","index":1,"delta":{"type":"input_json_delta","partial_json":"rs:\\"}} +{"type":"content_block_delta","index":1,"delta":{"type":"input_json_delta","partial_json":"\")"}} +{"type":"content_block_delta","index":1,"delta":{"type":"input_json_delta","partial_json":"\\n fo"}} +{"type":"content_block_delta","index":1,"delta":{"type":"input_json_delta","partial_json":"r i in range"}} +{"type":"content_block_delta","index":1,"delta":{"type":"input_json_delta","partial_json":"(1, n + "}} +{"type":"content_block_delta","index":1,"delta":{"type":"input_json_delta","partial_json":"1):\\n "}} +{"type":"content_block_delta","index":1,"delta":{"type":"input_json_delta","partial_json":" pr"}} +{"type":"content_block_delta","index":1,"delta":{"type":"input_json_delta","partial_json":"int(f\\"}} +{"type":"content_block_delta","index":1,"delta":{"type":"input_json_delta","partial_json":"\"F({i"}} +{"type":"content_block_delta","index":1,"delta":{"type":"input_json_delta","partial_json":"}) = {"}} +{"type":"content_block_delta","index":1,"delta":{"type":"input_json_delta","partial_json":"fibonacci"}} +{"type":"content_block_delta","index":1,"delta":{"type":"input_json_delta","partial_json":"(i)}\\\")"}} +{"type":"content_block_delta","index":1,"delta":{"type":"input_json_delta","partial_json":"\\n\"}"}} +{"type":"content_block_stop","index":1} +{"type":"content_block_start","index":2,"content_block":{"type":"text_editor_code_execution_tool_result","tool_use_id":"srvtoolu_0112cP8RpnKv67t2cscmN4ia","content":{"type":"text_editor_code_execution_create_result","is_file_update":false}}} +{"type":"content_block_stop","index":2} +{"type":"content_block_start","index":3,"content_block":{"type":"text","text":""}} +{"type":"content_block_delta","index":3,"delta":{"type":"text_delta","text":"Now let's"}} +{"type":"content_block_delta","index":3,"delta":{"type":"text_delta","text":" execute the script to find the 10th Fibonacci"}} +{"type":"content_block_delta","index":3,"delta":{"type":"text_delta","text":" number:"}} +{"type":"content_block_stop","index":3} +{"type":"content_block_start","index":4,"content_block":{"type":"server_tool_use","id":"srvtoolu_01K2E2j5mkxbtLqNBc6RJHds","name":"bash_code_execution","input":{}}} +{"type":"content_block_delta","index":4,"delta":{"type":"input_json_delta","partial_json":""}} +{"type":"content_block_delta","index":4,"delta":{"type":"input_json_delta","partial_json":"{\"command"}} +{"type":"content_block_delta","index":4,"delta":{"type":"input_json_delta","partial_json":"\": \"python"}} +{"type":"content_block_delta","index":4,"delta":{"type":"input_json_delta","partial_json":" /tmp/fibon"}} +{"type":"content_block_delta","index":4,"delta":{"type":"input_json_delta","partial_json":"ac"}} +{"type":"content_block_delta","index":4,"delta":{"type":"input_json_delta","partial_json":"ci.py"}} +{"type":"content_block_delta","index":4,"delta":{"type":"input_json_delta","partial_json":"\"}"}} +{"type":"content_block_stop","index":4} +{"type":"content_block_start","index":5,"content_block":{"type":"bash_code_execution_tool_result","tool_use_id":"srvtoolu_01K2E2j5mkxbtLqNBc6RJHds","content":{"type":"bash_code_execution_result","stdout":"The 10th Fibonacci number is: 34\n\nFirst 10 Fibonacci numbers:\nF(1) = 0\nF(2) = 1\nF(3) = 1\nF(4) = 2\nF(5) = 3\nF(6) = 5\nF(7) = 8\nF(8) = 13\nF(9) = 21\nF(10) = 34\n","stderr":"","return_code":0,"content":[]}}} +{"type":"content_block_stop","index":5} +{"type":"content_block_start","index":6,"content_block":{"type":"text","text":""}} +{"type":"content_block_delta","index":6,"delta":{"type":"text_delta","text":"Perfect"}} +{"type":"content_block_delta","index":6,"delta":{"type":"text_delta","text":"! The script has been created an"}} +{"type":"content_block_delta","index":6,"delta":{"type":"text_delta","text":"d executed successfully. \n\n**Result"}} +{"type":"content_block_delta","index":6,"delta":{"type":"text_delta","text":": The 10th Fibonacci number is"}} +{"type":"content_block_delta","index":6,"delta":{"type":"text_delta","text":" 34**\n\nThe script includes:\n1"}} +{"type":"content_block_delta","index":6,"delta":{"type":"text_delta","text":". An **iterative function** (`"}} +{"type":"content_block_delta","index":6,"delta":{"type":"text_delta","text":"fibonacci`) - efficient and fast, suitable for larger numbers"}} +{"type":"content_block_delta","index":6,"delta":{"type":"text_delta","text":"\n2. A **recursive function** (`fibonacci_recursive`) - sim"}} +{"type":"content_block_delta","index":6,"delta":{"type":"text_delta","text":"pler but less efficient for large values\n3. The main execution"}} +{"type":"content_block_delta","index":6,"delta":{"type":"text_delta","text":" block that calculates and displays the 10th Fibonacci number"}} +{"type":"content_block_delta","index":6,"delta":{"type":"text_delta","text":"\n4. A bonus display showing all"}} +{"type":"content_block_delta","index":6,"delta":{"type":"text_delta","text":" Fibonacci numbers from F(1) to F(10)"}} +{"type":"content_block_delta","index":6,"delta":{"type":"text_delta","text":"\n\nThe Fibonacci sequence starts with 0, "}} +{"type":"content_block_delta","index":6,"delta":{"type":"text_delta","text":"1, and each subsequent number is the sum of the"}} +{"type":"content_block_delta","index":6,"delta":{"type":"text_delta","text":" previous two numbers: "}} +{"type":"content_block_delta","index":6,"delta":{"type":"text_delta","text":"0, 1, 1,"}} +{"type":"content_block_delta","index":6,"delta":{"type":"text_delta","text":" 2, 3, 5"}} +{"type":"content_block_delta","index":6,"delta":{"type":"text_delta","text":", 8, 13, "}} +{"type":"content_block_delta","index":6,"delta":{"type":"text_delta","text":"21, 34, ..."}} +{"type":"content_block_stop","index":6} +{"type":"message_delta","delta":{"stop_reason":"end_turn","stop_sequence":null,"container":{"id":"container_011CU6pTr2hLT47seQ5Xs4yj","expires_at":"2025-10-14T10:02:00.044495Z"}},"usage":{"input_tokens":8050,"cache_creation_input_tokens":0,"cache_read_input_tokens":0,"output_tokens":771,"server_tool_use":{"web_search_requests":0,"web_fetch_requests":0}}} +{"type":"message_stop"} \ No newline at end of file diff --git a/packages/anthropic/src/__snapshots__/anthropic-messages-language-model.test.ts.snap b/packages/anthropic/src/__snapshots__/anthropic-messages-language-model.test.ts.snap index c31b70728f83..eec7a63ada94 100644 --- a/packages/anthropic/src/__snapshots__/anthropic-messages-language-model.test.ts.snap +++ b/packages/anthropic/src/__snapshots__/anthropic-messages-language-model.test.ts.snap @@ -462,6 +462,1285 @@ For the most current breaking tech news today, I'd recommend checking major tech ] `; +exports[`AnthropicMessagesLanguageModel > doStream > raw chunks > code execution 20250825 tool > should stream code execution tool results 1`] = ` +[ + { + "type": "stream-start", + "warnings": [], + }, + { + "id": "msg_01LEsrXVCLpf7xHaFdFTZNEJ", + "modelId": "claude-sonnet-4-5-20250929", + "type": "response-metadata", + }, + { + "id": "0", + "type": "text-start", + }, + { + "delta": "I'll create a Python script to calculate", + "id": "0", + "type": "text-delta", + }, + { + "delta": " Fibonacci numbers and then execute it to fin", + "id": "0", + "type": "text-delta", + }, + { + "delta": "d the 10th Fibonacci number.", + "id": "0", + "type": "text-delta", + }, + { + "id": "0", + "type": "text-end", + }, + { + "id": "srvtoolu_0112cP8RpnKv67t2cscmN4ia", + "providerExecuted": true, + "toolName": "text_editor_code_execution", + "type": "tool-input-start", + }, + { + "delta": "", + "id": "srvtoolu_0112cP8RpnKv67t2cscmN4ia", + "type": "tool-input-delta", + }, + { + "delta": "{"", + "id": "srvtoolu_0112cP8RpnKv67t2cscmN4ia", + "type": "tool-input-delta", + }, + { + "delta": "comma", + "id": "srvtoolu_0112cP8RpnKv67t2cscmN4ia", + "type": "tool-input-delta", + }, + { + "delta": "nd": "c", + "id": "srvtoolu_0112cP8RpnKv67t2cscmN4ia", + "type": "tool-input-delta", + }, + { + "delta": "reate"", + "id": "srvtoolu_0112cP8RpnKv67t2cscmN4ia", + "type": "tool-input-delta", + }, + { + "delta": ", "", + "id": "srvtoolu_0112cP8RpnKv67t2cscmN4ia", + "type": "tool-input-delta", + }, + { + "delta": "path": ", + "id": "srvtoolu_0112cP8RpnKv67t2cscmN4ia", + "type": "tool-input-delta", + }, + { + "delta": ""/", + "id": "srvtoolu_0112cP8RpnKv67t2cscmN4ia", + "type": "tool-input-delta", + }, + { + "delta": "tmp/fi", + "id": "srvtoolu_0112cP8RpnKv67t2cscmN4ia", + "type": "tool-input-delta", + }, + { + "delta": "bonacci.", + "id": "srvtoolu_0112cP8RpnKv67t2cscmN4ia", + "type": "tool-input-delta", + }, + { + "delta": "py"", + "id": "srvtoolu_0112cP8RpnKv67t2cscmN4ia", + "type": "tool-input-delta", + }, + { + "delta": ", "file_", + "id": "srvtoolu_0112cP8RpnKv67t2cscmN4ia", + "type": "tool-input-delta", + }, + { + "delta": "text":", + "id": "srvtoolu_0112cP8RpnKv67t2cscmN4ia", + "type": "tool-input-delta", + }, + { + "delta": " "def fib", + "id": "srvtoolu_0112cP8RpnKv67t2cscmN4ia", + "type": "tool-input-delta", + }, + { + "delta": "onacci(n):", + "id": "srvtoolu_0112cP8RpnKv67t2cscmN4ia", + "type": "tool-input-delta", + }, + { + "delta": "\\n \\"\\"\\", + "id": "srvtoolu_0112cP8RpnKv67t2cscmN4ia", + "type": "tool-input-delta", + }, + { + "delta": ""\\n Calc", + "id": "srvtoolu_0112cP8RpnKv67t2cscmN4ia", + "type": "tool-input-delta", + }, + { + "delta": "ulate th", + "id": "srvtoolu_0112cP8RpnKv67t2cscmN4ia", + "type": "tool-input-delta", + }, + { + "delta": "e nth Fibo", + "id": "srvtoolu_0112cP8RpnKv67t2cscmN4ia", + "type": "tool-input-delta", + }, + { + "delta": "nacc", + "id": "srvtoolu_0112cP8RpnKv67t2cscmN4ia", + "type": "tool-input-delta", + }, + { + "delta": "i ", + "id": "srvtoolu_0112cP8RpnKv67t2cscmN4ia", + "type": "tool-input-delta", + }, + { + "delta": "numb", + "id": "srvtoolu_0112cP8RpnKv67t2cscmN4ia", + "type": "tool-input-delta", + }, + { + "delta": "er.\\n", + "id": "srvtoolu_0112cP8RpnKv67t2cscmN4ia", + "type": "tool-input-delta", + }, + { + "delta": " \\", + "id": "srvtoolu_0112cP8RpnKv67t2cscmN4ia", + "type": "tool-input-delta", + }, + { + "delta": "n Ar", + "id": "srvtoolu_0112cP8RpnKv67t2cscmN4ia", + "type": "tool-input-delta", + }, + { + "delta": "gs:", + "id": "srvtoolu_0112cP8RpnKv67t2cscmN4ia", + "type": "tool-input-delta", + }, + { + "delta": "\\n ", + "id": "srvtoolu_0112cP8RpnKv67t2cscmN4ia", + "type": "tool-input-delta", + }, + { + "delta": " n: T", + "id": "srvtoolu_0112cP8RpnKv67t2cscmN4ia", + "type": "tool-input-delta", + }, + { + "delta": "he ", + "id": "srvtoolu_0112cP8RpnKv67t2cscmN4ia", + "type": "tool-input-delta", + }, + { + "delta": "positio", + "id": "srvtoolu_0112cP8RpnKv67t2cscmN4ia", + "type": "tool-input-delta", + }, + { + "delta": "n i", + "id": "srvtoolu_0112cP8RpnKv67t2cscmN4ia", + "type": "tool-input-delta", + }, + { + "delta": "n the Fibon", + "id": "srvtoolu_0112cP8RpnKv67t2cscmN4ia", + "type": "tool-input-delta", + }, + { + "delta": "acci s", + "id": "srvtoolu_0112cP8RpnKv67t2cscmN4ia", + "type": "tool-input-delta", + }, + { + "delta": "eq", + "id": "srvtoolu_0112cP8RpnKv67t2cscmN4ia", + "type": "tool-input-delta", + }, + { + "delta": "uence (1-", + "id": "srvtoolu_0112cP8RpnKv67t2cscmN4ia", + "type": "tool-input-delta", + }, + { + "delta": "indexe", + "id": "srvtoolu_0112cP8RpnKv67t2cscmN4ia", + "type": "tool-input-delta", + }, + { + "delta": "d)\\n ", + "id": "srvtoolu_0112cP8RpnKv67t2cscmN4ia", + "type": "tool-input-delta", + }, + { + "delta": " \\", + "id": "srvtoolu_0112cP8RpnKv67t2cscmN4ia", + "type": "tool-input-delta", + }, + { + "delta": "n ", + "id": "srvtoolu_0112cP8RpnKv67t2cscmN4ia", + "type": "tool-input-delta", + }, + { + "delta": " Returns:\\", + "id": "srvtoolu_0112cP8RpnKv67t2cscmN4ia", + "type": "tool-input-delta", + }, + { + "delta": "n ", + "id": "srvtoolu_0112cP8RpnKv67t2cscmN4ia", + "type": "tool-input-delta", + }, + { + "delta": " The n", + "id": "srvtoolu_0112cP8RpnKv67t2cscmN4ia", + "type": "tool-input-delta", + }, + { + "delta": "th ", + "id": "srvtoolu_0112cP8RpnKv67t2cscmN4ia", + "type": "tool-input-delta", + }, + { + "delta": "Fibonac", + "id": "srvtoolu_0112cP8RpnKv67t2cscmN4ia", + "type": "tool-input-delta", + }, + { + "delta": "ci num", + "id": "srvtoolu_0112cP8RpnKv67t2cscmN4ia", + "type": "tool-input-delta", + }, + { + "delta": "be", + "id": "srvtoolu_0112cP8RpnKv67t2cscmN4ia", + "type": "tool-input-delta", + }, + { + "delta": "r\\n \\"\\"\\", + "id": "srvtoolu_0112cP8RpnKv67t2cscmN4ia", + "type": "tool-input-delta", + }, + { + "delta": ""\\n ", + "id": "srvtoolu_0112cP8RpnKv67t2cscmN4ia", + "type": "tool-input-delta", + }, + { + "delta": " if", + "id": "srvtoolu_0112cP8RpnKv67t2cscmN4ia", + "type": "tool-input-delta", + }, + { + "delta": " n <= 0", + "id": "srvtoolu_0112cP8RpnKv67t2cscmN4ia", + "type": "tool-input-delta", + }, + { + "delta": ":\\n ", + "id": "srvtoolu_0112cP8RpnKv67t2cscmN4ia", + "type": "tool-input-delta", + }, + { + "delta": " return \\", + "id": "srvtoolu_0112cP8RpnKv67t2cscmN4ia", + "type": "tool-input-delta", + }, + { + "delta": ""Pleas", + "id": "srvtoolu_0112cP8RpnKv67t2cscmN4ia", + "type": "tool-input-delta", + }, + { + "delta": "e enter a ", + "id": "srvtoolu_0112cP8RpnKv67t2cscmN4ia", + "type": "tool-input-delta", + }, + { + "delta": "positive int", + "id": "srvtoolu_0112cP8RpnKv67t2cscmN4ia", + "type": "tool-input-delta", + }, + { + "delta": "eg", + "id": "srvtoolu_0112cP8RpnKv67t2cscmN4ia", + "type": "tool-input-delta", + }, + { + "delta": "er", + "id": "srvtoolu_0112cP8RpnKv67t2cscmN4ia", + "type": "tool-input-delta", + }, + { + "delta": "\\"\\n ", + "id": "srvtoolu_0112cP8RpnKv67t2cscmN4ia", + "type": "tool-input-delta", + }, + { + "delta": " elif n ==", + "id": "srvtoolu_0112cP8RpnKv67t2cscmN4ia", + "type": "tool-input-delta", + }, + { + "delta": " 1:\\n", + "id": "srvtoolu_0112cP8RpnKv67t2cscmN4ia", + "type": "tool-input-delta", + }, + { + "delta": " retu", + "id": "srvtoolu_0112cP8RpnKv67t2cscmN4ia", + "type": "tool-input-delta", + }, + { + "delta": "rn ", + "id": "srvtoolu_0112cP8RpnKv67t2cscmN4ia", + "type": "tool-input-delta", + }, + { + "delta": "0\\n el", + "id": "srvtoolu_0112cP8RpnKv67t2cscmN4ia", + "type": "tool-input-delta", + }, + { + "delta": "if n == 2:\\n", + "id": "srvtoolu_0112cP8RpnKv67t2cscmN4ia", + "type": "tool-input-delta", + }, + { + "delta": " ", + "id": "srvtoolu_0112cP8RpnKv67t2cscmN4ia", + "type": "tool-input-delta", + }, + { + "delta": " return ", + "id": "srvtoolu_0112cP8RpnKv67t2cscmN4ia", + "type": "tool-input-delta", + }, + { + "delta": "1\\n ", + "id": "srvtoolu_0112cP8RpnKv67t2cscmN4ia", + "type": "tool-input-delta", + }, + { + "delta": " else:\\n ", + "id": "srvtoolu_0112cP8RpnKv67t2cscmN4ia", + "type": "tool-input-delta", + }, + { + "delta": " # Usi", + "id": "srvtoolu_0112cP8RpnKv67t2cscmN4ia", + "type": "tool-input-delta", + }, + { + "delta": "ng", + "id": "srvtoolu_0112cP8RpnKv67t2cscmN4ia", + "type": "tool-input-delta", + }, + { + "delta": " itera", + "id": "srvtoolu_0112cP8RpnKv67t2cscmN4ia", + "type": "tool-input-delta", + }, + { + "delta": "tive ap", + "id": "srvtoolu_0112cP8RpnKv67t2cscmN4ia", + "type": "tool-input-delta", + }, + { + "delta": "proa", + "id": "srvtoolu_0112cP8RpnKv67t2cscmN4ia", + "type": "tool-input-delta", + }, + { + "delta": "ch ", + "id": "srvtoolu_0112cP8RpnKv67t2cscmN4ia", + "type": "tool-input-delta", + }, + { + "delta": "for effi", + "id": "srvtoolu_0112cP8RpnKv67t2cscmN4ia", + "type": "tool-input-delta", + }, + { + "delta": "ciency\\n", + "id": "srvtoolu_0112cP8RpnKv67t2cscmN4ia", + "type": "tool-input-delta", + }, + { + "delta": " a,", + "id": "srvtoolu_0112cP8RpnKv67t2cscmN4ia", + "type": "tool-input-delta", + }, + { + "delta": " b", + "id": "srvtoolu_0112cP8RpnKv67t2cscmN4ia", + "type": "tool-input-delta", + }, + { + "delta": " = 0", + "id": "srvtoolu_0112cP8RpnKv67t2cscmN4ia", + "type": "tool-input-delta", + }, + { + "delta": ", 1", + "id": "srvtoolu_0112cP8RpnKv67t2cscmN4ia", + "type": "tool-input-delta", + }, + { + "delta": "\\n ", + "id": "srvtoolu_0112cP8RpnKv67t2cscmN4ia", + "type": "tool-input-delta", + }, + { + "delta": "for _ in ra", + "id": "srvtoolu_0112cP8RpnKv67t2cscmN4ia", + "type": "tool-input-delta", + }, + { + "delta": "nge(2, n):\\", + "id": "srvtoolu_0112cP8RpnKv67t2cscmN4ia", + "type": "tool-input-delta", + }, + { + "delta": "n ", + "id": "srvtoolu_0112cP8RpnKv67t2cscmN4ia", + "type": "tool-input-delta", + }, + { + "delta": " a", + "id": "srvtoolu_0112cP8RpnKv67t2cscmN4ia", + "type": "tool-input-delta", + }, + { + "delta": ", b = b, ", + "id": "srvtoolu_0112cP8RpnKv67t2cscmN4ia", + "type": "tool-input-delta", + }, + { + "delta": "a + b\\n ", + "id": "srvtoolu_0112cP8RpnKv67t2cscmN4ia", + "type": "tool-input-delta", + }, + { + "delta": " ret", + "id": "srvtoolu_0112cP8RpnKv67t2cscmN4ia", + "type": "tool-input-delta", + }, + { + "delta": "urn ", + "id": "srvtoolu_0112cP8RpnKv67t2cscmN4ia", + "type": "tool-input-delta", + }, + { + "delta": "b\\n", + "id": "srvtoolu_0112cP8RpnKv67t2cscmN4ia", + "type": "tool-input-delta", + }, + { + "delta": "\\ndef fi", + "id": "srvtoolu_0112cP8RpnKv67t2cscmN4ia", + "type": "tool-input-delta", + }, + { + "delta": "bonacci_rec", + "id": "srvtoolu_0112cP8RpnKv67t2cscmN4ia", + "type": "tool-input-delta", + }, + { + "delta": "ursive(n", + "id": "srvtoolu_0112cP8RpnKv67t2cscmN4ia", + "type": "tool-input-delta", + }, + { + "delta": "):\\n ", + "id": "srvtoolu_0112cP8RpnKv67t2cscmN4ia", + "type": "tool-input-delta", + }, + { + "delta": " \\"\\"\\"\\n", + "id": "srvtoolu_0112cP8RpnKv67t2cscmN4ia", + "type": "tool-input-delta", + }, + { + "delta": " Ca", + "id": "srvtoolu_0112cP8RpnKv67t2cscmN4ia", + "type": "tool-input-delta", + }, + { + "delta": "lcula", + "id": "srvtoolu_0112cP8RpnKv67t2cscmN4ia", + "type": "tool-input-delta", + }, + { + "delta": "te the nth", + "id": "srvtoolu_0112cP8RpnKv67t2cscmN4ia", + "type": "tool-input-delta", + }, + { + "delta": " Fibonacci", + "id": "srvtoolu_0112cP8RpnKv67t2cscmN4ia", + "type": "tool-input-delta", + }, + { + "delta": " numb", + "id": "srvtoolu_0112cP8RpnKv67t2cscmN4ia", + "type": "tool-input-delta", + }, + { + "delta": "er using r", + "id": "srvtoolu_0112cP8RpnKv67t2cscmN4ia", + "type": "tool-input-delta", + }, + { + "delta": "ecursion.\\n", + "id": "srvtoolu_0112cP8RpnKv67t2cscmN4ia", + "type": "tool-input-delta", + }, + { + "delta": " \\n", + "id": "srvtoolu_0112cP8RpnKv67t2cscmN4ia", + "type": "tool-input-delta", + }, + { + "delta": " Arg", + "id": "srvtoolu_0112cP8RpnKv67t2cscmN4ia", + "type": "tool-input-delta", + }, + { + "delta": "s:\\n ", + "id": "srvtoolu_0112cP8RpnKv67t2cscmN4ia", + "type": "tool-input-delta", + }, + { + "delta": " ", + "id": "srvtoolu_0112cP8RpnKv67t2cscmN4ia", + "type": "tool-input-delta", + }, + { + "delta": " n:", + "id": "srvtoolu_0112cP8RpnKv67t2cscmN4ia", + "type": "tool-input-delta", + }, + { + "delta": " The po", + "id": "srvtoolu_0112cP8RpnKv67t2cscmN4ia", + "type": "tool-input-delta", + }, + { + "delta": "si", + "id": "srvtoolu_0112cP8RpnKv67t2cscmN4ia", + "type": "tool-input-delta", + }, + { + "delta": "tion in ", + "id": "srvtoolu_0112cP8RpnKv67t2cscmN4ia", + "type": "tool-input-delta", + }, + { + "delta": "the Fibona", + "id": "srvtoolu_0112cP8RpnKv67t2cscmN4ia", + "type": "tool-input-delta", + }, + { + "delta": "cci sequen", + "id": "srvtoolu_0112cP8RpnKv67t2cscmN4ia", + "type": "tool-input-delta", + }, + { + "delta": "ce (1-index", + "id": "srvtoolu_0112cP8RpnKv67t2cscmN4ia", + "type": "tool-input-delta", + }, + { + "delta": "ed)\\n ", + "id": "srvtoolu_0112cP8RpnKv67t2cscmN4ia", + "type": "tool-input-delta", + }, + { + "delta": " \\n ", + "id": "srvtoolu_0112cP8RpnKv67t2cscmN4ia", + "type": "tool-input-delta", + }, + { + "delta": " Returns:\\n", + "id": "srvtoolu_0112cP8RpnKv67t2cscmN4ia", + "type": "tool-input-delta", + }, + { + "delta": " ", + "id": "srvtoolu_0112cP8RpnKv67t2cscmN4ia", + "type": "tool-input-delta", + }, + { + "delta": " The nth F", + "id": "srvtoolu_0112cP8RpnKv67t2cscmN4ia", + "type": "tool-input-delta", + }, + { + "delta": "ibonacci n", + "id": "srvtoolu_0112cP8RpnKv67t2cscmN4ia", + "type": "tool-input-delta", + }, + { + "delta": "umber\\n \\", + "id": "srvtoolu_0112cP8RpnKv67t2cscmN4ia", + "type": "tool-input-delta", + }, + { + "delta": ""\\"\\"\\n ", + "id": "srvtoolu_0112cP8RpnKv67t2cscmN4ia", + "type": "tool-input-delta", + }, + { + "delta": "if n <", + "id": "srvtoolu_0112cP8RpnKv67t2cscmN4ia", + "type": "tool-input-delta", + }, + { + "delta": "= 0:\\n ", + "id": "srvtoolu_0112cP8RpnKv67t2cscmN4ia", + "type": "tool-input-delta", + }, + { + "delta": " retu", + "id": "srvtoolu_0112cP8RpnKv67t2cscmN4ia", + "type": "tool-input-delta", + }, + { + "delta": "rn \\"Plea", + "id": "srvtoolu_0112cP8RpnKv67t2cscmN4ia", + "type": "tool-input-delta", + }, + { + "delta": "se enter a", + "id": "srvtoolu_0112cP8RpnKv67t2cscmN4ia", + "type": "tool-input-delta", + }, + { + "delta": " positive in", + "id": "srvtoolu_0112cP8RpnKv67t2cscmN4ia", + "type": "tool-input-delta", + }, + { + "delta": "teger\\"\\n ", + "id": "srvtoolu_0112cP8RpnKv67t2cscmN4ia", + "type": "tool-input-delta", + }, + { + "delta": " elif n ", + "id": "srvtoolu_0112cP8RpnKv67t2cscmN4ia", + "type": "tool-input-delta", + }, + { + "delta": "== 1", + "id": "srvtoolu_0112cP8RpnKv67t2cscmN4ia", + "type": "tool-input-delta", + }, + { + "delta": ":\\n r", + "id": "srvtoolu_0112cP8RpnKv67t2cscmN4ia", + "type": "tool-input-delta", + }, + { + "delta": "et", + "id": "srvtoolu_0112cP8RpnKv67t2cscmN4ia", + "type": "tool-input-delta", + }, + { + "delta": "ur", + "id": "srvtoolu_0112cP8RpnKv67t2cscmN4ia", + "type": "tool-input-delta", + }, + { + "delta": "n 0\\n", + "id": "srvtoolu_0112cP8RpnKv67t2cscmN4ia", + "type": "tool-input-delta", + }, + { + "delta": " elif n", + "id": "srvtoolu_0112cP8RpnKv67t2cscmN4ia", + "type": "tool-input-delta", + }, + { + "delta": " == 2:\\n", + "id": "srvtoolu_0112cP8RpnKv67t2cscmN4ia", + "type": "tool-input-delta", + }, + { + "delta": " ", + "id": "srvtoolu_0112cP8RpnKv67t2cscmN4ia", + "type": "tool-input-delta", + }, + { + "delta": " return", + "id": "srvtoolu_0112cP8RpnKv67t2cscmN4ia", + "type": "tool-input-delta", + }, + { + "delta": " 1\\n els", + "id": "srvtoolu_0112cP8RpnKv67t2cscmN4ia", + "type": "tool-input-delta", + }, + { + "delta": "e:\\n ", + "id": "srvtoolu_0112cP8RpnKv67t2cscmN4ia", + "type": "tool-input-delta", + }, + { + "delta": "return fib", + "id": "srvtoolu_0112cP8RpnKv67t2cscmN4ia", + "type": "tool-input-delta", + }, + { + "delta": "onacci_recur", + "id": "srvtoolu_0112cP8RpnKv67t2cscmN4ia", + "type": "tool-input-delta", + }, + { + "delta": "sive(n-1) +", + "id": "srvtoolu_0112cP8RpnKv67t2cscmN4ia", + "type": "tool-input-delta", + }, + { + "delta": " fib", + "id": "srvtoolu_0112cP8RpnKv67t2cscmN4ia", + "type": "tool-input-delta", + }, + { + "delta": "onacci_re", + "id": "srvtoolu_0112cP8RpnKv67t2cscmN4ia", + "type": "tool-input-delta", + }, + { + "delta": "cursive(n", + "id": "srvtoolu_0112cP8RpnKv67t2cscmN4ia", + "type": "tool-input-delta", + }, + { + "delta": "-2)\\n\\nif ", + "id": "srvtoolu_0112cP8RpnKv67t2cscmN4ia", + "type": "tool-input-delta", + }, + { + "delta": "__name__ ==", + "id": "srvtoolu_0112cP8RpnKv67t2cscmN4ia", + "type": "tool-input-delta", + }, + { + "delta": " \\"_", + "id": "srvtoolu_0112cP8RpnKv67t2cscmN4ia", + "type": "tool-input-delta", + }, + { + "delta": "_main_", + "id": "srvtoolu_0112cP8RpnKv67t2cscmN4ia", + "type": "tool-input-delta", + }, + { + "delta": "_\\":\\n ", + "id": "srvtoolu_0112cP8RpnKv67t2cscmN4ia", + "type": "tool-input-delta", + }, + { + "delta": " # ", + "id": "srvtoolu_0112cP8RpnKv67t2cscmN4ia", + "type": "tool-input-delta", + }, + { + "delta": "Calcula", + "id": "srvtoolu_0112cP8RpnKv67t2cscmN4ia", + "type": "tool-input-delta", + }, + { + "delta": "te ", + "id": "srvtoolu_0112cP8RpnKv67t2cscmN4ia", + "type": "tool-input-delta", + }, + { + "delta": "the 1", + "id": "srvtoolu_0112cP8RpnKv67t2cscmN4ia", + "type": "tool-input-delta", + }, + { + "delta": "0th Fibonacc", + "id": "srvtoolu_0112cP8RpnKv67t2cscmN4ia", + "type": "tool-input-delta", + }, + { + "delta": "i numb", + "id": "srvtoolu_0112cP8RpnKv67t2cscmN4ia", + "type": "tool-input-delta", + }, + { + "delta": "er\\n n = ", + "id": "srvtoolu_0112cP8RpnKv67t2cscmN4ia", + "type": "tool-input-delta", + }, + { + "delta": "10\\n ", + "id": "srvtoolu_0112cP8RpnKv67t2cscmN4ia", + "type": "tool-input-delta", + }, + { + "delta": "result = fi", + "id": "srvtoolu_0112cP8RpnKv67t2cscmN4ia", + "type": "tool-input-delta", + }, + { + "delta": "bonacc", + "id": "srvtoolu_0112cP8RpnKv67t2cscmN4ia", + "type": "tool-input-delta", + }, + { + "delta": "i(n", + "id": "srvtoolu_0112cP8RpnKv67t2cscmN4ia", + "type": "tool-input-delta", + }, + { + "delta": ")\\n \\n", + "id": "srvtoolu_0112cP8RpnKv67t2cscmN4ia", + "type": "tool-input-delta", + }, + { + "delta": " print(", + "id": "srvtoolu_0112cP8RpnKv67t2cscmN4ia", + "type": "tool-input-delta", + }, + { + "delta": "f\\"The", + "id": "srvtoolu_0112cP8RpnKv67t2cscmN4ia", + "type": "tool-input-delta", + }, + { + "delta": " {n}t", + "id": "srvtoolu_0112cP8RpnKv67t2cscmN4ia", + "type": "tool-input-delta", + }, + { + "delta": "h Fibonac", + "id": "srvtoolu_0112cP8RpnKv67t2cscmN4ia", + "type": "tool-input-delta", + }, + { + "delta": "ci numb", + "id": "srvtoolu_0112cP8RpnKv67t2cscmN4ia", + "type": "tool-input-delta", + }, + { + "delta": "er", + "id": "srvtoolu_0112cP8RpnKv67t2cscmN4ia", + "type": "tool-input-delta", + }, + { + "delta": " is: {resul", + "id": "srvtoolu_0112cP8RpnKv67t2cscmN4ia", + "type": "tool-input-delta", + }, + { + "delta": "t}", + "id": "srvtoolu_0112cP8RpnKv67t2cscmN4ia", + "type": "tool-input-delta", + }, + { + "delta": "\\")\\", + "id": "srvtoolu_0112cP8RpnKv67t2cscmN4ia", + "type": "tool-input-delta", + }, + { + "delta": "n \\n ", + "id": "srvtoolu_0112cP8RpnKv67t2cscmN4ia", + "type": "tool-input-delta", + }, + { + "delta": " # Sho", + "id": "srvtoolu_0112cP8RpnKv67t2cscmN4ia", + "type": "tool-input-delta", + }, + { + "delta": "w the ", + "id": "srvtoolu_0112cP8RpnKv67t2cscmN4ia", + "type": "tool-input-delta", + }, + { + "delta": "first 10 ", + "id": "srvtoolu_0112cP8RpnKv67t2cscmN4ia", + "type": "tool-input-delta", + }, + { + "delta": "Fibona", + "id": "srvtoolu_0112cP8RpnKv67t2cscmN4ia", + "type": "tool-input-delta", + }, + { + "delta": "cci numb", + "id": "srvtoolu_0112cP8RpnKv67t2cscmN4ia", + "type": "tool-input-delta", + }, + { + "delta": "ers\\n p", + "id": "srvtoolu_0112cP8RpnKv67t2cscmN4ia", + "type": "tool-input-delta", + }, + { + "delta": "ri", + "id": "srvtoolu_0112cP8RpnKv67t2cscmN4ia", + "type": "tool-input-delta", + }, + { + "delta": "nt(f\\", + "id": "srvtoolu_0112cP8RpnKv67t2cscmN4ia", + "type": "tool-input-delta", + }, + { + "delta": ""\\\\nFirst ", + "id": "srvtoolu_0112cP8RpnKv67t2cscmN4ia", + "type": "tool-input-delta", + }, + { + "delta": "{n}", + "id": "srvtoolu_0112cP8RpnKv67t2cscmN4ia", + "type": "tool-input-delta", + }, + { + "delta": " Fibon", + "id": "srvtoolu_0112cP8RpnKv67t2cscmN4ia", + "type": "tool-input-delta", + }, + { + "delta": "acci numbe", + "id": "srvtoolu_0112cP8RpnKv67t2cscmN4ia", + "type": "tool-input-delta", + }, + { + "delta": "rs:\\", + "id": "srvtoolu_0112cP8RpnKv67t2cscmN4ia", + "type": "tool-input-delta", + }, + { + "delta": "")", + "id": "srvtoolu_0112cP8RpnKv67t2cscmN4ia", + "type": "tool-input-delta", + }, + { + "delta": "\\n fo", + "id": "srvtoolu_0112cP8RpnKv67t2cscmN4ia", + "type": "tool-input-delta", + }, + { + "delta": "r i in range", + "id": "srvtoolu_0112cP8RpnKv67t2cscmN4ia", + "type": "tool-input-delta", + }, + { + "delta": "(1, n + ", + "id": "srvtoolu_0112cP8RpnKv67t2cscmN4ia", + "type": "tool-input-delta", + }, + { + "delta": "1):\\n ", + "id": "srvtoolu_0112cP8RpnKv67t2cscmN4ia", + "type": "tool-input-delta", + }, + { + "delta": " pr", + "id": "srvtoolu_0112cP8RpnKv67t2cscmN4ia", + "type": "tool-input-delta", + }, + { + "delta": "int(f\\", + "id": "srvtoolu_0112cP8RpnKv67t2cscmN4ia", + "type": "tool-input-delta", + }, + { + "delta": ""F({i", + "id": "srvtoolu_0112cP8RpnKv67t2cscmN4ia", + "type": "tool-input-delta", + }, + { + "delta": "}) = {", + "id": "srvtoolu_0112cP8RpnKv67t2cscmN4ia", + "type": "tool-input-delta", + }, + { + "delta": "fibonacci", + "id": "srvtoolu_0112cP8RpnKv67t2cscmN4ia", + "type": "tool-input-delta", + }, + { + "delta": "(i)}\\")", + "id": "srvtoolu_0112cP8RpnKv67t2cscmN4ia", + "type": "tool-input-delta", + }, + { + "delta": "\\n"}", + "id": "srvtoolu_0112cP8RpnKv67t2cscmN4ia", + "type": "tool-input-delta", + }, + { + "id": "srvtoolu_0112cP8RpnKv67t2cscmN4ia", + "type": "tool-input-end", + }, + { + "input": "{"command": "create", "path": "/tmp/fibonacci.py", "file_text": "def fibonacci(n):\\n \\"\\"\\"\\n Calculate the nth Fibonacci number.\\n \\n Args:\\n n: The position in the Fibonacci sequence (1-indexed)\\n \\n Returns:\\n The nth Fibonacci number\\n \\"\\"\\"\\n if n <= 0:\\n return \\"Please enter a positive integer\\"\\n elif n == 1:\\n return 0\\n elif n == 2:\\n return 1\\n else:\\n # Using iterative approach for efficiency\\n a, b = 0, 1\\n for _ in range(2, n):\\n a, b = b, a + b\\n return b\\n\\ndef fibonacci_recursive(n):\\n \\"\\"\\"\\n Calculate the nth Fibonacci number using recursion.\\n \\n Args:\\n n: The position in the Fibonacci sequence (1-indexed)\\n \\n Returns:\\n The nth Fibonacci number\\n \\"\\"\\"\\n if n <= 0:\\n return \\"Please enter a positive integer\\"\\n elif n == 1:\\n return 0\\n elif n == 2:\\n return 1\\n else:\\n return fibonacci_recursive(n-1) + fibonacci_recursive(n-2)\\n\\nif __name__ == \\"__main__\\":\\n # Calculate the 10th Fibonacci number\\n n = 10\\n result = fibonacci(n)\\n \\n print(f\\"The {n}th Fibonacci number is: {result}\\")\\n \\n # Show the first 10 Fibonacci numbers\\n print(f\\"\\\\nFirst {n} Fibonacci numbers:\\")\\n for i in range(1, n + 1):\\n print(f\\"F({i}) = {fibonacci(i)}\\")\\n"}", + "providerExecuted": true, + "toolCallId": "srvtoolu_0112cP8RpnKv67t2cscmN4ia", + "toolName": "text_editor_code_execution", + "type": "tool-call", + }, + { + "id": "3", + "type": "text-start", + }, + { + "delta": "Now let's", + "id": "3", + "type": "text-delta", + }, + { + "delta": " execute the script to find the 10th Fibonacci", + "id": "3", + "type": "text-delta", + }, + { + "delta": " number:", + "id": "3", + "type": "text-delta", + }, + { + "id": "3", + "type": "text-end", + }, + { + "id": "srvtoolu_01K2E2j5mkxbtLqNBc6RJHds", + "providerExecuted": true, + "toolName": "bash_code_execution", + "type": "tool-input-start", + }, + { + "delta": "", + "id": "srvtoolu_01K2E2j5mkxbtLqNBc6RJHds", + "type": "tool-input-delta", + }, + { + "delta": "{"command", + "id": "srvtoolu_01K2E2j5mkxbtLqNBc6RJHds", + "type": "tool-input-delta", + }, + { + "delta": "": "python", + "id": "srvtoolu_01K2E2j5mkxbtLqNBc6RJHds", + "type": "tool-input-delta", + }, + { + "delta": " /tmp/fibon", + "id": "srvtoolu_01K2E2j5mkxbtLqNBc6RJHds", + "type": "tool-input-delta", + }, + { + "delta": "ac", + "id": "srvtoolu_01K2E2j5mkxbtLqNBc6RJHds", + "type": "tool-input-delta", + }, + { + "delta": "ci.py", + "id": "srvtoolu_01K2E2j5mkxbtLqNBc6RJHds", + "type": "tool-input-delta", + }, + { + "delta": ""}", + "id": "srvtoolu_01K2E2j5mkxbtLqNBc6RJHds", + "type": "tool-input-delta", + }, + { + "id": "srvtoolu_01K2E2j5mkxbtLqNBc6RJHds", + "type": "tool-input-end", + }, + { + "input": "{"command": "python /tmp/fibonacci.py"}", + "providerExecuted": true, + "toolCallId": "srvtoolu_01K2E2j5mkxbtLqNBc6RJHds", + "toolName": "bash_code_execution", + "type": "tool-call", + }, + { + "error": [AI_TypeValidationError: Type validation failed: Value: {"type":"content_block_start","index":5,"content_block":{"type":"bash_code_execution_tool_result","tool_use_id":"srvtoolu_01K2E2j5mkxbtLqNBc6RJHds","content":{"type":"bash_code_execution_result","stdout":"The 10th Fibonacci number is: 34\\n\\nFirst 10 Fibonacci numbers:\\nF(1) = 0\\nF(2) = 1\\nF(3) = 1\\nF(4) = 2\\nF(5) = 3\\nF(6) = 5\\nF(7) = 8\\nF(8) = 13\\nF(9) = 21\\nF(10) = 34\\n","stderr":"","return_code":0,"content":[]}}}. +Error message: [ + { + "expected": "object", + "code": "invalid_type", + "path": [ + "content_block", + "content", + "content" + ], + "message": "Invalid input: expected object, received array" + } +]], + "type": "error", + }, + { + "id": "6", + "type": "text-start", + }, + { + "delta": "Perfect", + "id": "6", + "type": "text-delta", + }, + { + "delta": "! The script has been created an", + "id": "6", + "type": "text-delta", + }, + { + "delta": "d executed successfully. + +**Result", + "id": "6", + "type": "text-delta", + }, + { + "delta": ": The 10th Fibonacci number is", + "id": "6", + "type": "text-delta", + }, + { + "delta": " 34** + +The script includes: +1", + "id": "6", + "type": "text-delta", + }, + { + "delta": ". An **iterative function** (\`", + "id": "6", + "type": "text-delta", + }, + { + "delta": "fibonacci\`) - efficient and fast, suitable for larger numbers", + "id": "6", + "type": "text-delta", + }, + { + "delta": " +2. A **recursive function** (\`fibonacci_recursive\`) - sim", + "id": "6", + "type": "text-delta", + }, + { + "delta": "pler but less efficient for large values +3. The main execution", + "id": "6", + "type": "text-delta", + }, + { + "delta": " block that calculates and displays the 10th Fibonacci number", + "id": "6", + "type": "text-delta", + }, + { + "delta": " +4. A bonus display showing all", + "id": "6", + "type": "text-delta", + }, + { + "delta": " Fibonacci numbers from F(1) to F(10)", + "id": "6", + "type": "text-delta", + }, + { + "delta": " + +The Fibonacci sequence starts with 0, ", + "id": "6", + "type": "text-delta", + }, + { + "delta": "1, and each subsequent number is the sum of the", + "id": "6", + "type": "text-delta", + }, + { + "delta": " previous two numbers: ", + "id": "6", + "type": "text-delta", + }, + { + "delta": "0, 1, 1,", + "id": "6", + "type": "text-delta", + }, + { + "delta": " 2, 3, 5", + "id": "6", + "type": "text-delta", + }, + { + "delta": ", 8, 13, ", + "id": "6", + "type": "text-delta", + }, + { + "delta": "21, 34, ...", + "id": "6", + "type": "text-delta", + }, + { + "id": "6", + "type": "text-end", + }, + { + "finishReason": "stop", + "providerMetadata": { + "anthropic": { + "cacheCreationInputTokens": 0, + "stopSequence": null, + "usage": { + "cache_creation": { + "ephemeral_1h_input_tokens": 0, + "ephemeral_5m_input_tokens": 0, + }, + "cache_creation_input_tokens": 0, + "cache_read_input_tokens": 0, + "input_tokens": 8050, + "output_tokens": 771, + "server_tool_use": { + "web_fetch_requests": 0, + "web_search_requests": 0, + }, + "service_tier": "standard", + }, + }, + }, + "type": "finish", + "usage": { + "cachedInputTokens": 0, + "inputTokens": 2263, + "outputTokens": 771, + "totalTokens": 3034, + }, + }, +] +`; + exports[`AnthropicMessagesLanguageModel > doStream > raw chunks > web fetch tool > txt response > should stream web search tool results 1`] = ` [ { diff --git a/packages/anthropic/src/anthropic-messages-language-model.test.ts b/packages/anthropic/src/anthropic-messages-language-model.test.ts index 72b75db62e23..943bb85a605d 100644 --- a/packages/anthropic/src/anthropic-messages-language-model.test.ts +++ b/packages/anthropic/src/anthropic-messages-language-model.test.ts @@ -3201,6 +3201,27 @@ describe('AnthropicMessagesLanguageModel', () => { `); }); + describe('code execution 20250825 tool', () => { + it('should stream code execution tool results', async () => { + prepareChunksFixtureResponse('anthropic-code-execution-20250825.1'); + + const result = await model.doStream({ + prompt: TEST_PROMPT, + tools: [ + { + type: 'provider-defined', + id: 'anthropic.code_execution_20250825', + name: 'code_execution', + args: {}, + }, + ], + }); + expect( + await convertReadableStreamToArray(result.stream), + ).toMatchSnapshot(); + }); + }); + describe('web fetch tool', () => { describe('txt response', () => { let result: Awaited>; From 61eb30466759daa6060b8096109e5b7ab4b1220a Mon Sep 17 00:00:00 2001 From: Lars Grammel Date: Tue, 14 Oct 2025 11:17:20 +0200 Subject: [PATCH 15/36] stream --- ...ropic-messages-language-model.test.ts.snap | 48 +++-- .../anthropic/src/anthropic-messages-api.ts | 10 +- .../src/anthropic-messages-language-model.ts | 204 +++++++++++++----- .../src/tool/code-execution_20250825.ts | 12 +- 4 files changed, 199 insertions(+), 75 deletions(-) diff --git a/packages/anthropic/src/__snapshots__/anthropic-messages-language-model.test.ts.snap b/packages/anthropic/src/__snapshots__/anthropic-messages-language-model.test.ts.snap index eec7a63ada94..f77847902955 100644 --- a/packages/anthropic/src/__snapshots__/anthropic-messages-language-model.test.ts.snap +++ b/packages/anthropic/src/__snapshots__/anthropic-messages-language-model.test.ts.snap @@ -1503,6 +1503,16 @@ exports[`AnthropicMessagesLanguageModel > doStream > raw chunks > code execution "toolName": "text_editor_code_execution", "type": "tool-call", }, + { + "providerExecuted": true, + "result": { + "isFileUpdate": false, + "type": "text_editor_code_execution_create_result", + }, + "toolCallId": "srvtoolu_0112cP8RpnKv67t2cscmN4ia", + "toolName": "text_editor_code_execution", + "type": "tool-result", + }, { "id": "3", "type": "text-start", @@ -1579,20 +1589,30 @@ exports[`AnthropicMessagesLanguageModel > doStream > raw chunks > code execution "type": "tool-call", }, { - "error": [AI_TypeValidationError: Type validation failed: Value: {"type":"content_block_start","index":5,"content_block":{"type":"bash_code_execution_tool_result","tool_use_id":"srvtoolu_01K2E2j5mkxbtLqNBc6RJHds","content":{"type":"bash_code_execution_result","stdout":"The 10th Fibonacci number is: 34\\n\\nFirst 10 Fibonacci numbers:\\nF(1) = 0\\nF(2) = 1\\nF(3) = 1\\nF(4) = 2\\nF(5) = 3\\nF(6) = 5\\nF(7) = 8\\nF(8) = 13\\nF(9) = 21\\nF(10) = 34\\n","stderr":"","return_code":0,"content":[]}}}. -Error message: [ - { - "expected": "object", - "code": "invalid_type", - "path": [ - "content_block", - "content", - "content" - ], - "message": "Invalid input: expected object, received array" - } -]], - "type": "error", + "providerExecuted": true, + "result": { + "content": [], + "returnCode": 0, + "stderr": "", + "stdout": "The 10th Fibonacci number is: 34 + +First 10 Fibonacci numbers: +F(1) = 0 +F(2) = 1 +F(3) = 1 +F(4) = 2 +F(5) = 3 +F(6) = 5 +F(7) = 8 +F(8) = 13 +F(9) = 21 +F(10) = 34 +", + "type": "bash_code_execution_tool_result", + }, + "toolCallId": "srvtoolu_01K2E2j5mkxbtLqNBc6RJHds", + "toolName": "bash_code_execution", + "type": "tool-result", }, { "id": "6", diff --git a/packages/anthropic/src/anthropic-messages-api.ts b/packages/anthropic/src/anthropic-messages-api.ts index aaad59d7c4b8..f9849629c5a6 100644 --- a/packages/anthropic/src/anthropic-messages-api.ts +++ b/packages/anthropic/src/anthropic-messages-api.ts @@ -585,10 +585,12 @@ export const anthropicMessagesChunkSchema = lazySchema(() => content: z.discriminatedUnion('type', [ z.object({ type: z.literal('bash_code_execution_result'), - content: z.object({ - type: z.literal('bash_code_execution_output'), - file_id: z.string(), - }), + content: z.array( + z.object({ + type: z.literal('bash_code_execution_output'), + file_id: z.string(), + }), + ), stdout: z.string(), stderr: z.string(), return_code: z.number(), diff --git a/packages/anthropic/src/anthropic-messages-language-model.ts b/packages/anthropic/src/anthropic-messages-language-model.ts index c0772f1b0c4e..701935cd83ab 100644 --- a/packages/anthropic/src/anthropic-messages-language-model.ts +++ b/packages/anthropic/src/anthropic-messages-language-model.ts @@ -39,6 +39,7 @@ import { import { prepareTools } from './anthropic-prepare-tools'; import { convertToAnthropicMessagesPrompt } from './convert-to-anthropic-messages-prompt'; import { mapAnthropicStopReason } from './map-anthropic-stop-reason'; +import { codeExecution_20250825OutputSchema } from './tool/code-execution_20250825'; function createCitationSource( citation: Citation, @@ -591,34 +592,34 @@ export class AnthropicMessagesLanguageModel implements LanguageModelV3 { } break; } - case 'text_editor_code_execution_tool_result': { - content.push({ - type: 'tool-result', - toolCallId: part.tool_use_id, - toolName: 'text_editor_code_execution', - result: { - type: part.content.type, - is_file_update: part.content.is_file_update, - }, - providerExecuted: true, - }); - break; - } - case 'bash_code_execution_tool_result': { - content.push({ - type: 'tool-result', - toolCallId: part.tool_use_id, - toolName: 'bash_code_execution', - result: { - type: part.content.type, - stdout: part.content.stdout, - stderr: part.content.stderr, - return_code: part.content.return_code, - }, - providerExecuted: true, - }); - break; - } + // case 'text_editor_code_execution_tool_result': { + // content.push({ + // type: 'tool-result', + // toolCallId: part.tool_use_id, + // toolName: 'text_editor_code_execution', + // result: { + // type: part.content.type, + // isFileUpdate: part.content.is_file_update, + // }, + // providerExecuted: true, + // }); + // break; + // } + // case 'bash_code_execution_tool_result': { + // content.push({ + // type: 'tool-result', + // toolCallId: part.tool_use_id, + // toolName: 'bash_code_execution', + // result: { + // type: part.content.type, + // stdout: part.content.stdout, + // stderr: part.content.stderr, + // return_code: part.content.return_code, + // }, + // providerExecuted: true, + // }); + // break; + // } } } @@ -956,34 +957,133 @@ export class AnthropicMessagesLanguageModel implements LanguageModelV3 { case 'text_editor_code_execution_tool_result': { const part = value.content_block; - controller.enqueue({ - type: 'tool-result', - toolCallId: part.tool_use_id, - toolName: 'text_editor_code_execution', - result: { - type: part.content.type, - is_file_update: part.content.is_file_update, - }, - providerExecuted: true, - }); - return; + const content = part.content; + + switch (content.type) { + case 'text_editor_code_execution_tool_result_error': { + controller.enqueue({ + type: 'tool-result', + toolCallId: part.tool_use_id, + toolName: 'text_editor_code_execution', + result: { + type: 'text_editor_code_execution_tool_result_error', + errorCode: content.error_code, + } satisfies InferSchema< + typeof codeExecution_20250825OutputSchema + >, + providerExecuted: true, + }); + return; + } + + case 'text_editor_code_execution_view_result': { + controller.enqueue({ + type: 'tool-result', + toolCallId: part.tool_use_id, + toolName: 'text_editor_code_execution', + result: { + type: 'text_editor_code_execution_view_result', + content: content.content, + fileType: content.file_type, + numLines: content.num_lines, + startLine: content.start_line, + totalLines: content.total_lines, + } satisfies InferSchema< + typeof codeExecution_20250825OutputSchema + >, + providerExecuted: true, + }); + return; + } + + case 'text_editor_code_execution_create_result': { + controller.enqueue({ + type: 'tool-result', + toolCallId: part.tool_use_id, + toolName: 'text_editor_code_execution', + result: { + type: 'text_editor_code_execution_create_result', + isFileUpdate: content.is_file_update, + } satisfies InferSchema< + typeof codeExecution_20250825OutputSchema + >, + providerExecuted: true, + }); + return; + } + + case 'text_editor_code_execution_str_replace_result': { + controller.enqueue({ + type: 'tool-result', + toolCallId: part.tool_use_id, + toolName: 'text_editor_code_execution', + result: { + type: 'text_editor_code_execution_str_replace_result', + lines: content.lines, + newLines: content.new_lines, + newStart: content.new_start, + oldLines: content.old_lines, + oldStart: content.old_start, + } satisfies InferSchema< + typeof codeExecution_20250825OutputSchema + >, + providerExecuted: true, + }); + return; + } + + default: { + return; + } + } } case 'bash_code_execution_tool_result': { const part = value.content_block; - controller.enqueue({ - type: 'tool-result', - toolCallId: part.tool_use_id, - toolName: 'bash_code_execution', - result: { - type: part.content.type, - stdout: part.content.stdout, - stderr: part.content.stderr, - return_code: part.content.return_code, - }, - providerExecuted: true, - }); - return; + const content = part.content; + + switch (content.type) { + case 'bash_code_execution_tool_result_error': { + controller.enqueue({ + type: 'tool-result', + toolCallId: part.tool_use_id, + toolName: 'bash_code_execution', + result: { + type: 'bash_code_execution_tool_result_error', + errorCode: content.error_code, + } satisfies InferSchema< + typeof codeExecution_20250825OutputSchema + >, + providerExecuted: true, + }); + return; + } + case 'bash_code_execution_result': { + controller.enqueue({ + type: 'tool-result', + toolCallId: part.tool_use_id, + toolName: 'bash_code_execution', + result: { + type: 'bash_code_execution_tool_result', + stdout: content.stdout, + stderr: content.stderr, + returnCode: content.return_code, + content: content.content.map(content => ({ + type: content.type, + fileId: content.file_id, + })), + } satisfies InferSchema< + typeof codeExecution_20250825OutputSchema + >, + providerExecuted: true, + }); + return; + } + + default: { + return; + } + } } default: { diff --git a/packages/anthropic/src/tool/code-execution_20250825.ts b/packages/anthropic/src/tool/code-execution_20250825.ts index c688555ec550..8f8feba86d76 100644 --- a/packages/anthropic/src/tool/code-execution_20250825.ts +++ b/packages/anthropic/src/tool/code-execution_20250825.ts @@ -10,10 +10,12 @@ export const codeExecution_20250825OutputSchema = lazySchema(() => z.discriminatedUnion('type', [ z.object({ type: z.literal('bash_code_execution_tool_result'), - content: z.object({ - type: z.literal('bash_code_execution_output'), - fileId: z.string(), - }), + content: z.array( + z.object({ + type: z.literal('bash_code_execution_output'), + fileId: z.string(), + }), + ), stdout: z.string(), stderr: z.string(), returnCode: z.number(), @@ -50,7 +52,7 @@ export const codeExecution_20250825OutputSchema = lazySchema(() => ), ); -const codeExecution_20250825InputSchema = lazySchema(() => +export const codeExecution_20250825InputSchema = lazySchema(() => zodSchema( z.discriminatedUnion('type', [ z.object({ From 89675a46d6174081222a136e143d34f26ebb7a69 Mon Sep 17 00:00:00 2001 From: Lars Grammel Date: Tue, 14 Oct 2025 11:19:10 +0200 Subject: [PATCH 16/36] refactor --- .../src/anthropic-messages-language-model.ts | 16 ++++++++++------ 1 file changed, 10 insertions(+), 6 deletions(-) diff --git a/packages/anthropic/src/anthropic-messages-language-model.ts b/packages/anthropic/src/anthropic-messages-language-model.ts index 701935cd83ab..bc691b98de7a 100644 --- a/packages/anthropic/src/anthropic-messages-language-model.ts +++ b/packages/anthropic/src/anthropic-messages-language-model.ts @@ -804,12 +804,16 @@ export class AnthropicMessagesLanguageModel implements LanguageModelV3 { case 'server_tool_use': { if ( - value.content_block.name === 'web_fetch' || - value.content_block.name === 'web_search' || - value.content_block.name === 'code_execution' || - value.content_block.name === - 'text_editor_code_execution' || - value.content_block.name === 'bash_code_execution' + [ + 'web_fetch', + 'web_search', + // code execution 20250825: + 'code_execution', + // code execution 20250825 text editor: + 'text_editor_code_execution', + // code execution 20250825 bash: + 'bash_code_execution', + ].includes(value.content_block.name) ) { contentBlocks[value.index] = { type: 'tool-call', From 5d93678e28a4179971bbc6da143ff2a9ba2f8d21 Mon Sep 17 00:00:00 2001 From: Lars Grammel Date: Tue, 14 Oct 2025 11:23:16 +0200 Subject: [PATCH 17/36] ex2 --- .../anthropic-code-execution-20250825.ts | 34 ++++++++++++++++--- 1 file changed, 29 insertions(+), 5 deletions(-) diff --git a/examples/ai-core/src/stream-text/anthropic-code-execution-20250825.ts b/examples/ai-core/src/stream-text/anthropic-code-execution-20250825.ts index 9afa0ec070fa..78fec4faca40 100644 --- a/examples/ai-core/src/stream-text/anthropic-code-execution-20250825.ts +++ b/examples/ai-core/src/stream-text/anthropic-code-execution-20250825.ts @@ -1,7 +1,6 @@ import { anthropic } from '@ai-sdk/anthropic'; import { streamText } from 'ai'; import { run } from '../lib/run'; -import { saveRawChunks } from '../lib/save-raw-chunks'; run(async () => { const result = streamText({ @@ -15,8 +14,33 @@ run(async () => { includeRawChunks: true, }); - await saveRawChunks({ - result, - filename: 'anthropic-code-execution-20250825', - }); + for await (const part of result.fullStream) { + switch (part.type) { + case 'text-delta': { + process.stdout.write(part.text); + break; + } + + case 'tool-call': { + process.stdout.write( + `\n\nTool call: '${part.toolName}'\nInput: ${JSON.stringify(part.input, null, 2)}\n`, + ); + break; + } + + case 'tool-result': { + process.stdout.write( + `\nTool result: '${part.toolName}'\nOutput: ${JSON.stringify(part.output, null, 2)}\n`, + ); + break; + } + + case 'error': { + console.error('\n\nCode execution error:', part.error); + break; + } + } + } + + process.stdout.write('\n\n'); }); From bd197cdc7f1c443598751948065fe73504c661db Mon Sep 17 00:00:00 2001 From: Lars Grammel Date: Tue, 14 Oct 2025 11:31:13 +0200 Subject: [PATCH 18/36] ex3 --- .../anthropic-code-execution-20250825.ts | 18 +++++++++++++++ .../anthropic-code-execution-latest.ts | 23 ------------------- 2 files changed, 18 insertions(+), 23 deletions(-) create mode 100644 examples/ai-core/src/generate-text/anthropic-code-execution-20250825.ts delete mode 100644 examples/ai-core/src/generate-text/anthropic-code-execution-latest.ts diff --git a/examples/ai-core/src/generate-text/anthropic-code-execution-20250825.ts b/examples/ai-core/src/generate-text/anthropic-code-execution-20250825.ts new file mode 100644 index 000000000000..6df5792f5370 --- /dev/null +++ b/examples/ai-core/src/generate-text/anthropic-code-execution-20250825.ts @@ -0,0 +1,18 @@ +import { anthropic } from '@ai-sdk/anthropic'; +import { generateText } from 'ai'; +import { saveRawJson } from '../lib/save-raw-json'; +import { run } from '../lib/run'; + +run(async () => { + const result = await generateText({ + model: anthropic('claude-sonnet-4-5'), + prompt: + 'Write a Python script to calculate fibonacci number' + + ' and then execute it to find the 10th fibonacci number', + tools: { + code_execution: anthropic.tools.codeExecution_20250825(), + }, + }); + + console.dir(result.content, { depth: Infinity }); +}); diff --git a/examples/ai-core/src/generate-text/anthropic-code-execution-latest.ts b/examples/ai-core/src/generate-text/anthropic-code-execution-latest.ts deleted file mode 100644 index bf0bab1a06e0..000000000000 --- a/examples/ai-core/src/generate-text/anthropic-code-execution-latest.ts +++ /dev/null @@ -1,23 +0,0 @@ -import { anthropic } from '@ai-sdk/anthropic'; -import { generateText } from 'ai'; -import 'dotenv/config'; - -async function main() { - // Using the latest code execution tool with enhanced Bash and file operation support - const codeExecutionTool = anthropic.tools.codeExecution_20250825(); - - const result = await generateText({ - model: anthropic('claude-opus-4-20250514'), - prompt: - 'Calculate the mean and standard deviation of [1, 2, 3, 4, 5, 6, 7, 8, 9, 10] using Python', - tools: { - code_execution: codeExecutionTool, - }, - }); - - console.log('Result:', result.text); - console.log('\nTool Calls:', JSON.stringify(result.toolCalls, null, 2)); - console.log('\nTool Results:', JSON.stringify(result.toolResults, null, 2)); -} - -main().catch(console.error); From 1f36110d3254d63d83947935b47ef502bda93eb7 Mon Sep 17 00:00:00 2001 From: Lars Grammel Date: Tue, 14 Oct 2025 11:35:33 +0200 Subject: [PATCH 19/36] test2 --- .../anthropic-code-execution-20250825.1.json | 70 +++++++++++++++++++ ...ropic-messages-language-model.test.ts.snap | 35 ++++++++++ .../anthropic/src/anthropic-messages-api.ts | 10 +-- .../anthropic-messages-language-model.test.ts | 62 +++++++++++++++- 4 files changed, 172 insertions(+), 5 deletions(-) create mode 100644 packages/anthropic/src/__fixtures__/anthropic-code-execution-20250825.1.json diff --git a/packages/anthropic/src/__fixtures__/anthropic-code-execution-20250825.1.json b/packages/anthropic/src/__fixtures__/anthropic-code-execution-20250825.1.json new file mode 100644 index 000000000000..d8fb0cf3f8a0 --- /dev/null +++ b/packages/anthropic/src/__fixtures__/anthropic-code-execution-20250825.1.json @@ -0,0 +1,70 @@ +{ + "model": "claude-sonnet-4-5-20250929", + "id": "msg_01Wx5dv3Qisv7pjTdanFgc9Q", + "type": "message", + "role": "assistant", + "content": [ + { + "type": "text", + "text": "I'll create a Python script to calculate Fibonacci numbers and then execute it to find the 10th Fibonacci number." + }, + { + "type": "server_tool_use", + "id": "srvtoolu_012LCpyzxGBy44bCW4zCxmH2", + "name": "text_editor_code_execution", + "input": { + "command": "create", + "path": "/tmp/fibonacci.py", + "file_text": "def fibonacci(n):\n \"\"\"\n Calculate the nth Fibonacci number.\n \n Args:\n n: The position in the Fibonacci sequence (1-indexed)\n \n Returns:\n The nth Fibonacci number\n \"\"\"\n if n <= 0:\n raise ValueError(\"n must be a positive integer\")\n elif n == 1 or n == 2:\n return 1\n \n # Using iterative approach for efficiency\n fib_prev = 1 # F(1)\n fib_curr = 1 # F(2)\n \n for i in range(3, n + 1):\n fib_next = fib_prev + fib_curr\n fib_prev = fib_curr\n fib_curr = fib_next\n \n return fib_curr\n\n\nif __name__ == \"__main__\":\n # Calculate the 10th Fibonacci number\n n = 10\n result = fibonacci(n)\n print(f\"The {n}th Fibonacci number is: {result}\")\n \n # Display the first 10 Fibonacci numbers for reference\n print(f\"\\nFirst {n} Fibonacci numbers:\")\n for i in range(1, n + 1):\n print(f\"F({i}) = {fibonacci(i)}\")\n" + } + }, + { + "type": "text_editor_code_execution_tool_result", + "tool_use_id": "srvtoolu_012LCpyzxGBy44bCW4zCxmH2", + "content": { + "type": "text_editor_code_execution_create_result", + "is_file_update": false + } + }, + { "type": "text", "text": "Now let me execute the script:" }, + { + "type": "server_tool_use", + "id": "srvtoolu_01KqzcSjepCqNjqeUVqnmEjj", + "name": "bash_code_execution", + "input": { "command": "python /tmp/fibonacci.py" } + }, + { + "type": "bash_code_execution_tool_result", + "tool_use_id": "srvtoolu_01KqzcSjepCqNjqeUVqnmEjj", + "content": { + "type": "bash_code_execution_result", + "stdout": "The 10th Fibonacci number is: 55\n\nFirst 10 Fibonacci numbers:\nF(1) = 1\nF(2) = 1\nF(3) = 2\nF(4) = 3\nF(5) = 5\nF(6) = 8\nF(7) = 13\nF(8) = 21\nF(9) = 34\nF(10) = 55\n", + "stderr": "", + "return_code": 0, + "content": [] + } + }, + { + "type": "text", + "text": "Perfect! The script has been created and executed successfully. \n\n**Result: The 10th Fibonacci number is 55**\n\nThe script uses an iterative approach to calculate Fibonacci numbers efficiently, which is better than a recursive approach for larger values of n (avoiding exponential time complexity). The Fibonacci sequence shown is: 1, 1, 2, 3, 5, 8, 13, 21, 34, 55..." + } + ], + "container": { + "id": "container_011CU6rVteVhydYXRyNs6G1M", + "expires_at": "2025-10-14T10:28:40.590791Z" + }, + "stop_reason": "end_turn", + "stop_sequence": null, + "usage": { + "input_tokens": 7881, + "cache_creation_input_tokens": 0, + "cache_read_input_tokens": 0, + "cache_creation": { + "ephemeral_5m_input_tokens": 0, + "ephemeral_1h_input_tokens": 0 + }, + "output_tokens": 608, + "service_tier": "standard", + "server_tool_use": { "web_search_requests": 0, "web_fetch_requests": 0 } + } +} diff --git a/packages/anthropic/src/__snapshots__/anthropic-messages-language-model.test.ts.snap b/packages/anthropic/src/__snapshots__/anthropic-messages-language-model.test.ts.snap index f77847902955..7b7b31075145 100644 --- a/packages/anthropic/src/__snapshots__/anthropic-messages-language-model.test.ts.snap +++ b/packages/anthropic/src/__snapshots__/anthropic-messages-language-model.test.ts.snap @@ -1,5 +1,40 @@ // Vitest Snapshot v1, https://vitest.dev/guide/snapshot.html +exports[`AnthropicMessagesLanguageModel > doGenerate > code execution 20250825 > should include code execution tool call and result in content 1`] = ` +[ + { + "text": "I'll create a Python script to calculate Fibonacci numbers and then execute it to find the 10th Fibonacci number.", + "type": "text", + }, + { + "input": "{"command":"create","path":"/tmp/fibonacci.py","file_text":"def fibonacci(n):\\n \\"\\"\\"\\n Calculate the nth Fibonacci number.\\n \\n Args:\\n n: The position in the Fibonacci sequence (1-indexed)\\n \\n Returns:\\n The nth Fibonacci number\\n \\"\\"\\"\\n if n <= 0:\\n raise ValueError(\\"n must be a positive integer\\")\\n elif n == 1 or n == 2:\\n return 1\\n \\n # Using iterative approach for efficiency\\n fib_prev = 1 # F(1)\\n fib_curr = 1 # F(2)\\n \\n for i in range(3, n + 1):\\n fib_next = fib_prev + fib_curr\\n fib_prev = fib_curr\\n fib_curr = fib_next\\n \\n return fib_curr\\n\\n\\nif __name__ == \\"__main__\\":\\n # Calculate the 10th Fibonacci number\\n n = 10\\n result = fibonacci(n)\\n print(f\\"The {n}th Fibonacci number is: {result}\\")\\n \\n # Display the first 10 Fibonacci numbers for reference\\n print(f\\"\\\\nFirst {n} Fibonacci numbers:\\")\\n for i in range(1, n + 1):\\n print(f\\"F({i}) = {fibonacci(i)}\\")\\n"}", + "providerExecuted": true, + "toolCallId": "srvtoolu_012LCpyzxGBy44bCW4zCxmH2", + "toolName": "text_editor_code_execution", + "type": "tool-call", + }, + { + "text": "Now let me execute the script:", + "type": "text", + }, + { + "input": "{"command":"python /tmp/fibonacci.py"}", + "providerExecuted": true, + "toolCallId": "srvtoolu_01KqzcSjepCqNjqeUVqnmEjj", + "toolName": "bash_code_execution", + "type": "tool-call", + }, + { + "text": "Perfect! The script has been created and executed successfully. + +**Result: The 10th Fibonacci number is 55** + +The script uses an iterative approach to calculate Fibonacci numbers efficiently, which is better than a recursive approach for larger values of n (avoiding exponential time complexity). The Fibonacci sequence shown is: 1, 1, 2, 3, 5, 8, 13, 21, 34, 55...", + "type": "text", + }, +] +`; + exports[`AnthropicMessagesLanguageModel > doGenerate > web fetch tool > text response > should include web fetch tool call and result in content 1`] = ` [ { diff --git a/packages/anthropic/src/anthropic-messages-api.ts b/packages/anthropic/src/anthropic-messages-api.ts index f9849629c5a6..fdf3dc5b90ec 100644 --- a/packages/anthropic/src/anthropic-messages-api.ts +++ b/packages/anthropic/src/anthropic-messages-api.ts @@ -412,10 +412,12 @@ export const anthropicMessagesResponseSchema = lazySchema(() => content: z.discriminatedUnion('type', [ z.object({ type: z.literal('bash_code_execution_result'), - content: z.object({ - type: z.literal('bash_code_execution_output'), - file_id: z.string(), - }), + content: z.array( + z.object({ + type: z.literal('bash_code_execution_output'), + file_id: z.string(), + }), + ), stdout: z.string(), stderr: z.string(), return_code: z.number(), diff --git a/packages/anthropic/src/anthropic-messages-language-model.test.ts b/packages/anthropic/src/anthropic-messages-language-model.test.ts index 943bb85a605d..145b83917844 100644 --- a/packages/anthropic/src/anthropic-messages-language-model.test.ts +++ b/packages/anthropic/src/anthropic-messages-language-model.test.ts @@ -1651,7 +1651,67 @@ describe('AnthropicMessagesLanguageModel', () => { }); }); - describe('code execution', () => { + describe('code execution 20250825', () => { + it('should send request body with include and tool', async () => { + prepareJsonFixtureResponse('anthropic-code-execution-20250825.1'); + + await model.doGenerate({ + prompt: TEST_PROMPT, + tools: [ + { + type: 'provider-defined', + id: 'anthropic.code_execution_20250825', + name: 'code_execution', + args: {}, + }, + ], + }); + + expect(await server.calls[0].requestBodyJson).toMatchInlineSnapshot(` + { + "max_tokens": 4096, + "messages": [ + { + "content": [ + { + "text": "Hello", + "type": "text", + }, + ], + "role": "user", + }, + ], + "model": "claude-3-haiku-20240307", + "tools": [ + { + "name": "code_execution", + "type": "code_execution_20250825", + }, + ], + } + `); + }); + + it('should include code execution tool call and result in content', async () => { + prepareJsonFixtureResponse('anthropic-code-execution-20250825.1'); + + const result = await model.doGenerate({ + prompt: TEST_PROMPT, + tools: [ + { + type: 'provider-defined', + id: 'anthropic.code_execution_20250825', + name: 'code_execution', + args: {}, + }, + ], + }); + + expect(result.content).toMatchSnapshot(); + }); + }); + + describe('code execution 20250522', () => { const TEST_PROMPT = [ { role: 'user' as const, From 750cb6bf2df08be41185820acdf81cdb637e46b9 Mon Sep 17 00:00:00 2001 From: Lars Grammel Date: Tue, 14 Oct 2025 11:43:03 +0200 Subject: [PATCH 20/36] refactor --- .../src/anthropic-messages-language-model.ts | 155 +++++++++--------- 1 file changed, 76 insertions(+), 79 deletions(-) diff --git a/packages/anthropic/src/anthropic-messages-language-model.ts b/packages/anthropic/src/anthropic-messages-language-model.ts index bc691b98de7a..b9d06235c915 100644 --- a/packages/anthropic/src/anthropic-messages-language-model.ts +++ b/packages/anthropic/src/anthropic-messages-language-model.ts @@ -961,85 +961,16 @@ export class AnthropicMessagesLanguageModel implements LanguageModelV3 { case 'text_editor_code_execution_tool_result': { const part = value.content_block; - const content = part.content; - - switch (content.type) { - case 'text_editor_code_execution_tool_result_error': { - controller.enqueue({ - type: 'tool-result', - toolCallId: part.tool_use_id, - toolName: 'text_editor_code_execution', - result: { - type: 'text_editor_code_execution_tool_result_error', - errorCode: content.error_code, - } satisfies InferSchema< - typeof codeExecution_20250825OutputSchema - >, - providerExecuted: true, - }); - return; - } - - case 'text_editor_code_execution_view_result': { - controller.enqueue({ - type: 'tool-result', - toolCallId: part.tool_use_id, - toolName: 'text_editor_code_execution', - result: { - type: 'text_editor_code_execution_view_result', - content: content.content, - fileType: content.file_type, - numLines: content.num_lines, - startLine: content.start_line, - totalLines: content.total_lines, - } satisfies InferSchema< - typeof codeExecution_20250825OutputSchema - >, - providerExecuted: true, - }); - return; - } - - case 'text_editor_code_execution_create_result': { - controller.enqueue({ - type: 'tool-result', - toolCallId: part.tool_use_id, - toolName: 'text_editor_code_execution', - result: { - type: 'text_editor_code_execution_create_result', - isFileUpdate: content.is_file_update, - } satisfies InferSchema< - typeof codeExecution_20250825OutputSchema - >, - providerExecuted: true, - }); - return; - } - - case 'text_editor_code_execution_str_replace_result': { - controller.enqueue({ - type: 'tool-result', - toolCallId: part.tool_use_id, - toolName: 'text_editor_code_execution', - result: { - type: 'text_editor_code_execution_str_replace_result', - lines: content.lines, - newLines: content.new_lines, - newStart: content.new_start, - oldLines: content.old_lines, - oldStart: content.old_start, - } satisfies InferSchema< - typeof codeExecution_20250825OutputSchema - >, - providerExecuted: true, - }); - return; - } - - default: { - return; - } - } + controller.enqueue({ + type: 'tool-result', + toolCallId: part.tool_use_id, + toolName: 'text_editor_code_execution', + result: mapTextEditorCodeExecutionToolResult( + part.content, + ), + providerExecuted: true, + }); + return; } case 'bash_code_execution_tool_result': { @@ -1319,3 +1250,69 @@ export class AnthropicMessagesLanguageModel implements LanguageModelV3 { }; } } + +function mapTextEditorCodeExecutionToolResult( + content: + | { + type: 'text_editor_code_execution_tool_result_error'; + error_code: string; + } + | { + type: 'text_editor_code_execution_view_result'; + content: string; + file_type: string; + num_lines: number | null; + start_line: number | null; + total_lines: number | null; + } + | { + type: 'text_editor_code_execution_create_result'; + is_file_update: boolean; + } + | { + type: 'text_editor_code_execution_str_replace_result'; + lines: string[] | null; + new_lines: number | null; + new_start: number | null; + old_lines: number | null; + old_start: number | null; + }, +): InferSchema { + switch (content.type) { + case 'text_editor_code_execution_tool_result_error': { + return { + type: 'text_editor_code_execution_tool_result_error', + errorCode: content.error_code, + }; + } + + case 'text_editor_code_execution_view_result': { + return { + type: 'text_editor_code_execution_view_result', + content: content.content, + fileType: content.file_type, + numLines: content.num_lines, + startLine: content.start_line, + totalLines: content.total_lines, + }; + } + + case 'text_editor_code_execution_create_result': { + return { + type: 'text_editor_code_execution_create_result', + isFileUpdate: content.is_file_update, + }; + } + + case 'text_editor_code_execution_str_replace_result': { + return { + type: 'text_editor_code_execution_str_replace_result', + lines: content.lines, + newLines: content.new_lines, + newStart: content.new_start, + oldLines: content.old_lines, + oldStart: content.old_start, + }; + } + } +} From d75166e45619a823c02d9df0e78f357f0af5fdef Mon Sep 17 00:00:00 2001 From: Lars Grammel Date: Tue, 14 Oct 2025 11:46:59 +0200 Subject: [PATCH 21/36] refactor --- .../src/anthropic-messages-language-model.ts | 91 ++++++++++--------- 1 file changed, 47 insertions(+), 44 deletions(-) diff --git a/packages/anthropic/src/anthropic-messages-language-model.ts b/packages/anthropic/src/anthropic-messages-language-model.ts index b9d06235c915..f2ca5b460cbe 100644 --- a/packages/anthropic/src/anthropic-messages-language-model.ts +++ b/packages/anthropic/src/anthropic-messages-language-model.ts @@ -924,6 +924,7 @@ export class AnthropicMessagesLanguageModel implements LanguageModelV3 { return; } + // code execution 20250522: case 'code_execution_tool_result': { const part = value.content_block; @@ -959,6 +960,7 @@ export class AnthropicMessagesLanguageModel implements LanguageModelV3 { return; } + // code execution 20250825 text editor: case 'text_editor_code_execution_tool_result': { const part = value.content_block; controller.enqueue({ @@ -973,52 +975,17 @@ export class AnthropicMessagesLanguageModel implements LanguageModelV3 { return; } + // code execution 20250825 bash: case 'bash_code_execution_tool_result': { const part = value.content_block; - const content = part.content; - - switch (content.type) { - case 'bash_code_execution_tool_result_error': { - controller.enqueue({ - type: 'tool-result', - toolCallId: part.tool_use_id, - toolName: 'bash_code_execution', - result: { - type: 'bash_code_execution_tool_result_error', - errorCode: content.error_code, - } satisfies InferSchema< - typeof codeExecution_20250825OutputSchema - >, - providerExecuted: true, - }); - return; - } - case 'bash_code_execution_result': { - controller.enqueue({ - type: 'tool-result', - toolCallId: part.tool_use_id, - toolName: 'bash_code_execution', - result: { - type: 'bash_code_execution_tool_result', - stdout: content.stdout, - stderr: content.stderr, - returnCode: content.return_code, - content: content.content.map(content => ({ - type: content.type, - fileId: content.file_id, - })), - } satisfies InferSchema< - typeof codeExecution_20250825OutputSchema - >, - providerExecuted: true, - }); - return; - } - - default: { - return; - } - } + controller.enqueue({ + type: 'tool-result', + toolCallId: part.tool_use_id, + toolName: 'bash_code_execution', + result: mapBashCodeExecutionToolResult(part.content), + providerExecuted: true, + }); + return; } default: { @@ -1251,6 +1218,42 @@ export class AnthropicMessagesLanguageModel implements LanguageModelV3 { } } +function mapBashCodeExecutionToolResult( + content: + | { + type: 'bash_code_execution_tool_result_error'; + error_code: string; + } + | { + type: 'bash_code_execution_result'; + content: { type: 'bash_code_execution_output'; file_id: string }[]; + stdout: string; + stderr: string; + return_code: number; + }, +): InferSchema { + switch (content.type) { + case 'bash_code_execution_tool_result_error': { + return { + type: 'bash_code_execution_tool_result_error', + errorCode: content.error_code, + }; + } + case 'bash_code_execution_result': { + return { + type: 'bash_code_execution_tool_result', + stdout: content.stdout, + stderr: content.stderr, + returnCode: content.return_code, + content: content.content.map(content => ({ + type: content.type, + fileId: content.file_id, + })), + }; + } + } +} + function mapTextEditorCodeExecutionToolResult( content: | { From 18750e523e6ac2c9e0d7729c96c1f496f9ff39ac Mon Sep 17 00:00:00 2001 From: Lars Grammel Date: Tue, 14 Oct 2025 11:47:56 +0200 Subject: [PATCH 22/36] generate --- ...ropic-messages-language-model.test.ts.snap | 36 +++++++++++++ .../src/anthropic-messages-language-model.ts | 54 +++++++++---------- 2 files changed, 62 insertions(+), 28 deletions(-) diff --git a/packages/anthropic/src/__snapshots__/anthropic-messages-language-model.test.ts.snap b/packages/anthropic/src/__snapshots__/anthropic-messages-language-model.test.ts.snap index 7b7b31075145..4eb4e499178b 100644 --- a/packages/anthropic/src/__snapshots__/anthropic-messages-language-model.test.ts.snap +++ b/packages/anthropic/src/__snapshots__/anthropic-messages-language-model.test.ts.snap @@ -13,6 +13,16 @@ exports[`AnthropicMessagesLanguageModel > doGenerate > code execution 20250825 > "toolName": "text_editor_code_execution", "type": "tool-call", }, + { + "providerExecuted": true, + "result": { + "isFileUpdate": false, + "type": "text_editor_code_execution_create_result", + }, + "toolCallId": "srvtoolu_012LCpyzxGBy44bCW4zCxmH2", + "toolName": "text_editor_code_execution", + "type": "tool-result", + }, { "text": "Now let me execute the script:", "type": "text", @@ -24,6 +34,32 @@ exports[`AnthropicMessagesLanguageModel > doGenerate > code execution 20250825 > "toolName": "bash_code_execution", "type": "tool-call", }, + { + "providerExecuted": true, + "result": { + "content": [], + "returnCode": 0, + "stderr": "", + "stdout": "The 10th Fibonacci number is: 55 + +First 10 Fibonacci numbers: +F(1) = 1 +F(2) = 1 +F(3) = 2 +F(4) = 3 +F(5) = 5 +F(6) = 8 +F(7) = 13 +F(8) = 21 +F(9) = 34 +F(10) = 55 +", + "type": "bash_code_execution_tool_result", + }, + "toolCallId": "srvtoolu_01KqzcSjepCqNjqeUVqnmEjj", + "toolName": "bash_code_execution", + "type": "tool-result", + }, { "text": "Perfect! The script has been created and executed successfully. diff --git a/packages/anthropic/src/anthropic-messages-language-model.ts b/packages/anthropic/src/anthropic-messages-language-model.ts index f2ca5b460cbe..536419d77d67 100644 --- a/packages/anthropic/src/anthropic-messages-language-model.ts +++ b/packages/anthropic/src/anthropic-messages-language-model.ts @@ -563,6 +563,8 @@ export class AnthropicMessagesLanguageModel implements LanguageModelV3 { } break; } + + // code execution 20250522: case 'code_execution_tool_result': { if (part.content.type === 'code_execution_result') { content.push({ @@ -592,34 +594,30 @@ export class AnthropicMessagesLanguageModel implements LanguageModelV3 { } break; } - // case 'text_editor_code_execution_tool_result': { - // content.push({ - // type: 'tool-result', - // toolCallId: part.tool_use_id, - // toolName: 'text_editor_code_execution', - // result: { - // type: part.content.type, - // isFileUpdate: part.content.is_file_update, - // }, - // providerExecuted: true, - // }); - // break; - // } - // case 'bash_code_execution_tool_result': { - // content.push({ - // type: 'tool-result', - // toolCallId: part.tool_use_id, - // toolName: 'bash_code_execution', - // result: { - // type: part.content.type, - // stdout: part.content.stdout, - // stderr: part.content.stderr, - // return_code: part.content.return_code, - // }, - // providerExecuted: true, - // }); - // break; - // } + + // code execution 20250825 text editor: + case 'text_editor_code_execution_tool_result': { + content.push({ + type: 'tool-result', + toolCallId: part.tool_use_id, + toolName: 'text_editor_code_execution', + result: mapTextEditorCodeExecutionToolResult(part.content), + providerExecuted: true, + }); + break; + } + + // code execution 20250825 bash: + case 'bash_code_execution_tool_result': { + content.push({ + type: 'tool-result', + toolCallId: part.tool_use_id, + toolName: 'bash_code_execution', + result: mapBashCodeExecutionToolResult(part.content), + providerExecuted: true, + }); + break; + } } } From f0f07edb087a2a6945ee6ea9a2cb54d2e90ba902 Mon Sep 17 00:00:00 2001 From: Lars Grammel Date: Tue, 14 Oct 2025 11:53:46 +0200 Subject: [PATCH 23/36] fix --- .../anthropic-messages-language-model.test.ts.snap | 8 ++++---- .../anthropic/src/anthropic-messages-language-model.ts | 8 ++++---- 2 files changed, 8 insertions(+), 8 deletions(-) diff --git a/packages/anthropic/src/__snapshots__/anthropic-messages-language-model.test.ts.snap b/packages/anthropic/src/__snapshots__/anthropic-messages-language-model.test.ts.snap index 4eb4e499178b..3244208ee093 100644 --- a/packages/anthropic/src/__snapshots__/anthropic-messages-language-model.test.ts.snap +++ b/packages/anthropic/src/__snapshots__/anthropic-messages-language-model.test.ts.snap @@ -20,7 +20,7 @@ exports[`AnthropicMessagesLanguageModel > doGenerate > code execution 20250825 > "type": "text_editor_code_execution_create_result", }, "toolCallId": "srvtoolu_012LCpyzxGBy44bCW4zCxmH2", - "toolName": "text_editor_code_execution", + "toolName": "code_execution", "type": "tool-result", }, { @@ -57,7 +57,7 @@ F(10) = 55 "type": "bash_code_execution_tool_result", }, "toolCallId": "srvtoolu_01KqzcSjepCqNjqeUVqnmEjj", - "toolName": "bash_code_execution", + "toolName": "code_execution", "type": "tool-result", }, { @@ -1581,7 +1581,7 @@ exports[`AnthropicMessagesLanguageModel > doStream > raw chunks > code execution "type": "text_editor_code_execution_create_result", }, "toolCallId": "srvtoolu_0112cP8RpnKv67t2cscmN4ia", - "toolName": "text_editor_code_execution", + "toolName": "code_execution", "type": "tool-result", }, { @@ -1682,7 +1682,7 @@ F(10) = 34 "type": "bash_code_execution_tool_result", }, "toolCallId": "srvtoolu_01K2E2j5mkxbtLqNBc6RJHds", - "toolName": "bash_code_execution", + "toolName": "code_execution", "type": "tool-result", }, { diff --git a/packages/anthropic/src/anthropic-messages-language-model.ts b/packages/anthropic/src/anthropic-messages-language-model.ts index 536419d77d67..b39f545c02dc 100644 --- a/packages/anthropic/src/anthropic-messages-language-model.ts +++ b/packages/anthropic/src/anthropic-messages-language-model.ts @@ -600,7 +600,7 @@ export class AnthropicMessagesLanguageModel implements LanguageModelV3 { content.push({ type: 'tool-result', toolCallId: part.tool_use_id, - toolName: 'text_editor_code_execution', + toolName: 'code_execution', result: mapTextEditorCodeExecutionToolResult(part.content), providerExecuted: true, }); @@ -612,7 +612,7 @@ export class AnthropicMessagesLanguageModel implements LanguageModelV3 { content.push({ type: 'tool-result', toolCallId: part.tool_use_id, - toolName: 'bash_code_execution', + toolName: 'code_execution', result: mapBashCodeExecutionToolResult(part.content), providerExecuted: true, }); @@ -964,7 +964,7 @@ export class AnthropicMessagesLanguageModel implements LanguageModelV3 { controller.enqueue({ type: 'tool-result', toolCallId: part.tool_use_id, - toolName: 'text_editor_code_execution', + toolName: 'code_execution', result: mapTextEditorCodeExecutionToolResult( part.content, ), @@ -979,7 +979,7 @@ export class AnthropicMessagesLanguageModel implements LanguageModelV3 { controller.enqueue({ type: 'tool-result', toolCallId: part.tool_use_id, - toolName: 'bash_code_execution', + toolName: 'code_execution', result: mapBashCodeExecutionToolResult(part.content), providerExecuted: true, }); From e427ee4af85368da081ad8201afd98fba8ce2d2e Mon Sep 17 00:00:00 2001 From: Lars Grammel Date: Tue, 14 Oct 2025 11:54:27 +0200 Subject: [PATCH 24/36] clean --- .../src/generate-text/anthropic-code-execution-20250825.ts | 1 - 1 file changed, 1 deletion(-) diff --git a/examples/ai-core/src/generate-text/anthropic-code-execution-20250825.ts b/examples/ai-core/src/generate-text/anthropic-code-execution-20250825.ts index 6df5792f5370..44059a531a61 100644 --- a/examples/ai-core/src/generate-text/anthropic-code-execution-20250825.ts +++ b/examples/ai-core/src/generate-text/anthropic-code-execution-20250825.ts @@ -1,6 +1,5 @@ import { anthropic } from '@ai-sdk/anthropic'; import { generateText } from 'ai'; -import { saveRawJson } from '../lib/save-raw-json'; import { run } from '../lib/run'; run(async () => { From 72aef196fd202479818f734f44ed6b13764d7eb3 Mon Sep 17 00:00:00 2001 From: Lars Grammel Date: Tue, 14 Oct 2025 12:01:44 +0200 Subject: [PATCH 25/36] generate input --- ...ropic-messages-language-model.test.ts.snap | 8 ++++---- .../src/anthropic-messages-language-model.ts | 16 +++++++++++++--- .../src/tool/code-execution_20250825.ts | 19 ++++++++++++++++++- 3 files changed, 35 insertions(+), 8 deletions(-) diff --git a/packages/anthropic/src/__snapshots__/anthropic-messages-language-model.test.ts.snap b/packages/anthropic/src/__snapshots__/anthropic-messages-language-model.test.ts.snap index 3244208ee093..63f3de13f77c 100644 --- a/packages/anthropic/src/__snapshots__/anthropic-messages-language-model.test.ts.snap +++ b/packages/anthropic/src/__snapshots__/anthropic-messages-language-model.test.ts.snap @@ -7,10 +7,10 @@ exports[`AnthropicMessagesLanguageModel > doGenerate > code execution 20250825 > "type": "text", }, { - "input": "{"command":"create","path":"/tmp/fibonacci.py","file_text":"def fibonacci(n):\\n \\"\\"\\"\\n Calculate the nth Fibonacci number.\\n \\n Args:\\n n: The position in the Fibonacci sequence (1-indexed)\\n \\n Returns:\\n The nth Fibonacci number\\n \\"\\"\\"\\n if n <= 0:\\n raise ValueError(\\"n must be a positive integer\\")\\n elif n == 1 or n == 2:\\n return 1\\n \\n # Using iterative approach for efficiency\\n fib_prev = 1 # F(1)\\n fib_curr = 1 # F(2)\\n \\n for i in range(3, n + 1):\\n fib_next = fib_prev + fib_curr\\n fib_prev = fib_curr\\n fib_curr = fib_next\\n \\n return fib_curr\\n\\n\\nif __name__ == \\"__main__\\":\\n # Calculate the 10th Fibonacci number\\n n = 10\\n result = fibonacci(n)\\n print(f\\"The {n}th Fibonacci number is: {result}\\")\\n \\n # Display the first 10 Fibonacci numbers for reference\\n print(f\\"\\\\nFirst {n} Fibonacci numbers:\\")\\n for i in range(1, n + 1):\\n print(f\\"F({i}) = {fibonacci(i)}\\")\\n"}", + "input": "{"type":"text_editor_code_execution","command":"create","path":"/tmp/fibonacci.py","file_text":"def fibonacci(n):\\n \\"\\"\\"\\n Calculate the nth Fibonacci number.\\n \\n Args:\\n n: The position in the Fibonacci sequence (1-indexed)\\n \\n Returns:\\n The nth Fibonacci number\\n \\"\\"\\"\\n if n <= 0:\\n raise ValueError(\\"n must be a positive integer\\")\\n elif n == 1 or n == 2:\\n return 1\\n \\n # Using iterative approach for efficiency\\n fib_prev = 1 # F(1)\\n fib_curr = 1 # F(2)\\n \\n for i in range(3, n + 1):\\n fib_next = fib_prev + fib_curr\\n fib_prev = fib_curr\\n fib_curr = fib_next\\n \\n return fib_curr\\n\\n\\nif __name__ == \\"__main__\\":\\n # Calculate the 10th Fibonacci number\\n n = 10\\n result = fibonacci(n)\\n print(f\\"The {n}th Fibonacci number is: {result}\\")\\n \\n # Display the first 10 Fibonacci numbers for reference\\n print(f\\"\\\\nFirst {n} Fibonacci numbers:\\")\\n for i in range(1, n + 1):\\n print(f\\"F({i}) = {fibonacci(i)}\\")\\n"}", "providerExecuted": true, "toolCallId": "srvtoolu_012LCpyzxGBy44bCW4zCxmH2", - "toolName": "text_editor_code_execution", + "toolName": "code_execution", "type": "tool-call", }, { @@ -28,10 +28,10 @@ exports[`AnthropicMessagesLanguageModel > doGenerate > code execution 20250825 > "type": "text", }, { - "input": "{"command":"python /tmp/fibonacci.py"}", + "input": "{"type":"bash_code_execution","command":"python /tmp/fibonacci.py"}", "providerExecuted": true, "toolCallId": "srvtoolu_01KqzcSjepCqNjqeUVqnmEjj", - "toolName": "bash_code_execution", + "toolName": "code_execution", "type": "tool-call", }, { diff --git a/packages/anthropic/src/anthropic-messages-language-model.ts b/packages/anthropic/src/anthropic-messages-language-model.ts index b39f545c02dc..daf4f761f141 100644 --- a/packages/anthropic/src/anthropic-messages-language-model.ts +++ b/packages/anthropic/src/anthropic-messages-language-model.ts @@ -462,12 +462,22 @@ export class AnthropicMessagesLanguageModel implements LanguageModelV3 { break; } case 'server_tool_use': { + // code execution 20250825 needs mapping: if ( - part.name === 'web_search' || - part.name === 'code_execution' || - part.name === 'web_fetch' || part.name === 'text_editor_code_execution' || part.name === 'bash_code_execution' + ) { + content.push({ + type: 'tool-call', + toolCallId: part.id, + toolName: 'code_execution', + input: JSON.stringify({ type: part.name, ...part.input }), + providerExecuted: true, + }); + } else if ( + part.name === 'web_search' || + part.name === 'code_execution' || + part.name === 'web_fetch' ) { content.push({ type: 'tool-call', diff --git a/packages/anthropic/src/tool/code-execution_20250825.ts b/packages/anthropic/src/tool/code-execution_20250825.ts index 8f8feba86d76..ee300a990f40 100644 --- a/packages/anthropic/src/tool/code-execution_20250825.ts +++ b/packages/anthropic/src/tool/code-execution_20250825.ts @@ -61,7 +61,9 @@ export const codeExecution_20250825InputSchema = lazySchema(() => }), z.object({ type: z.literal('text_editor_code_execution'), - // TODO what is supported here? + command: z.string(), + path: z.string(), + file_text: z.string().nullish(), }), ]), ), @@ -78,6 +80,21 @@ const factory = createProviderDefinedToolFactoryWithOutputSchema< } | { type: 'text_editor_code_execution'; + + /** + * The command to run. + */ + command: string; + + /** + * The path to the file to edit. + */ + path: string; + + /** + * The text of the file to edit. + */ + file_text?: string | null; }, | { type: 'bash_code_execution_tool_result'; From b11fe09b1587ba08c6119ab90626286a61ccb972 Mon Sep 17 00:00:00 2001 From: Lars Grammel Date: Tue, 14 Oct 2025 12:23:33 +0200 Subject: [PATCH 26/36] input tool mapping --- ...ropic-messages-language-model.test.ts.snap | 36 +++---------- .../anthropic-messages-language-model.test.ts | 11 +--- .../src/anthropic-messages-language-model.ts | 51 +++++++++++++++++-- 3 files changed, 57 insertions(+), 41 deletions(-) diff --git a/packages/anthropic/src/__snapshots__/anthropic-messages-language-model.test.ts.snap b/packages/anthropic/src/__snapshots__/anthropic-messages-language-model.test.ts.snap index 63f3de13f77c..7b7b00417a4f 100644 --- a/packages/anthropic/src/__snapshots__/anthropic-messages-language-model.test.ts.snap +++ b/packages/anthropic/src/__snapshots__/anthropic-messages-language-model.test.ts.snap @@ -570,16 +570,11 @@ exports[`AnthropicMessagesLanguageModel > doStream > raw chunks > code execution { "id": "srvtoolu_0112cP8RpnKv67t2cscmN4ia", "providerExecuted": true, - "toolName": "text_editor_code_execution", + "toolName": "code_execution", "type": "tool-input-start", }, { - "delta": "", - "id": "srvtoolu_0112cP8RpnKv67t2cscmN4ia", - "type": "tool-input-delta", - }, - { - "delta": "{"", + "delta": "{"type": "text_editor_code_execution","", "id": "srvtoolu_0112cP8RpnKv67t2cscmN4ia", "type": "tool-input-delta", }, @@ -1568,10 +1563,10 @@ exports[`AnthropicMessagesLanguageModel > doStream > raw chunks > code execution "type": "tool-input-end", }, { - "input": "{"command": "create", "path": "/tmp/fibonacci.py", "file_text": "def fibonacci(n):\\n \\"\\"\\"\\n Calculate the nth Fibonacci number.\\n \\n Args:\\n n: The position in the Fibonacci sequence (1-indexed)\\n \\n Returns:\\n The nth Fibonacci number\\n \\"\\"\\"\\n if n <= 0:\\n return \\"Please enter a positive integer\\"\\n elif n == 1:\\n return 0\\n elif n == 2:\\n return 1\\n else:\\n # Using iterative approach for efficiency\\n a, b = 0, 1\\n for _ in range(2, n):\\n a, b = b, a + b\\n return b\\n\\ndef fibonacci_recursive(n):\\n \\"\\"\\"\\n Calculate the nth Fibonacci number using recursion.\\n \\n Args:\\n n: The position in the Fibonacci sequence (1-indexed)\\n \\n Returns:\\n The nth Fibonacci number\\n \\"\\"\\"\\n if n <= 0:\\n return \\"Please enter a positive integer\\"\\n elif n == 1:\\n return 0\\n elif n == 2:\\n return 1\\n else:\\n return fibonacci_recursive(n-1) + fibonacci_recursive(n-2)\\n\\nif __name__ == \\"__main__\\":\\n # Calculate the 10th Fibonacci number\\n n = 10\\n result = fibonacci(n)\\n \\n print(f\\"The {n}th Fibonacci number is: {result}\\")\\n \\n # Show the first 10 Fibonacci numbers\\n print(f\\"\\\\nFirst {n} Fibonacci numbers:\\")\\n for i in range(1, n + 1):\\n print(f\\"F({i}) = {fibonacci(i)}\\")\\n"}", + "input": "{"type": "text_editor_code_execution","command": "create", "path": "/tmp/fibonacci.py", "file_text": "def fibonacci(n):\\n \\"\\"\\"\\n Calculate the nth Fibonacci number.\\n \\n Args:\\n n: The position in the Fibonacci sequence (1-indexed)\\n \\n Returns:\\n The nth Fibonacci number\\n \\"\\"\\"\\n if n <= 0:\\n return \\"Please enter a positive integer\\"\\n elif n == 1:\\n return 0\\n elif n == 2:\\n return 1\\n else:\\n # Using iterative approach for efficiency\\n a, b = 0, 1\\n for _ in range(2, n):\\n a, b = b, a + b\\n return b\\n\\ndef fibonacci_recursive(n):\\n \\"\\"\\"\\n Calculate the nth Fibonacci number using recursion.\\n \\n Args:\\n n: The position in the Fibonacci sequence (1-indexed)\\n \\n Returns:\\n The nth Fibonacci number\\n \\"\\"\\"\\n if n <= 0:\\n return \\"Please enter a positive integer\\"\\n elif n == 1:\\n return 0\\n elif n == 2:\\n return 1\\n else:\\n return fibonacci_recursive(n-1) + fibonacci_recursive(n-2)\\n\\nif __name__ == \\"__main__\\":\\n # Calculate the 10th Fibonacci number\\n n = 10\\n result = fibonacci(n)\\n \\n print(f\\"The {n}th Fibonacci number is: {result}\\")\\n \\n # Show the first 10 Fibonacci numbers\\n print(f\\"\\\\nFirst {n} Fibonacci numbers:\\")\\n for i in range(1, n + 1):\\n print(f\\"F({i}) = {fibonacci(i)}\\")\\n"}", "providerExecuted": true, "toolCallId": "srvtoolu_0112cP8RpnKv67t2cscmN4ia", - "toolName": "text_editor_code_execution", + "toolName": "code_execution", "type": "tool-call", }, { @@ -1610,16 +1605,11 @@ exports[`AnthropicMessagesLanguageModel > doStream > raw chunks > code execution { "id": "srvtoolu_01K2E2j5mkxbtLqNBc6RJHds", "providerExecuted": true, - "toolName": "bash_code_execution", + "toolName": "code_execution", "type": "tool-input-start", }, { - "delta": "", - "id": "srvtoolu_01K2E2j5mkxbtLqNBc6RJHds", - "type": "tool-input-delta", - }, - { - "delta": "{"command", + "delta": "{"type": "bash_code_execution","command", "id": "srvtoolu_01K2E2j5mkxbtLqNBc6RJHds", "type": "tool-input-delta", }, @@ -1653,10 +1643,10 @@ exports[`AnthropicMessagesLanguageModel > doStream > raw chunks > code execution "type": "tool-input-end", }, { - "input": "{"command": "python /tmp/fibonacci.py"}", + "input": "{"type": "bash_code_execution","command": "python /tmp/fibonacci.py"}", "providerExecuted": true, "toolCallId": "srvtoolu_01K2E2j5mkxbtLqNBc6RJHds", - "toolName": "bash_code_execution", + "toolName": "code_execution", "type": "tool-call", }, { @@ -1867,11 +1857,6 @@ exports[`AnthropicMessagesLanguageModel > doStream > raw chunks > web fetch tool "toolName": "web_fetch", "type": "tool-input-start", }, - { - "delta": "", - "id": "srvtoolu_01VNMRfQny2LCrLKEdYaVcCe", - "type": "tool-input-delta", - }, { "delta": "{"url": ", "id": "srvtoolu_01VNMRfQny2LCrLKEdYaVcCe", @@ -2252,11 +2237,6 @@ exports[`AnthropicMessagesLanguageModel > doStream > raw chunks > web search too "toolName": "web_search", "type": "tool-input-start", }, - { - "delta": "", - "id": "srvtoolu_01Bj5uzzLcYG5hfueSLcDH8k", - "type": "tool-input-delta", - }, { "delta": "{"query": "t", "id": "srvtoolu_01Bj5uzzLcYG5hfueSLcDH8k", diff --git a/packages/anthropic/src/anthropic-messages-language-model.test.ts b/packages/anthropic/src/anthropic-messages-language-model.test.ts index 145b83917844..9dc22b688d26 100644 --- a/packages/anthropic/src/anthropic-messages-language-model.test.ts +++ b/packages/anthropic/src/anthropic-messages-language-model.test.ts @@ -2078,11 +2078,6 @@ describe('AnthropicMessagesLanguageModel', () => { "id": "1", "type": "text-start", }, - { - "delta": "", - "id": "1", - "type": "text-delta", - }, { "delta": "{"value", "id": "1", @@ -2532,11 +2527,6 @@ describe('AnthropicMessagesLanguageModel', () => { "toolName": "test-tool", "type": "tool-input-start", }, - { - "delta": "", - "id": "toolu_01DBsB4vvYLnBDzZ5rBSxSLs", - "type": "tool-input-delta", - }, { "delta": "{"value", "id": "toolu_01DBsB4vvYLnBDzZ5rBSxSLs", @@ -2568,6 +2558,7 @@ describe('AnthropicMessagesLanguageModel', () => { }, { "input": "{"value":"Sparkle Day"}", + "providerExecuted": undefined, "toolCallId": "toolu_01DBsB4vvYLnBDzZ5rBSxSLs", "toolName": "test-tool", "type": "tool-call", diff --git a/packages/anthropic/src/anthropic-messages-language-model.ts b/packages/anthropic/src/anthropic-messages-language-model.ts index daf4f761f141..29a3679ecdcb 100644 --- a/packages/anthropic/src/anthropic-messages-language-model.ts +++ b/packages/anthropic/src/anthropic-messages-language-model.ts @@ -700,6 +700,7 @@ export class AnthropicMessagesLanguageModel implements LanguageModelV3 { toolName: string; input: string; providerExecuted?: boolean; + firstDelta: boolean; } | { type: 'text' | 'reasoning' } > = {}; @@ -796,6 +797,7 @@ export class AnthropicMessagesLanguageModel implements LanguageModelV3 { toolCallId: value.content_block.id, toolName: value.content_block.name, input: '', + firstDelta: true, }; controller.enqueue( @@ -829,11 +831,21 @@ export class AnthropicMessagesLanguageModel implements LanguageModelV3 { toolName: value.content_block.name, input: '', providerExecuted: true, + firstDelta: true, }; + + // map tool names for the code execution 20250825 tool: + const mappedToolName = + value.content_block.name === + 'text_editor_code_execution' || + value.content_block.name === 'bash_code_execution' + ? 'code_execution' + : value.content_block.name; + controller.enqueue({ type: 'tool-input-start', id: value.content_block.id, - toolName: value.content_block.name, + toolName: mappedToolName, providerExecuted: true, }); } @@ -1035,7 +1047,22 @@ export class AnthropicMessagesLanguageModel implements LanguageModelV3 { type: 'tool-input-end', id: contentBlock.toolCallId, }); - controller.enqueue(contentBlock); + + // map tool names for the code execution 20250825 tool: + const toolName = + contentBlock.toolName === + 'text_editor_code_execution' || + contentBlock.toolName === 'bash_code_execution' + ? 'code_execution' + : contentBlock.toolName; + + controller.enqueue({ + type: 'tool-call', + toolCallId: contentBlock.toolCallId, + toolName, + input: contentBlock.input, + providerExecuted: contentBlock.providerExecuted, + }); } break; } @@ -1097,7 +1124,13 @@ export class AnthropicMessagesLanguageModel implements LanguageModelV3 { case 'input_json_delta': { const contentBlock = contentBlocks[value.index]; - const delta = value.delta.partial_json; + let delta = value.delta.partial_json; + + // skip empty deltas to enable replacing the first character + // in the code execution 20250825 tool. + if (delta.length === 0) { + return; + } if (usesJsonResponseTool) { if (contentBlock?.type !== 'text') { @@ -1114,6 +1147,17 @@ export class AnthropicMessagesLanguageModel implements LanguageModelV3 { return; } + // for the code execution 20250825, we need to add + // the type to the delta and change the tool name. + if ( + contentBlock.firstDelta && + (contentBlock.toolName === 'bash_code_execution' || + contentBlock.toolName === + 'text_editor_code_execution') + ) { + delta = `{"type": "${contentBlock.toolName}",${delta.substring(1)}`; + } + controller.enqueue({ type: 'tool-input-delta', id: contentBlock.toolCallId, @@ -1121,6 +1165,7 @@ export class AnthropicMessagesLanguageModel implements LanguageModelV3 { }); contentBlock.input += delta; + contentBlock.firstDelta = false; } return; From 3be1418ea4e1807774ce76a5129384c21c05d63c Mon Sep 17 00:00:00 2001 From: Lars Grammel Date: Tue, 14 Oct 2025 12:28:40 +0200 Subject: [PATCH 27/36] no mapping --- ...ropic-messages-language-model.test.ts.snap | 12 +- .../src/anthropic-messages-language-model.ts | 139 +----------------- .../src/tool/code-execution_20250825.ts | 54 +++---- 3 files changed, 39 insertions(+), 166 deletions(-) diff --git a/packages/anthropic/src/__snapshots__/anthropic-messages-language-model.test.ts.snap b/packages/anthropic/src/__snapshots__/anthropic-messages-language-model.test.ts.snap index 7b7b00417a4f..0f73790eb110 100644 --- a/packages/anthropic/src/__snapshots__/anthropic-messages-language-model.test.ts.snap +++ b/packages/anthropic/src/__snapshots__/anthropic-messages-language-model.test.ts.snap @@ -16,7 +16,7 @@ exports[`AnthropicMessagesLanguageModel > doGenerate > code execution 20250825 > { "providerExecuted": true, "result": { - "isFileUpdate": false, + "is_file_update": false, "type": "text_editor_code_execution_create_result", }, "toolCallId": "srvtoolu_012LCpyzxGBy44bCW4zCxmH2", @@ -38,7 +38,7 @@ exports[`AnthropicMessagesLanguageModel > doGenerate > code execution 20250825 > "providerExecuted": true, "result": { "content": [], - "returnCode": 0, + "return_code": 0, "stderr": "", "stdout": "The 10th Fibonacci number is: 55 @@ -54,7 +54,7 @@ F(8) = 21 F(9) = 34 F(10) = 55 ", - "type": "bash_code_execution_tool_result", + "type": "bash_code_execution_result", }, "toolCallId": "srvtoolu_01KqzcSjepCqNjqeUVqnmEjj", "toolName": "code_execution", @@ -1572,7 +1572,7 @@ exports[`AnthropicMessagesLanguageModel > doStream > raw chunks > code execution { "providerExecuted": true, "result": { - "isFileUpdate": false, + "is_file_update": false, "type": "text_editor_code_execution_create_result", }, "toolCallId": "srvtoolu_0112cP8RpnKv67t2cscmN4ia", @@ -1653,7 +1653,7 @@ exports[`AnthropicMessagesLanguageModel > doStream > raw chunks > code execution "providerExecuted": true, "result": { "content": [], - "returnCode": 0, + "return_code": 0, "stderr": "", "stdout": "The 10th Fibonacci number is: 34 @@ -1669,7 +1669,7 @@ F(8) = 13 F(9) = 21 F(10) = 34 ", - "type": "bash_code_execution_tool_result", + "type": "bash_code_execution_result", }, "toolCallId": "srvtoolu_01K2E2j5mkxbtLqNBc6RJHds", "toolName": "code_execution", diff --git a/packages/anthropic/src/anthropic-messages-language-model.ts b/packages/anthropic/src/anthropic-messages-language-model.ts index 29a3679ecdcb..dc9b4b2c20e4 100644 --- a/packages/anthropic/src/anthropic-messages-language-model.ts +++ b/packages/anthropic/src/anthropic-messages-language-model.ts @@ -605,25 +605,14 @@ export class AnthropicMessagesLanguageModel implements LanguageModelV3 { break; } - // code execution 20250825 text editor: + // code execution 20250825: + case 'bash_code_execution_tool_result': case 'text_editor_code_execution_tool_result': { content.push({ type: 'tool-result', toolCallId: part.tool_use_id, toolName: 'code_execution', - result: mapTextEditorCodeExecutionToolResult(part.content), - providerExecuted: true, - }); - break; - } - - // code execution 20250825 bash: - case 'bash_code_execution_tool_result': { - content.push({ - type: 'tool-result', - toolCallId: part.tool_use_id, - toolName: 'code_execution', - result: mapBashCodeExecutionToolResult(part.content), + result: part.content, providerExecuted: true, }); break; @@ -980,29 +969,15 @@ export class AnthropicMessagesLanguageModel implements LanguageModelV3 { return; } - // code execution 20250825 text editor: + // code execution 20250825: + case 'bash_code_execution_tool_result': case 'text_editor_code_execution_tool_result': { const part = value.content_block; controller.enqueue({ type: 'tool-result', toolCallId: part.tool_use_id, toolName: 'code_execution', - result: mapTextEditorCodeExecutionToolResult( - part.content, - ), - providerExecuted: true, - }); - return; - } - - // code execution 20250825 bash: - case 'bash_code_execution_tool_result': { - const part = value.content_block; - controller.enqueue({ - type: 'tool-result', - toolCallId: part.tool_use_id, - toolName: 'code_execution', - result: mapBashCodeExecutionToolResult(part.content), + result: part.content, providerExecuted: true, }); return; @@ -1270,105 +1245,3 @@ export class AnthropicMessagesLanguageModel implements LanguageModelV3 { }; } } - -function mapBashCodeExecutionToolResult( - content: - | { - type: 'bash_code_execution_tool_result_error'; - error_code: string; - } - | { - type: 'bash_code_execution_result'; - content: { type: 'bash_code_execution_output'; file_id: string }[]; - stdout: string; - stderr: string; - return_code: number; - }, -): InferSchema { - switch (content.type) { - case 'bash_code_execution_tool_result_error': { - return { - type: 'bash_code_execution_tool_result_error', - errorCode: content.error_code, - }; - } - case 'bash_code_execution_result': { - return { - type: 'bash_code_execution_tool_result', - stdout: content.stdout, - stderr: content.stderr, - returnCode: content.return_code, - content: content.content.map(content => ({ - type: content.type, - fileId: content.file_id, - })), - }; - } - } -} - -function mapTextEditorCodeExecutionToolResult( - content: - | { - type: 'text_editor_code_execution_tool_result_error'; - error_code: string; - } - | { - type: 'text_editor_code_execution_view_result'; - content: string; - file_type: string; - num_lines: number | null; - start_line: number | null; - total_lines: number | null; - } - | { - type: 'text_editor_code_execution_create_result'; - is_file_update: boolean; - } - | { - type: 'text_editor_code_execution_str_replace_result'; - lines: string[] | null; - new_lines: number | null; - new_start: number | null; - old_lines: number | null; - old_start: number | null; - }, -): InferSchema { - switch (content.type) { - case 'text_editor_code_execution_tool_result_error': { - return { - type: 'text_editor_code_execution_tool_result_error', - errorCode: content.error_code, - }; - } - - case 'text_editor_code_execution_view_result': { - return { - type: 'text_editor_code_execution_view_result', - content: content.content, - fileType: content.file_type, - numLines: content.num_lines, - startLine: content.start_line, - totalLines: content.total_lines, - }; - } - - case 'text_editor_code_execution_create_result': { - return { - type: 'text_editor_code_execution_create_result', - isFileUpdate: content.is_file_update, - }; - } - - case 'text_editor_code_execution_str_replace_result': { - return { - type: 'text_editor_code_execution_str_replace_result', - lines: content.lines, - newLines: content.new_lines, - newStart: content.new_start, - oldLines: content.old_lines, - oldStart: content.old_start, - }; - } - } -} diff --git a/packages/anthropic/src/tool/code-execution_20250825.ts b/packages/anthropic/src/tool/code-execution_20250825.ts index ee300a990f40..ffb2f3a5e40a 100644 --- a/packages/anthropic/src/tool/code-execution_20250825.ts +++ b/packages/anthropic/src/tool/code-execution_20250825.ts @@ -9,44 +9,44 @@ export const codeExecution_20250825OutputSchema = lazySchema(() => zodSchema( z.discriminatedUnion('type', [ z.object({ - type: z.literal('bash_code_execution_tool_result'), + type: z.literal('bash_code_execution_result'), content: z.array( z.object({ type: z.literal('bash_code_execution_output'), - fileId: z.string(), + file_id: z.string(), }), ), stdout: z.string(), stderr: z.string(), - returnCode: z.number(), + return_code: z.number(), }), z.object({ type: z.literal('bash_code_execution_tool_result_error'), - errorCode: z.string(), + error_code: z.string(), }), z.object({ type: z.literal('text_editor_code_execution_tool_result_error'), - errorCode: z.string(), + error_code: z.string(), }), z.object({ type: z.literal('text_editor_code_execution_view_result'), content: z.string(), - fileType: z.string(), - numLines: z.number().nullable(), - startLine: z.number().nullable(), - totalLines: z.number().nullable(), + file_type: z.string(), + num_lines: z.number().nullable(), + start_line: z.number().nullable(), + total_lines: z.number().nullable(), }), z.object({ type: z.literal('text_editor_code_execution_create_result'), - isFileUpdate: z.boolean(), + is_file_update: z.boolean(), }), z.object({ type: z.literal('text_editor_code_execution_str_replace_result'), lines: z.array(z.string()).nullable(), - newLines: z.number().nullable(), - newStart: z.number().nullable(), - oldLines: z.number().nullable(), - oldStart: z.number().nullable(), + new_lines: z.number().nullable(), + new_start: z.number().nullable(), + old_lines: z.number().nullable(), + old_start: z.number().nullable(), }), ]), ), @@ -97,7 +97,7 @@ const factory = createProviderDefinedToolFactoryWithOutputSchema< file_text?: string | null; }, | { - type: 'bash_code_execution_tool_result'; + type: 'bash_code_execution_result'; /** * Output from successful execution @@ -112,7 +112,7 @@ const factory = createProviderDefinedToolFactoryWithOutputSchema< /** * 0 for success, non-zero for failure */ - returnCode: number; + return_code: number; } | { type: 'bash_code_execution_tool_result_error'; @@ -121,7 +121,7 @@ const factory = createProviderDefinedToolFactoryWithOutputSchema< * Available options: invalid_tool_input, unavailable, too_many_requests, * execution_time_exceeded, output_file_too_large. */ - errorCode: string; + error_code: string; } | { type: 'text_editor_code_execution_tool_result_error'; @@ -130,7 +130,7 @@ const factory = createProviderDefinedToolFactoryWithOutputSchema< * Available options: invalid_tool_input, unavailable, too_many_requests, * execution_time_exceeded, file_not_found. */ - errorCode: string; + error_code: string; } | { type: 'text_editor_code_execution_view_result'; @@ -140,25 +140,25 @@ const factory = createProviderDefinedToolFactoryWithOutputSchema< /** * The type of the file. Available options: text, image, pdf. */ - fileType: string; + file_type: string; - numLines: number | null; - startLine: number | null; - totalLines: number | null; + num_lines: number | null; + start_line: number | null; + total_lines: number | null; } | { type: 'text_editor_code_execution_create_result'; - isFileUpdate: boolean; + is_file_update: boolean; } | { type: 'text_editor_code_execution_str_replace_result'; lines: string[] | null; - newLines: number | null; - newStart: number | null; - oldLines: number | null; - oldStart: number | null; + new_lines: number | null; + new_start: number | null; + old_lines: number | null; + old_start: number | null; }, { // no arguments From 5ea0d0c77811239b7bb34b488346740b45554ad3 Mon Sep 17 00:00:00 2001 From: Lars Grammel Date: Tue, 14 Oct 2025 13:00:28 +0200 Subject: [PATCH 28/36] example4 --- .../agent/anthropic-code-execution.ts | 13 +++++ .../chat-anthropic-code-execution/route.ts | 12 ++++ .../chat-anthropic-code-execution/page.tsx | 56 +++++++++++++++++++ .../tool/anthropic-code-execution-view.tsx | 34 +++++++++++ 4 files changed, 115 insertions(+) create mode 100644 examples/next-openai/agent/anthropic-code-execution.ts create mode 100644 examples/next-openai/app/api/chat-anthropic-code-execution/route.ts create mode 100644 examples/next-openai/app/chat-anthropic-code-execution/page.tsx create mode 100644 examples/next-openai/components/tool/anthropic-code-execution-view.tsx diff --git a/examples/next-openai/agent/anthropic-code-execution.ts b/examples/next-openai/agent/anthropic-code-execution.ts new file mode 100644 index 000000000000..454b231e59ad --- /dev/null +++ b/examples/next-openai/agent/anthropic-code-execution.ts @@ -0,0 +1,13 @@ +import { anthropic } from '@ai-sdk/anthropic'; +import { BasicAgent, InferAgentUIMessage } from 'ai'; + +export const anthropicCodeExecutionAgent = new BasicAgent({ + model: anthropic('claude-sonnet-4-5'), + tools: { + code_execution: anthropic.tools.codeExecution_20250825(), + }, +}); + +export type AnthropicCodeExecutionMessage = InferAgentUIMessage< + typeof anthropicCodeExecutionAgent +>; diff --git a/examples/next-openai/app/api/chat-anthropic-code-execution/route.ts b/examples/next-openai/app/api/chat-anthropic-code-execution/route.ts new file mode 100644 index 000000000000..f561f1f8337b --- /dev/null +++ b/examples/next-openai/app/api/chat-anthropic-code-execution/route.ts @@ -0,0 +1,12 @@ +import { anthropicCodeExecutionAgent } from '@/agent/anthropic-code-execution'; +import { validateUIMessages } from 'ai'; + +export async function POST(request: Request) { + const body = await request.json(); + + console.dir(body.messages, { depth: Infinity }); + + return anthropicCodeExecutionAgent.respond({ + messages: await validateUIMessages({ messages: body.messages }), + }); +} diff --git a/examples/next-openai/app/chat-anthropic-code-execution/page.tsx b/examples/next-openai/app/chat-anthropic-code-execution/page.tsx new file mode 100644 index 000000000000..0acf94aeddd3 --- /dev/null +++ b/examples/next-openai/app/chat-anthropic-code-execution/page.tsx @@ -0,0 +1,56 @@ +'use client'; + +import { AnthropicCodeExecutionMessage } from '@/agent/anthropic-code-execution'; +import { Response } from '@/components/ai-elements/response'; +import ChatInput from '@/components/chat-input'; +import AnthropicCodeExecutionView from '@/components/tool/anthropic-code-execution-view'; +import { useChat } from '@ai-sdk/react'; +import { DefaultChatTransport } from 'ai'; + +export default function TestAnthropicCodeExecution() { + const { error, status, sendMessage, messages, regenerate } = + useChat({ + transport: new DefaultChatTransport({ + api: '/api/chat-anthropic-code-execution', + }), + }); + + return ( +
+

OpenAI Web Search Test

+ + {messages.map(message => ( +
+ {message.role === 'user' ? 'User: ' : 'AI: '} + {message.parts.map((part, index) => { + switch (part.type) { + case 'text': { + return {part.text}; + } + case 'tool-code_execution': { + return ( + + ); + } + } + })} +
+ ))} + + {error && ( +
+
An error occurred.
+ +
+ )} + + sendMessage({ text })} /> +
+ ); +} diff --git a/examples/next-openai/components/tool/anthropic-code-execution-view.tsx b/examples/next-openai/components/tool/anthropic-code-execution-view.tsx new file mode 100644 index 000000000000..eeb3660aabb5 --- /dev/null +++ b/examples/next-openai/components/tool/anthropic-code-execution-view.tsx @@ -0,0 +1,34 @@ +import { anthropic } from '@ai-sdk/anthropic'; +import { UIToolInvocation } from 'ai'; + +export default function AnthropicCodeExecutionView({ + invocation, +}: { + invocation: UIToolInvocation< + ReturnType + >; +}) { + switch (invocation.state) { + case 'input-streaming': + case 'input-available': + return ( +
+ Executing the following: +
{JSON.stringify(invocation.input, null, 2)}
+
+ ); + case 'output-available': + return ( + <> +
+ Executing the following: +
{JSON.stringify(invocation.input, null, 2)}
+
+
+ Output: +
{JSON.stringify(invocation.output, null, 2)}
+
+ + ); + } +} From 35613621cc93809b1ea2870451a5fb200daff304 Mon Sep 17 00:00:00 2001 From: Lars Grammel Date: Tue, 14 Oct 2025 13:30:28 +0200 Subject: [PATCH 29/36] example --- .../chat-anthropic-code-execution/page.tsx | 2 +- .../tool/anthropic-code-execution-view.tsx | 119 +++++++++++++++--- 2 files changed, 106 insertions(+), 15 deletions(-) diff --git a/examples/next-openai/app/chat-anthropic-code-execution/page.tsx b/examples/next-openai/app/chat-anthropic-code-execution/page.tsx index 0acf94aeddd3..02aff58ecab9 100644 --- a/examples/next-openai/app/chat-anthropic-code-execution/page.tsx +++ b/examples/next-openai/app/chat-anthropic-code-execution/page.tsx @@ -17,7 +17,7 @@ export default function TestAnthropicCodeExecution() { return (
-

OpenAI Web Search Test

+

Anthropic Code Execution Test

{messages.map(message => (
diff --git a/examples/next-openai/components/tool/anthropic-code-execution-view.tsx b/examples/next-openai/components/tool/anthropic-code-execution-view.tsx index eeb3660aabb5..d4102efef48d 100644 --- a/examples/next-openai/components/tool/anthropic-code-execution-view.tsx +++ b/examples/next-openai/components/tool/anthropic-code-execution-view.tsx @@ -10,25 +10,116 @@ export default function AnthropicCodeExecutionView({ }) { switch (invocation.state) { case 'input-streaming': - case 'input-available': - return ( -
- Executing the following: -
{JSON.stringify(invocation.input, null, 2)}
-
- ); + case 'input-available': { + return ; + } case 'output-available': + console.log(invocation.output); return ( <> -
- Executing the following: -
{JSON.stringify(invocation.input, null, 2)}
-
-
- Output: -
{JSON.stringify(invocation.output, null, 2)}
+ + +
+
+              {invocation.output.type === 'bash_code_execution_result' && (
+                <>
+                  Stdout:
+                  
+ {invocation.output.stdout} +
+ {invocation.output.stderr && ( + <> + Stderr: +
+ {invocation.output.stderr} +
+ + )} + {invocation.output.return_code != null && ( + <> + Return Code: +
+ {invocation.output.return_code} +
+ + )} + + )} + {invocation.output.type === + 'text_editor_code_execution_create_result' && ( + <> + File Create Result +
+ Is File Update:{' '} + {invocation.output.is_file_update ? 'Yes' : 'No'} +
+ + )} +
); } } + +function InputView({ + input, +}: { + input: UIToolInvocation< + ReturnType + >['input']; +}) { + switch (input?.type) { + case 'text_editor_code_execution': { + const command = input.command; + const path = input.path; + const file_text = input.file_text; + + return ( +
+
+            Text Editor
+            
+ {command && ( + <> + Command: {command} +
+ + )} + {path && ( + <> + File Path: {path} +
+ + )} + {file_text && ( + <> + File Text: {file_text} +
+ + )} +
+
+ ); + } + + case 'bash_code_execution': { + const command = input.command; + + return ( +
+
+            Bash
+            
+ {command && ( + <> + Command: {command} +
+ + )} +
+
+ ); + } + } +} From 7f94b32f2c5f8ed8cf6fe77a35eb368b65e4ef79 Mon Sep 17 00:00:00 2001 From: Lars Grammel Date: Tue, 14 Oct 2025 13:32:42 +0200 Subject: [PATCH 30/36] clean --- .../components/tool/anthropic-code-execution-view.tsx | 1 - 1 file changed, 1 deletion(-) diff --git a/examples/next-openai/components/tool/anthropic-code-execution-view.tsx b/examples/next-openai/components/tool/anthropic-code-execution-view.tsx index d4102efef48d..0e182fe79aca 100644 --- a/examples/next-openai/components/tool/anthropic-code-execution-view.tsx +++ b/examples/next-openai/components/tool/anthropic-code-execution-view.tsx @@ -14,7 +14,6 @@ export default function AnthropicCodeExecutionView({ return ; } case 'output-available': - console.log(invocation.output); return ( <> From 6c9275f447e2649ac4466ceb047522bca75f32c2 Mon Sep 17 00:00:00 2001 From: Lars Grammel Date: Tue, 14 Oct 2025 15:10:20 +0200 Subject: [PATCH 31/36] extract --- ...nvert-to-anthropic-messages-prompt.test.ts | 72 ++++++++++--------- .../convert-to-anthropic-messages-prompt.ts | 53 ++++++++++---- 2 files changed, 75 insertions(+), 50 deletions(-) diff --git a/packages/anthropic/src/convert-to-anthropic-messages-prompt.test.ts b/packages/anthropic/src/convert-to-anthropic-messages-prompt.test.ts index 0e5a4a1d17d7..7141cdf361d0 100644 --- a/packages/anthropic/src/convert-to-anthropic-messages-prompt.test.ts +++ b/packages/anthropic/src/convert-to-anthropic-messages-prompt.test.ts @@ -1086,44 +1086,45 @@ describe('assistant messages', () => { expect(warnings).toMatchInlineSnapshot(`[]`); }); - it('should convert anthropic code_execution tool call and result parts', async () => { - const warnings: LanguageModelV3CallWarning[] = []; - const result = await convertToAnthropicMessagesPrompt({ - prompt: [ - { - role: 'assistant', - content: [ - { - input: { - code: 'print("Hello, world!")', + describe('code_execution 20250522', () => { + it('should convert anthropic code_execution tool call and result parts', async () => { + const warnings: LanguageModelV3CallWarning[] = []; + const result = await convertToAnthropicMessagesPrompt({ + prompt: [ + { + role: 'assistant', + content: [ + { + input: { + code: 'print("Hello, world!")', + }, + providerExecuted: true, + toolCallId: 'srvtoolu_01XyZ1234567890', + toolName: 'code_execution', + type: 'tool-call', }, - providerExecuted: true, - toolCallId: 'srvtoolu_01XyZ1234567890', - toolName: 'code_execution', - type: 'tool-call', - }, - { - output: { - type: 'json', - value: { - type: 'code_execution_result', - stdout: 'Hello, world!', - stderr: '', - return_code: 0, + { + output: { + type: 'json', + value: { + type: 'code_execution_result', + stdout: 'Hello, world!', + stderr: '', + return_code: 0, + }, }, + toolCallId: 'srvtoolu_01XyZ1234567890', + toolName: 'code_execution', + type: 'tool-result', }, - toolCallId: 'srvtoolu_01XyZ1234567890', - toolName: 'code_execution', - type: 'tool-result', - }, - ], - }, - ], - sendReasoning: false, - warnings, - }); + ], + }, + ], + sendReasoning: false, + warnings, + }); - expect(result).toMatchInlineSnapshot(` + expect(result).toMatchInlineSnapshot(` { "betas": Set {}, "prompt": { @@ -1158,7 +1159,8 @@ describe('assistant messages', () => { }, } `); - expect(warnings).toMatchInlineSnapshot(`[]`); + expect(warnings).toMatchInlineSnapshot(`[]`); + }); }); }); diff --git a/packages/anthropic/src/convert-to-anthropic-messages-prompt.ts b/packages/anthropic/src/convert-to-anthropic-messages-prompt.ts index 3cc795b08e19..e515f8303c9a 100644 --- a/packages/anthropic/src/convert-to-anthropic-messages-prompt.ts +++ b/packages/anthropic/src/convert-to-anthropic-messages-prompt.ts @@ -470,23 +470,46 @@ export async function convertToAnthropicMessagesPrompt({ break; } - const codeExecutionOutput = await validateTypes({ - value: output.value, - schema: codeExecution_20250522OutputSchema, - }); + if ( + output.value == null || + typeof output.value !== 'object' || + !('type' in output.value) || + typeof output.value.type !== 'string' + ) { + warnings.push({ + type: 'other', + message: `provider executed tool result output value is not a valid code execution result for tool ${part.toolName}`, + }); + break; + } - anthropicContent.push({ - type: 'code_execution_tool_result', - tool_use_id: part.toolCallId, - content: { - type: codeExecutionOutput.type, - stdout: codeExecutionOutput.stdout, - stderr: codeExecutionOutput.stderr, - return_code: codeExecutionOutput.return_code, - }, - cache_control: cacheControl, - }); + // to distinguish between code execution 20250522 and 20250825, + // we check if a type property is present in the output.value + if (output.value.type === 'code_execution_result') { + // code execution 20250522 + const codeExecutionOutput = await validateTypes({ + value: output.value, + schema: codeExecution_20250522OutputSchema, + }); + anthropicContent.push({ + type: 'code_execution_tool_result', + tool_use_id: part.toolCallId, + content: { + type: codeExecutionOutput.type, + stdout: codeExecutionOutput.stdout, + stderr: codeExecutionOutput.stderr, + return_code: codeExecutionOutput.return_code, + }, + cache_control: cacheControl, + }); + } else { + // code execution 20250825 + const codeExecutionOutput = await validateTypes({ + value: output.value, + schema: codeExecution_20250825OutputSchema, + }); + } break; } From 50a53074ae869d9ee565da0545fd97e4acf0fd2b Mon Sep 17 00:00:00 2001 From: Lars Grammel Date: Tue, 14 Oct 2025 15:22:36 +0200 Subject: [PATCH 32/36] output mapping --- .../anthropic/src/anthropic-messages-api.ts | 2 + ...nvert-to-anthropic-messages-prompt.test.ts | 127 ++++++++++++++++++ .../convert-to-anthropic-messages-prompt.ts | 21 +++ 3 files changed, 150 insertions(+) diff --git a/packages/anthropic/src/anthropic-messages-api.ts b/packages/anthropic/src/anthropic-messages-api.ts index fdf3dc5b90ec..d110ef269589 100644 --- a/packages/anthropic/src/anthropic-messages-api.ts +++ b/packages/anthropic/src/anthropic-messages-api.ts @@ -34,6 +34,8 @@ export interface AnthropicAssistantMessage { | AnthropicCodeExecutionToolResultContent | AnthropicWebFetchToolResultContent | AnthropicWebSearchToolResultContent + | AnthropicBashCodeExecutionToolResultContent + | AnthropicTextEditorCodeExecutionToolResultContent >; } diff --git a/packages/anthropic/src/convert-to-anthropic-messages-prompt.test.ts b/packages/anthropic/src/convert-to-anthropic-messages-prompt.test.ts index 7141cdf361d0..affad42c89ea 100644 --- a/packages/anthropic/src/convert-to-anthropic-messages-prompt.test.ts +++ b/packages/anthropic/src/convert-to-anthropic-messages-prompt.test.ts @@ -1162,6 +1162,133 @@ describe('assistant messages', () => { expect(warnings).toMatchInlineSnapshot(`[]`); }); }); + + describe('code_execution 20250825', () => { + it('should convert anthropic code_execution tool call and result parts', async () => { + const warnings: LanguageModelV3CallWarning[] = []; + const result = await convertToAnthropicMessagesPrompt({ + prompt: [ + { + role: 'assistant', + content: [ + { + type: 'tool-call', + toolCallId: 'srvtoolu_01Hq9rR6fZwwDGHkTYRafn7k', + toolName: 'code_execution', + input: { + type: 'text_editor_code_execution', + command: 'create', + path: '/tmp/fibonacci.py', + file_text: 'def..', + }, + providerExecuted: true, + }, + { + type: 'tool-result', + toolCallId: 'srvtoolu_01Hq9rR6fZwwDGHkTYRafn7k', + toolName: 'code_execution', + output: { + type: 'json', + value: { + type: 'text_editor_code_execution_create_result', + is_file_update: false, + }, + }, + }, + { + type: 'tool-call', + toolCallId: 'srvtoolu_0193G3ttnkiTfZASwHQSKc2V', + toolName: 'code_execution', + input: { + type: 'bash_code_execution', + command: 'python /tmp/fibonacci.py', + }, + providerExecuted: true, + }, + { + type: 'tool-result', + toolCallId: 'srvtoolu_0193G3ttnkiTfZASwHQSKc2V', + toolName: 'code_execution', + output: { + type: 'json', + value: { + type: 'bash_code_execution_result', + content: [], + stdout: 'The 10th Fibonacci number is: 34\n', + stderr: '', + return_code: 0, + }, + }, + }, + ], + }, + ], + sendReasoning: false, + warnings, + }); + + expect(result).toMatchInlineSnapshot(` + { + "betas": Set {}, + "prompt": { + "messages": [ + { + "content": [ + { + "cache_control": undefined, + "id": "srvtoolu_01Hq9rR6fZwwDGHkTYRafn7k", + "input": { + "command": "create", + "file_text": "def..", + "path": "/tmp/fibonacci.py", + "type": "text_editor_code_execution", + }, + "name": "code_execution", + "type": "server_tool_use", + }, + { + "cache_control": undefined, + "content": { + "is_file_update": false, + "type": "text_editor_code_execution_create_result", + }, + "tool_use_id": "srvtoolu_01Hq9rR6fZwwDGHkTYRafn7k", + "type": "text_editor_code_execution_tool_result", + }, + { + "cache_control": undefined, + "id": "srvtoolu_0193G3ttnkiTfZASwHQSKc2V", + "input": { + "command": "python /tmp/fibonacci.py", + "type": "bash_code_execution", + }, + "name": "code_execution", + "type": "server_tool_use", + }, + { + "cache_control": undefined, + "content": { + "content": [], + "return_code": 0, + "stderr": "", + "stdout": "The 10th Fibonacci number is: 34 + ", + "type": "bash_code_execution_result", + }, + "tool_use_id": "srvtoolu_0193G3ttnkiTfZASwHQSKc2V", + "type": "bash_code_execution_tool_result", + }, + ], + "role": "assistant", + }, + ], + "system": undefined, + }, + } + `); + expect(warnings).toMatchInlineSnapshot(`[]`); + }); + }); }); describe('cache control', () => { diff --git a/packages/anthropic/src/convert-to-anthropic-messages-prompt.ts b/packages/anthropic/src/convert-to-anthropic-messages-prompt.ts index e515f8303c9a..e602c01da039 100644 --- a/packages/anthropic/src/convert-to-anthropic-messages-prompt.ts +++ b/packages/anthropic/src/convert-to-anthropic-messages-prompt.ts @@ -430,6 +430,8 @@ export async function convertToAnthropicMessagesPrompt({ part.toolName === 'web_fetch' || part.toolName === 'web_search' ) { + // TODO invert mapping for code execution 20250825 + anthropicContent.push({ type: 'server_tool_use', id: part.toolCallId, @@ -509,6 +511,25 @@ export async function convertToAnthropicMessagesPrompt({ value: output.value, schema: codeExecution_20250825OutputSchema, }); + + anthropicContent.push( + codeExecutionOutput.type === + 'bash_code_execution_result' || + codeExecutionOutput.type === + 'bash_code_execution_tool_result_error' + ? { + type: 'bash_code_execution_tool_result', + tool_use_id: part.toolCallId, + cache_control: cacheControl, + content: codeExecutionOutput, + } + : { + type: 'text_editor_code_execution_tool_result', + tool_use_id: part.toolCallId, + cache_control: cacheControl, + content: codeExecutionOutput, + }, + ); } break; } From 4b843bb13c0370e99d9590548f1c64aa0d0bdb4a Mon Sep 17 00:00:00 2001 From: Lars Grammel Date: Tue, 14 Oct 2025 15:28:13 +0200 Subject: [PATCH 33/36] map inputs back --- .../anthropic/src/anthropic-messages-api.ts | 9 +++++++- ...nvert-to-anthropic-messages-prompt.test.ts | 4 ++-- .../convert-to-anthropic-messages-prompt.ts | 21 ++++++++++++++++--- 3 files changed, 28 insertions(+), 6 deletions(-) diff --git a/packages/anthropic/src/anthropic-messages-api.ts b/packages/anthropic/src/anthropic-messages-api.ts index d110ef269589..d2b13798eb34 100644 --- a/packages/anthropic/src/anthropic-messages-api.ts +++ b/packages/anthropic/src/anthropic-messages-api.ts @@ -100,7 +100,14 @@ export interface AnthropicToolCallContent { export interface AnthropicServerToolUseContent { type: 'server_tool_use'; id: string; - name: 'code_execution' | 'web_fetch' | 'web_search'; + name: + | 'web_fetch' + | 'web_search' + // code execution 20250522: + | 'code_execution' + // code execution 20250825: + | 'bash_code_execution' + | 'text_editor_code_execution'; input: unknown; cache_control: AnthropicCacheControl | undefined; } diff --git a/packages/anthropic/src/convert-to-anthropic-messages-prompt.test.ts b/packages/anthropic/src/convert-to-anthropic-messages-prompt.test.ts index affad42c89ea..e612d102efb1 100644 --- a/packages/anthropic/src/convert-to-anthropic-messages-prompt.test.ts +++ b/packages/anthropic/src/convert-to-anthropic-messages-prompt.test.ts @@ -1243,7 +1243,7 @@ describe('assistant messages', () => { "path": "/tmp/fibonacci.py", "type": "text_editor_code_execution", }, - "name": "code_execution", + "name": "text_editor_code_execution", "type": "server_tool_use", }, { @@ -1262,7 +1262,7 @@ describe('assistant messages', () => { "command": "python /tmp/fibonacci.py", "type": "bash_code_execution", }, - "name": "code_execution", + "name": "bash_code_execution", "type": "server_tool_use", }, { diff --git a/packages/anthropic/src/convert-to-anthropic-messages-prompt.ts b/packages/anthropic/src/convert-to-anthropic-messages-prompt.ts index e602c01da039..4b4c15baddf8 100644 --- a/packages/anthropic/src/convert-to-anthropic-messages-prompt.ts +++ b/packages/anthropic/src/convert-to-anthropic-messages-prompt.ts @@ -425,13 +425,28 @@ export async function convertToAnthropicMessagesPrompt({ case 'tool-call': { if (part.providerExecuted) { + // code execution 20250825: if ( - part.toolName === 'code_execution' || + part.toolName === 'code_execution' && + part.input != null && + typeof part.input === 'object' && + 'type' in part.input && + typeof part.input.type === 'string' && + (part.input.type === 'bash_code_execution' || + part.input.type === 'text_editor_code_execution') + ) { + anthropicContent.push({ + type: 'server_tool_use', + id: part.toolCallId, + name: part.input.type, // map back to subtool name + input: part.input, + cache_control: cacheControl, + }); + } else if ( + part.toolName === 'code_execution' || // code execution 20250522 part.toolName === 'web_fetch' || part.toolName === 'web_search' ) { - // TODO invert mapping for code execution 20250825 - anthropicContent.push({ type: 'server_tool_use', id: part.toolCallId, From 722e6425102531da8df435c6e97cbc7b3173054d Mon Sep 17 00:00:00 2001 From: Lars Grammel Date: Tue, 14 Oct 2025 15:38:24 +0200 Subject: [PATCH 34/36] types --- .../src/tool/code-execution_20250825.ts | 54 ++++++++++++++++--- 1 file changed, 46 insertions(+), 8 deletions(-) diff --git a/packages/anthropic/src/tool/code-execution_20250825.ts b/packages/anthropic/src/tool/code-execution_20250825.ts index ffb2f3a5e40a..b4471bbcd153 100644 --- a/packages/anthropic/src/tool/code-execution_20250825.ts +++ b/packages/anthropic/src/tool/code-execution_20250825.ts @@ -59,12 +59,26 @@ export const codeExecution_20250825InputSchema = lazySchema(() => type: z.literal('bash_code_execution'), command: z.string(), }), - z.object({ - type: z.literal('text_editor_code_execution'), - command: z.string(), - path: z.string(), - file_text: z.string().nullish(), - }), + z.discriminatedUnion('command', [ + z.object({ + type: z.literal('text_editor_code_execution'), + command: z.literal('view'), + path: z.string(), + }), + z.object({ + type: z.literal('text_editor_code_execution'), + command: z.literal('create'), + path: z.string(), + file_text: z.string().nullish(), + }), + z.object({ + type: z.literal('text_editor_code_execution'), + command: z.literal('str_replace'), + path: z.string(), + old_str: z.string(), + new_str: z.string(), + }), + ]), ]), ), ); @@ -80,11 +94,16 @@ const factory = createProviderDefinedToolFactoryWithOutputSchema< } | { type: 'text_editor_code_execution'; + command: 'view'; /** - * The command to run. + * The path to the file to view. */ - command: string; + path: string; + } + | { + type: 'text_editor_code_execution'; + command: 'create'; /** * The path to the file to edit. @@ -95,6 +114,25 @@ const factory = createProviderDefinedToolFactoryWithOutputSchema< * The text of the file to edit. */ file_text?: string | null; + } + | { + type: 'text_editor_code_execution'; + command: 'str_replace'; + + /** + * The path to the file to edit. + */ + path: string; + + /** + * The string to replace. + */ + old_str: string; + + /** + * The new string to replace the old string with. + */ + new_str: string; }, | { type: 'bash_code_execution_result'; From 251a3b06fb94f98a7c684e03676166aac87ee7f1 Mon Sep 17 00:00:00 2001 From: Lars Grammel Date: Tue, 14 Oct 2025 15:42:52 +0200 Subject: [PATCH 35/36] view --- .../tool/anthropic-code-execution-view.tsx | 101 ++++++++++++------ 1 file changed, 71 insertions(+), 30 deletions(-) diff --git a/examples/next-openai/components/tool/anthropic-code-execution-view.tsx b/examples/next-openai/components/tool/anthropic-code-execution-view.tsx index 0e182fe79aca..65748e47100e 100644 --- a/examples/next-openai/components/tool/anthropic-code-execution-view.tsx +++ b/examples/next-openai/components/tool/anthropic-code-execution-view.tsx @@ -70,36 +70,77 @@ function InputView({ }) { switch (input?.type) { case 'text_editor_code_execution': { - const command = input.command; - const path = input.path; - const file_text = input.file_text; - - return ( -
-
-            Text Editor
-            
- {command && ( - <> - Command: {command} -
- - )} - {path && ( - <> - File Path: {path} -
- - )} - {file_text && ( - <> - File Text: {file_text} -
- - )} -
-
- ); + switch (input.command) { + case 'view': { + return ( +
+
+                Text Editor (View)
+                {input.path && (
+                  <>
+                    File Path:{' '}
+                    {input.path}
+                    
+ + )} +
+
+ ); + } + case 'create': { + return ( +
+
+                Text Editor (Create)
+                {input.path && (
+                  <>
+                    File Path:{' '}
+                    {input.path}
+                    
+ + )} + {input.file_text && ( + <> + File Text:{' '} + {input.file_text} +
+ + )} +
+
+ ); + } + case 'str_replace': { + return ( +
+
+                Text Editor (Replace)
+                {input.path && (
+                  <>
+                    File Path:{' '}
+                    {input.path}
+                    
+ + )} + {input.old_str && ( + <> + Old String:{' '} + {input.old_str} +
+ + )} + {input.new_str && ( + <> + New String:{' '} + {input.new_str} +
+ + )} +
+
+ ); + } + } } case 'bash_code_execution': { From 442c0c59094d62a51d3611ea8d6d50023ea190de Mon Sep 17 00:00:00 2001 From: Lars Grammel Date: Tue, 14 Oct 2025 15:48:46 +0200 Subject: [PATCH 36/36] view --- .../tool/anthropic-code-execution-view.tsx | 68 ++++++++++++++++++- 1 file changed, 66 insertions(+), 2 deletions(-) diff --git a/examples/next-openai/components/tool/anthropic-code-execution-view.tsx b/examples/next-openai/components/tool/anthropic-code-execution-view.tsx index 65748e47100e..dc53d23361b5 100644 --- a/examples/next-openai/components/tool/anthropic-code-execution-view.tsx +++ b/examples/next-openai/components/tool/anthropic-code-execution-view.tsx @@ -44,6 +44,16 @@ export default function AnthropicCodeExecutionView({ )} )} + {invocation.output.type === + 'bash_code_execution_tool_result_error' && ( + <> + Bash Tool Result Error +
+ Error Code:{' '} + {invocation.output.error_code} +
+ + )} {invocation.output.type === 'text_editor_code_execution_create_result' && ( <> @@ -54,6 +64,55 @@ export default function AnthropicCodeExecutionView({
)} + {invocation.output.type === + 'text_editor_code_execution_view_result' && ( + <> + File View Result +
+ File Type:{' '} + {invocation.output.file_type} +
+ Content: +
+ {invocation.output.content} +
+ + )} + {invocation.output.type === + 'text_editor_code_execution_str_replace_result' && ( + <> + File Str Replace Result +
+ New Start:{' '} + {invocation.output.new_start} +
+ New Lines:{' '} + {invocation.output.new_lines} +
+ Old Start:{' '} + {invocation.output.old_start} +
+ Old Lines:{' '} + {invocation.output.old_lines} +
+ Lines: +
+ {invocation.output.lines?.join('\n')} +
+ + )} + {invocation.output.type === + 'text_editor_code_execution_tool_result_error' && ( + <> + + Text Editor Tool Result Error + +
+ Error Code:{' '} + {invocation.output.error_code} +
+ + )}
@@ -76,6 +135,7 @@ function InputView({
                 Text Editor (View)
+                
{input.path && ( <> File Path:{' '} @@ -92,6 +152,7 @@ function InputView({
                 Text Editor (Create)
+                
{input.path && ( <> File Path:{' '} @@ -115,6 +176,7 @@ function InputView({
                 Text Editor (Replace)
+                
{input.path && ( <> File Path:{' '} @@ -124,14 +186,16 @@ function InputView({ )} {input.old_str && ( <> - Old String:{' '} + Old String: +
{input.old_str}
)} {input.new_str && ( <> - New String:{' '} + New String: +
{input.new_str}