feat(langchain): emit source-url/source-document parts from citations#15757
Merged
Conversation
LangChain citation annotations on text content blocks (e.g. from web search or RAG) were previously dropped instead of being surfaced to the UI. They are now mapped to spec-compliant AI SDK source-url / source-document UI message stream parts across the model, streamEvents, and LangGraph (messages + values) paths. Citation metadata that the source parts cannot represent natively (citedText, startIndex, endIndex, source) is preserved under providerMetadata.langchain so the projection is lossless. Sources are deduped by sourceId; url-less citations are keyed by content rather than position to avoid collisions when the messages and values events surface differing citation subsets. Co-authored-by: Cursor <cursoragent@cursor.com>
Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
Collaborator
|
@dflynn15 please sign the commits, see https://docs.github.com/en/authentication/managing-commit-signature-verification/signing-commits (otherwise we cannot merge) |
Contributor
Author
|
Thanks for the flag, all fixed up, apologies for that @lgrammel! |
lgrammel
approved these changes
Jun 5, 2026
Contributor
|
|
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.
Summary
The
@ai-sdk/langchainadapter previously dropped LangChain citation annotations entirely. Models that return citations through LangChain (web search, RAG, etc.) produced no source information in auseChatUI, even though the same models surfacesource-urlparts when used through native AI SDK providers.This change maps LangChain citation annotations to spec-compliant
source-url/source-documentUI message stream parts.{ type: 'citation', url?, title?, source?, citedText?, startIndex?, endIndex? }entries in theannotationsarray of a text content block. The adapter's text extractors only read.text, so these were silently discarded.urlbecomesource-urlparts (keyed by url); url-less citations with atitle/sourcebecomesource-documentparts. Url-less citations with no human-readable label are skipped rather than emitted as a placeholder.processModelChunk),streamEvents(on_chat_model_stream), and LangGraph (messages+values).citedText,startIndex,endIndex,source) is preserved underproviderMetadata.langchain, so the projection is lossless.sourceId. Url-less citations are keyed by content (not position) so that differing citation subsets/orderings between themessagesandvaluesevents don't cause id collisions.Test plan
extractCitationsFromContentBlocks(array content, serializedkwargsmessages, empty annotations, plain-string content).emitSourceChunks:source-url,source-document,providerMetadataround-trip + omission, dedupe across calls, url-less id collision regression, and skipping label-less citations.streamEvents, and LangGraph (messages + values dedupe) paths.pnpm --filter @ai-sdk/langchain test(node + edge) — 208 passing.tsc --noEmitclean for the package.Notes
patchchangeset included.Made with Cursor