[Bug] @ai-sdk/langchain: tool-output-available emitted BEFORE tool-input-start for bare ToolMessage in messages mode#21548
Description
Describe the Bug
When a ToolMessage arrives via LangGraph's messages mode without a preceding AIMessageChunk (e.g. from handleChainEnd with the nostream tag), the adapter emits tool-output-available BEFORE tool-input-start. This violates the UI message stream contract and can cause the frontend's useChat hook to crash or render incorrectly.
To Reproduce
import { toUIMessageStream } from "@ai-sdk/langchain";
import { createUIMessageStream } from "ai";
import { ToolMessage } from "@langchain/core/messages";
// Simulate a bare ToolMessage in messages mode (no preceding AIMessageChunk)
async function* source() {
yield [
"messages",
[
new ToolMessage({
tool_call_id: "call_1",
content: "3 products found",
name: "searchProducts",
id: "tool-1",
}),
{ langgraph_node: "executor" },
],
];
}
const uiStream = toUIMessageStream(source());
// ... collect chunks
Observed chunk order:
1. tool-output-available (call_1)
2. tool-input-start (call_1) ← should come first
Expected chunk order:
1. tool-input-start (call_1)
2. tool-output-available (call_1)