Skip to content

Commit 073e977

Browse files
authored
fix(anthropic): Add execution tool response block types (#9108)
1 parent 775a976 commit 073e977

File tree

2 files changed

+119
-3
lines changed

2 files changed

+119
-3
lines changed

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

Lines changed: 114 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -240,3 +240,117 @@ test("Drop content blocks that we don't know how to handle", async () => {
240240
system: undefined,
241241
});
242242
});
243+
244+
test("Can properly format messages with bash_code_execution_tool_result blocks", async () => {
245+
const messageHistory = [
246+
new AIMessage({
247+
content: [
248+
{
249+
type: "server_tool_use",
250+
id: "bash_call",
251+
name: "bash_code_execution",
252+
input: { command: "echo 'hello'" },
253+
},
254+
{
255+
type: "bash_code_execution_tool_result",
256+
tool_use_id: "bash_call",
257+
content: {
258+
type: "bash_code_execution_result",
259+
stdout: "hello\n",
260+
stderr: "",
261+
return_code: 0,
262+
content: [],
263+
},
264+
},
265+
],
266+
}),
267+
];
268+
269+
const formattedMessages = _convertMessagesToAnthropicPayload(messageHistory);
270+
271+
expect(formattedMessages).toEqual({
272+
messages: [
273+
{
274+
role: "assistant",
275+
content: [
276+
{
277+
type: "server_tool_use",
278+
id: "bash_call",
279+
name: "bash_code_execution",
280+
input: { command: "echo 'hello'" },
281+
},
282+
{
283+
type: "bash_code_execution_tool_result",
284+
tool_use_id: "bash_call",
285+
content: {
286+
type: "bash_code_execution_result",
287+
stdout: "hello\n",
288+
stderr: "",
289+
return_code: 0,
290+
content: [],
291+
},
292+
},
293+
],
294+
},
295+
],
296+
system: undefined,
297+
});
298+
});
299+
300+
test("Can properly format messages with text_editor_code_execution_tool_result blocks", async () => {
301+
const messageHistory = [
302+
new AIMessage({
303+
content: [
304+
{
305+
type: "server_tool_use",
306+
id: "editor_call",
307+
name: "text_editor_code_execution",
308+
input: { command: "view", path: "/tmp/test.txt" },
309+
},
310+
{
311+
type: "text_editor_code_execution_tool_result",
312+
tool_use_id: "editor_call",
313+
content: {
314+
type: "text_editor_code_execution_view_result",
315+
file_type: "text",
316+
content: "file contents here",
317+
num_lines: 1,
318+
start_line: 1,
319+
total_lines: 1,
320+
},
321+
},
322+
],
323+
}),
324+
];
325+
326+
const formattedMessages = _convertMessagesToAnthropicPayload(messageHistory);
327+
328+
expect(formattedMessages).toEqual({
329+
messages: [
330+
{
331+
role: "assistant",
332+
content: [
333+
{
334+
type: "server_tool_use",
335+
id: "editor_call",
336+
name: "text_editor_code_execution",
337+
input: { command: "view", path: "/tmp/test.txt" },
338+
},
339+
{
340+
type: "text_editor_code_execution_tool_result",
341+
tool_use_id: "editor_call",
342+
content: {
343+
type: "text_editor_code_execution_view_result",
344+
file_type: "text",
345+
content: "file contents here",
346+
num_lines: 1,
347+
start_line: 1,
348+
total_lines: 1,
349+
},
350+
},
351+
],
352+
},
353+
],
354+
system: undefined,
355+
});
356+
});

libs/providers/langchain-anthropic/src/utils/message_inputs.ts

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -155,12 +155,14 @@ function* _formatContentBlocks(
155155
content: ContentBlock[]
156156
): Generator<Anthropic.Beta.BetaContentBlockParam> {
157157
const toolTypes = [
158-
"tool_use",
159-
"tool_result",
158+
"bash_code_execution_tool_result",
160159
"input_json_delta",
161160
"server_tool_use",
162-
"web_search_tool_result",
161+
"text_editor_code_execution_tool_result",
162+
"tool_result",
163+
"tool_use",
163164
"web_search_result",
165+
"web_search_tool_result",
164166
];
165167
const textTypes = ["text", "text_delta"];
166168
for (const contentPart of content) {

0 commit comments

Comments
 (0)