Description
When using @ai-sdk/langchain with LangGraph agents that have tools, multi-turn conversations fail with:
400 An assistant message with 'tool_calls' must be followed by tool messages responding to each 'tool_call_id'. The following tool_call_ids did not have response messages: call_xxxxx
Reproduction
Repository: https://github.com/tuntisz/ai-sdk-langchain-bug-repro
git clone https://github.com/tuntisz/ai-sdk-langchain-bug-repro
cd ai-sdk-langchain-bug-repro
pnpm install
pnpm test
The tests fail, showing:
AIMessages with tool_calls: 2
ToolMessages: 1
ORPHANED tool call IDs: [ 'call_8xtoEZ2bDLCMkKhK1wQ1Y3XC' ]
Visual reproduction
- Set
OPENAI_API_KEY and run pnpm dev
- Open http://localhost:3000
- Type:
do maths with 123 → works
- Type:
do maths with 345 → works
- Type:
do maths with 999 → ERROR
Root Cause
The bug is in toUIMessageStream's handling of LangGraph values events:
-
When LangGraph emits a values event, it contains the full message history including AIMessages with tool_calls from previous turns
-
processLangGraphEvent (case "values", ~line 540-560 in adapter.ts) iterates through ALL messages and emits tool-input-start/tool-input-available for any AIMessage with tool_calls that hasn't been emitted in the current stream
-
The bug: It emits these events for historical tool calls but does NOT emit corresponding tool-output-available events for them
-
The client builds a UIMessage with tool parts in state input-available (no output)
-
When toBaseMessages converts this back, it creates an AIMessage with tool_calls but no ToolMessage follows
Evidence from stream
On the third request, the stream shows:
{"type":"start"}
{"type":"tool-input-start","toolCallId":"call_HISTORICAL",...} <- Re-emitted from history
{"type":"tool-input-available","toolCallId":"call_HISTORICAL",...}
{"type":"start-step"}
{"type":"tool-input-start","toolCallId":"call_CURRENT",...} <- Current turn
{"type":"tool-input-available","toolCallId":"call_CURRENT",...}
{"type":"tool-output-available","toolCallId":"call_CURRENT",...} <- Only current gets output!
{"type":"error","errorText":"400 An assistant message with 'tool_calls'..."}
Suggested Fix
In processLangGraphEvent case "values", when iterating through historical messages, either:
- Skip emitting tool events for historical tool calls - they're already in the client's message history and shouldn't be re-emitted
- Or emit both input AND output events for historical tool calls to keep them consistent
Option 1 seems cleaner since the client already has the complete tool call history.
Environment
@ai-sdk/langchain: 2.0.3
ai: 6.0.3
langchain: 1.2.3
@langchain/openai: 1.2.0
- Node.js: 20.x
Screenshot
Notice that each turn with a tool call sees the prior turns tool call started, but not completed. The request fails on the third message with a tool call.

Description
When using
@ai-sdk/langchainwith LangGraph agents that have tools, multi-turn conversations fail with:Reproduction
Repository: https://github.com/tuntisz/ai-sdk-langchain-bug-repro
The tests fail, showing:
Visual reproduction
OPENAI_API_KEYand runpnpm devdo maths with 123→ worksdo maths with 345→ worksdo maths with 999→ ERRORRoot Cause
The bug is in
toUIMessageStream's handling of LangGraphvaluesevents:When LangGraph emits a
valuesevent, it contains the full message history including AIMessages withtool_callsfrom previous turnsprocessLangGraphEvent(case "values", ~line 540-560 in adapter.ts) iterates through ALL messages and emitstool-input-start/tool-input-availablefor any AIMessage withtool_callsthat hasn't been emitted in the current streamThe bug: It emits these events for historical tool calls but does NOT emit corresponding
tool-output-availableevents for themThe client builds a
UIMessagewith tool parts in stateinput-available(no output)When
toBaseMessagesconverts this back, it creates anAIMessagewithtool_callsbut noToolMessagefollowsEvidence from stream
On the third request, the stream shows:
Suggested Fix
In
processLangGraphEventcase "values", when iterating through historical messages, either:Option 1 seems cleaner since the client already has the complete tool call history.
Environment
@ai-sdk/langchain: 2.0.3ai: 6.0.3langchain: 1.2.3@langchain/openai: 1.2.0Screenshot
Notice that each turn with a tool call sees the prior turns tool call started, but not completed. The request fails on the third message with a tool call.
