Skip to content

Conversation

@christian-bromann
Copy link
Contributor

Background

When using @ai-sdk/langchain with LangGraph agents, the stream was missing finish and finish-step events. The toUIMessageStream function only emitted these lifecycle events for model and streamEvents stream types, but not for langgraph streams.

This caused the stream to go from text-end straight to [DONE] without proper protocol events, which breaks the expected AI SDK UI Message Stream protocol and can cause issues in multi-turn conversations.

Summary

  • Added finish event emission for LangGraph streams at the end of the stream
  • Added finish-step event emission for LangGraph streams when a step was started (tracked via langGraphState.currentStep)
  • Added 4 new tests to verify the finish event behavior for LangGraph streams
  • Updated existing test snapshots to include the new finish events

Manual Verification

  1. Created a LangGraph agent with tools
  2. Started a stream with streamMode: ['values', 'messages']
  3. Verified the stream now includes:
    • start-step when a step begins
    • finish-step when a step ends or at stream completion
    • finish at the end of the stream
  4. Verified multi-turn conversations work correctly without the "tool_calls must be followed by tool messages" error

Checklist

  • Tests have been added / updated (for bug fixes / features)
  • Documentation has been added / updated (for bug fixes / features) n/a
  • A patch changeset for relevant packages has been added (for bug fixes / features - run pnpm changeset in the project root)
  • I have reviewed this pull request (self-review)

Related Issues

Fixes #11417

Related reproduction: https://github.com/tuntisz/ai-sdk-langchain-bug-repro

Previously, `toUIMessageStream` only emitted `finish` events for model
and streamEvents stream types, but not for LangGraph streams. This caused
the stream to go from `text-end` straight to `[DONE]` without proper
lifecycle events.

This fix ensures that:
- LangGraph streams now emit `finish-step` when a step was started
- LangGraph streams now emit `finish` event at the end of the stream

This addresses the issue where multi-turn conversations with LangGraph
agents would fail due to missing protocol events.
@vercel-ai-sdk vercel-ai-sdk bot added the ai/core label Jan 8, 2026
@lgrammel lgrammel merged commit 3f81ea8 into vercel:main Jan 8, 2026
24 of 25 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants