[GH-ISSUE #239] Bug: returnCommandWithStateUpdate fails when lastMessage.content is an array with tool_use blocks #235

Closed
opened 2026-06-05 17:21:11 -04:00 by yindo · 1 comment
Owner

Originally created by @matsumoto-hisa on GitHub (Feb 19, 2026).
Original GitHub issue: https://github.com/langchain-ai/deepagentsjs/issues/239

Description

When returnCommandWithStateUpdate constructs a ToolMessage, it passes lastMessage.content directly. However, when using Anthropic models, content can be an array containing tool_use blocks. Passing these blocks as ToolMessage.content causes errors downstream because tool_use blocks are not valid content for a ToolMessage.

Current Behavior

// src/subagent.ts - returnCommandWithStateUpdate
const lastMessage = messages?.[messages.length - 1];
return new Command({ update: {
    ...stateUpdate,
    messages: [new ToolMessage({
        content: lastMessage?.content || "Task completed",  // ← array with tool_use blocks passed as-is
        tool_call_id: toolCallId,
        name: "task"
    })]
} });

When lastMessage.content is an array like:

[
  { "type": "text", "text": "Here is the result..." },
  { "type": "tool_use", "id": "...", "name": "...", "input": {...} }
]

The tool_use blocks get passed into the ToolMessage, which is invalid.

Expected Behavior

tool_use blocks should be filtered out from the content array before constructing the ToolMessage:

let content = lastMessage?.content || "Task completed";
if (Array.isArray(content)) {
    content = content.filter((block) => block.type !== "tool_use");
    if (content.length === 0) content = "Task completed";
}
return new Command({ update: {
    ...stateUpdate,
    messages: [new ToolMessage({
        content,
        tool_call_id: toolCallId,
        name: "task"
    })]
} });

Reproduction

  1. Create a supervisor with subagents using createSubAgentMiddleware
  2. Use an Anthropic model (e.g., Claude) that returns array-format content with tool_use blocks
  3. The subagent's final message content gets passed as-is to ToolMessage, causing errors

Environment

  • deepagents: 1.8.0
  • @langchain/anthropic: 1.3.3
  • @langchain/core: 1.1.25

Workaround

We are currently using patch-package to apply this fix locally.

Originally created by @matsumoto-hisa on GitHub (Feb 19, 2026). Original GitHub issue: https://github.com/langchain-ai/deepagentsjs/issues/239 ## Description When `returnCommandWithStateUpdate` constructs a `ToolMessage`, it passes `lastMessage.content` directly. However, when using Anthropic models, `content` can be an array containing `tool_use` blocks. Passing these blocks as `ToolMessage.content` causes errors downstream because `tool_use` blocks are not valid content for a `ToolMessage`. ## Current Behavior ```ts // src/subagent.ts - returnCommandWithStateUpdate const lastMessage = messages?.[messages.length - 1]; return new Command({ update: { ...stateUpdate, messages: [new ToolMessage({ content: lastMessage?.content || "Task completed", // ← array with tool_use blocks passed as-is tool_call_id: toolCallId, name: "task" })] } }); ``` When `lastMessage.content` is an array like: ```json [ { "type": "text", "text": "Here is the result..." }, { "type": "tool_use", "id": "...", "name": "...", "input": {...} } ] ``` The `tool_use` blocks get passed into the `ToolMessage`, which is invalid. ## Expected Behavior `tool_use` blocks should be filtered out from the content array before constructing the `ToolMessage`: ```ts let content = lastMessage?.content || "Task completed"; if (Array.isArray(content)) { content = content.filter((block) => block.type !== "tool_use"); if (content.length === 0) content = "Task completed"; } return new Command({ update: { ...stateUpdate, messages: [new ToolMessage({ content, tool_call_id: toolCallId, name: "task" })] } }); ``` ## Reproduction 1. Create a supervisor with subagents using `createSubAgentMiddleware` 2. Use an Anthropic model (e.g., Claude) that returns array-format content with `tool_use` blocks 3. The subagent's final message content gets passed as-is to `ToolMessage`, causing errors ## Environment - deepagents: 1.8.0 - @langchain/anthropic: 1.3.3 - @langchain/core: 1.1.25 ## Workaround We are currently using `patch-package` to apply this fix locally.
yindo closed this issue 2026-06-05 17:21:11 -04:00
Author
Owner

@antonnak commented on GitHub (Feb 23, 2026):

This also affects thinking and redacted_thinking blocks, not just tool_use. See #245.

<!-- gh-comment-id:3946107324 --> @antonnak commented on GitHub (Feb 23, 2026): This also affects `thinking` and `redacted_thinking` blocks, not just `tool_use`. See #245.
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: langchain-ai/deepagentsjs#235