Backport: fix(langchain): recognize Python AIMessageChunk type in RemoteGraph streams#15604
Merged
Merged
Conversation
…treams (#15196) ## Background `@ai-sdk/langchain`'s `toUIMessageStream` adapter handles plain JSON message objects from LangGraph `RemoteGraph` streams. The type guard `isAIMessageChunk` and the `values`-event `tool_calls` extraction in `processLangGraphEvent` both match `obj.type === 'ai'`, which is the discriminator used by TypeScript `langchain-core`. Python `langchain-core` serializes streaming chunks with `type === 'AIMessageChunk'` ([Python source](https://github.com/langchain-ai/langchain/blob/master/libs/core/langchain_core/messages/ai.py)) instead of the TypeScript `type === 'ai'` ([TypeScript source](https://github.com/langchain-ai/langchainjs/blob/main/libs/langchain-core/src/messages/ai.ts)). Plain Python message objects fall through both gates. User-visible symptom: when streaming from a Python LangGraph server (`langgraph new --python`, LangSmith Cloud Python deployments, RemoteGraph against a Python deployment), `start`, `start-step`, `finish-step`, and `finish` arrive in the UI stream, but `text-start` / `text-delta` / `text-end` and tool-call events never do. The model output is silently lost. ## Summary `packages/langchain/src/utils.ts`: - `isAIMessageChunk`: also match `obj.type === 'AIMessageChunk'` for plain message objects; JSDoc updated to document both discriminators. - `processLangGraphEvent` `values` branch: also recognize `obj.type === 'AIMessageChunk'` when extracting `tool_calls` from non-streamed messages. The change is intentionally narrow — no behavior change for TypeScript callers, only widens the accepted discriminator for plain RemoteGraph objects. ## Manual Verification Reproduced against `@ai-sdk/langchain@2.0.162` (stable) and `@ai-sdk/langchain@3.0.0-canary.131` (canary) using a standalone async-iterable mock that emits `['messages', [chunk, metadata]]` events with both shapes (`type: 'ai'` vs `type: 'AIMessageChunk'`): - Before fix: TypeScript shape produced 2 `text-delta` UIMessageChunks, Python shape produced 0. - After fix (patched `dist/index.js` locally): both shapes produced identical output (`text-start`, 2 `text-delta`, `text-end`). Then verified at the source level: the three new regression tests fail on unfixed `main`, all 190 langchain tests pass with the fix applied. ## Checklist - [x] All commits are signed (PRs with unsigned commits cannot be merged) - [x] Tests have been added / updated (for bug fixes / features) - [x] Documentation has been added / updated (for bug fixes / features) — JSDoc on `isAIMessageChunk` - [x] A _patch_ changeset for relevant packages has been added (for bug fixes / features - run `pnpm changeset` in the project root) - [x] I have reviewed this pull request (self-review) ## Future Work `isToolMessageType` (same file) uses the equivalent `obj.type === 'tool'` pattern. Python `langchain-core` also uses the `'tool'` literal for `ToolMessage`, so it is not affected by this bug — but if future Python versions emit class-name discriminators (`'ToolMessage'`), a parallel guard would be needed. Out of scope here. I deliberately did not add a runnable example under `examples/` (per AGENTS.md "When to Deviate"): exercising the bug end-to-end requires a Python LangGraph server, which is impractical for a TypeScript monorepo example, and the new unit tests serve as a deterministic, in-process reproduction. Happy to add one if maintainers prefer. ## Related Issues Fixes #14341. Co-authored-by: cristiandrei1234 <cristiandrei1234@users.noreply.github.com> Co-authored-by: Lars Grammel <lars.grammel@gmail.com>
5 tasks
lgrammel
approved these changes
May 26, 2026
auto-merge was automatically disabled
May 26, 2026 23:00
Pull request was closed
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
This is an automated backport of #15196 to the release-v6.0 branch. FYI @cristiandrei1234