Last active
October 3, 2025 15:08
-
-
Save pokey/a0ecf7d882e2a4e2533163b62227afe6 to your computer and use it in GitHub Desktop.
Anthropic API bug: 500 error when reusing container with bash_code_execution_output in conversation history
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| /** | |
| * Bug Reproduction: 500 error when reusing container with bash_code_execution_output in conversation history | |
| * | |
| * Steps to reproduce: | |
| * 1. Upload a file with container_upload | |
| * 2. Make a request that generates bash_code_execution_output file references | |
| * 3. Try to reuse the container with the full conversation history | |
| * | |
| * Expected: Second request succeeds | |
| * Actual: Second request fails with 500 Internal Server Error | |
| * | |
| * Workaround: Filter out bash_code_execution_output file references from the assistant content | |
| */ | |
| import { Anthropic } from '@anthropic-ai/sdk'; | |
| import { createReadStream } from 'fs'; | |
| const anthropic = new Anthropic(); | |
| async function main() { | |
| console.log("Step 1: Upload file and make first request..."); | |
| // Upload a file | |
| const fileObject = await anthropic.beta.files.upload({ | |
| file: createReadStream("test_data.csv"), | |
| }); | |
| // First request with file upload | |
| const response1 = await anthropic.beta.messages.create({ | |
| model: "claude-sonnet-4-5", | |
| betas: ["code-execution-2025-08-25", "files-api-2025-04-14"], | |
| max_tokens: 4096, | |
| messages: [{ | |
| role: "user", | |
| content: [ | |
| { type: "text", text: "Analyze this CSV and save a summary to analysis.json" }, | |
| { type: "container_upload", file_id: fileObject.id } | |
| ] | |
| }], | |
| tools: [{ | |
| type: "code_execution_20250825", | |
| name: "code_execution" | |
| }] | |
| }); | |
| console.log("✓ First request succeeded"); | |
| const containerId = response1.container!.id; | |
| console.log(`Container ID: ${containerId}`); | |
| // Check if response has bash_code_execution_output file references | |
| const hasFileOutputs = JSON.stringify(response1.content).includes("bash_code_execution_output"); | |
| console.log(`Response has file outputs: ${hasFileOutputs}\n`); | |
| console.log("Step 2: Reuse container with conversation history (will fail)..."); | |
| // BUG: This causes a 500 error when response1.content includes | |
| // bash_code_execution_output file references in tool results | |
| try { | |
| const response2 = await anthropic.beta.messages.create({ | |
| container: containerId, | |
| model: "claude-sonnet-4-5", | |
| betas: ["code-execution-2025-08-25", "files-api-2025-04-14"], | |
| max_tokens: 4096, | |
| messages: [ | |
| { | |
| role: "user", | |
| content: "Analyze this CSV and save a summary to analysis.json", | |
| }, | |
| { | |
| role: "assistant", | |
| content: response1.content, // This causes 500 error | |
| }, | |
| { | |
| role: "user", | |
| content: "What was the total sales?", | |
| } | |
| ], | |
| tools: [{ | |
| type: "code_execution_20250825", | |
| name: "code_execution" | |
| }] | |
| }); | |
| console.log("✓ Second request succeeded"); | |
| } catch (error: any) { | |
| console.error("✗ Second request failed with error:"); | |
| console.error(`Status: ${error.status}`); | |
| console.error(`Message: ${error.message}`); | |
| console.error(`Request ID: ${error.requestID}\n`); | |
| } | |
| console.log("Step 3: Reuse container with filtered conversation history (workaround)..."); | |
| // WORKAROUND: Filter out bash_code_execution_output file references | |
| const filteredContent = response1.content.map((block: any) => { | |
| if (block.type === "bash_code_execution_tool_result" && block.content?.content) { | |
| return { | |
| ...block, | |
| content: { | |
| ...block.content, | |
| content: block.content.content.filter((c: any) => c.type !== "bash_code_execution_output") | |
| } | |
| }; | |
| } | |
| return block; | |
| }); | |
| try { | |
| const response3 = await anthropic.beta.messages.create({ | |
| container: containerId, | |
| model: "claude-sonnet-4-5", | |
| betas: ["code-execution-2025-08-25", "files-api-2025-04-14"], | |
| max_tokens: 4096, | |
| messages: [ | |
| { | |
| role: "user", | |
| content: "Analyze this CSV and save a summary to analysis.json", | |
| }, | |
| { | |
| role: "assistant", | |
| content: filteredContent, // Use filtered content | |
| }, | |
| { | |
| role: "user", | |
| content: "What was the total sales?", | |
| } | |
| ], | |
| tools: [{ | |
| type: "code_execution_20250825", | |
| name: "code_execution" | |
| }] | |
| }); | |
| console.log("✓ Third request succeeded with workaround!"); | |
| } catch (error: any) { | |
| console.error("✗ Third request also failed:"); | |
| console.error(`Status: ${error.status}`); | |
| console.error(`Message: ${error.message}`); | |
| } | |
| } | |
| main().catch(console.error); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| ➜ bun ./containerBugRepro.ts | |
| Step 1: Upload file and make first request... | |
| ✓ First request succeeded | |
| Container ID: container_011CTkUQG4YRQUgVoDxBme5n | |
| Response has file outputs: true | |
| Step 2: Reuse container with conversation history (will fail)... | |
| ✗ Second request failed with error: | |
| Status: 500 | |
| Message: 500 {"type":"error","error":{"type":"api_error","message":"Internal server error"},"request_id":"req_011CTkU | |
| TaLDwSYRp5MeuiJYo"} Request ID: req_011CTkUTaLDwSYRp5MeuiJYo | |
| Step 3: Reuse container with filtered conversation history (workaround)... | |
| ✓ Third request succeeded with workaround! |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| date | region | product | sales | units | |
|---|---|---|---|---|---|
| 2024-01-15 | North | Widget A | 1250.50 | 25 | |
| 2024-01-16 | South | Widget B | 890.25 | 18 | |
| 2024-01-17 | East | Widget A | 1456.75 | 29 | |
| 2024-01-18 | West | Widget C | 2100.00 | 42 | |
| 2024-01-19 | North | Widget B | 765.50 | 15 | |
| 2024-01-20 | South | Widget A | 1890.00 | 38 | |
| 2024-01-21 | East | Widget C | 2340.25 | 47 | |
| 2024-01-22 | West | Widget B | 1123.75 | 22 | |
| 2024-01-23 | North | Widget C | 1567.50 | 31 | |
| 2024-01-24 | South | Widget B | 934.25 | 19 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment