-
Notifications
You must be signed in to change notification settings - Fork 2.9k
fix(anthropic): add support for container upload block type in message formatting #9097
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
christian-bromann
merged 8 commits into
langchain-ai:v1
from
pokey:pokey/fixanthropic-add-support-for-container-upload-block-type-in-message-formatting
Oct 7, 2025
Merged
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
2205c1a
fix(anthropic): add support for container upload block type in messag…
pokey 33b75f8
Remove warning on unexpected content blocks
pokey 00c7d07
Add integration test
pokey 1cf9951
Merge branch 'v1' into pokey/fixanthropic-add-support-for-container-u…
pokey dff2ca3
Re-add accidentally dropped test
pokey cc50521
Create tiny-falcons-joke.md
hntrl 25d6316
Merge branch 'v1' into pokey/fixanthropic-add-support-for-container-u…
christian-bromann 834e5a0
Merge branch 'v1' into pokey/fixanthropic-add-support-for-container-u…
pokey File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,5 @@ | ||
| --- | ||
| "@langchain/anthropic": patch | ||
| --- | ||
|
|
||
| fix(anthropic): add support for container upload block type in message formatting |
111 changes: 111 additions & 0 deletions
111
libs/providers/langchain-anthropic/src/tests/chat_models-code_execution.int.test.ts
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,111 @@ | ||
| import { describe, it, expect } from "vitest"; | ||
| import Anthropic from "@anthropic-ai/sdk"; | ||
| import { HumanMessage, AIMessage } from "@langchain/core/messages"; | ||
| import { ChatAnthropic } from "../chat_models.js"; | ||
| import fs from "fs"; | ||
| import path from "path"; | ||
| import os from "os"; | ||
|
|
||
| describe("Files API with Code Execution", () => { | ||
| it("should handle createMessageWithFiles using container_upload blocks", async () => { | ||
| // Create Anthropic client for Files API | ||
| const anthropicClient = new Anthropic({ | ||
| apiKey: process.env.ANTHROPIC_API_KEY, | ||
| }); | ||
|
|
||
| // Create a temporary CSV file | ||
| const csvContent = | ||
| "name,age,city\nAlice,30,NYC\nBob,25,LA\nCharlie,35,Chicago"; | ||
| const tmpDir = os.tmpdir(); | ||
| const tmpFilePath = path.join(tmpDir, "test_data.csv"); | ||
| fs.writeFileSync(tmpFilePath, csvContent); | ||
|
|
||
| try { | ||
| // Upload file using Anthropic Files API | ||
| const fileUpload = await anthropicClient.beta.files.upload({ | ||
| file: fs.createReadStream(tmpFilePath), | ||
| }); | ||
|
|
||
| // Create ChatAnthropic model with code execution beta header | ||
| const model = new ChatAnthropic({ | ||
| model: "claude-3-5-haiku-20241022", | ||
| temperature: 0, | ||
| clientOptions: { | ||
| defaultHeaders: { | ||
| "anthropic-beta": "code-execution-2025-08-25,files-api-2025-04-14", | ||
| }, | ||
| }, | ||
| }); | ||
|
|
||
| // Define the built-in code_execution tool | ||
| const codeExecutionTool = { | ||
| type: "code_execution_20250825" as const, | ||
| name: "code_execution", | ||
| }; | ||
|
|
||
| // Create a message with container_upload block | ||
| const message = new HumanMessage({ | ||
| content: [ | ||
| { | ||
| type: "text", | ||
| text: "Analyze this CSV data and tell me the average age", | ||
| }, | ||
| { type: "container_upload", file_id: fileUpload.id }, | ||
| ], | ||
| }); | ||
|
|
||
| // Invoke the model with the file | ||
| const result = await model.invoke([message], { | ||
| tools: [codeExecutionTool], | ||
| }); | ||
|
|
||
| // Verify the result is an AIMessage | ||
| expect(result).toBeInstanceOf(AIMessage); | ||
|
|
||
| // The response should contain content blocks | ||
| const content = Array.isArray(result.content) ? result.content : []; | ||
| expect(content.length).toBeGreaterThan(0); | ||
|
|
||
| // Verify that code_execution tool was used (server_tool_use block) | ||
| expect( | ||
| content.some( | ||
| (block) => | ||
| typeof block === "object" && | ||
| "type" in block && | ||
| block.type === "server_tool_use" | ||
| ) | ||
| ).toBe(true); | ||
|
|
||
| // Verify that we got a code execution result | ||
| expect( | ||
| content.some( | ||
| (block) => | ||
| typeof block === "object" && | ||
| "type" in block && | ||
| block.type === "bash_code_execution_tool_result" | ||
| ) | ||
| ).toBe(true); | ||
|
|
||
| // The response should contain text with the calculated average | ||
| const textBlocks = content.filter( | ||
| (block) => | ||
| typeof block === "object" && "type" in block && block.type === "text" | ||
| ); | ||
| const responseText = textBlocks | ||
| .map((block) => | ||
| typeof block === "object" && "text" in block ? block.text : "" | ||
| ) | ||
| .join(" ") | ||
| .toLowerCase(); | ||
|
|
||
| // The response should mention the average age (30) | ||
| expect(responseText).toMatch(/average|mean/i); | ||
| expect(responseText).toMatch(/30/); | ||
| } finally { | ||
| // Clean up temporary file | ||
| if (fs.existsSync(tmpFilePath)) { | ||
| fs.unlinkSync(tmpFilePath); | ||
| } | ||
| } | ||
| }, 60000); | ||
| }); |
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
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
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
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.