Skip to content

feat(anthropic): search result blocks and custom content documents - #11968

Closed
lithdew wants to merge 1 commit into
vercel:mainfrom
lithdew:main
Closed

lithdew wants to merge 1 commit into
vercel:mainfrom
lithdew:main

Conversation

@lithdew

@lithdew lithdew commented Jan 22, 2026 •

Copy link
Copy Markdown

Background

Anthropic's API supports search result blocks and custom content documents that enable Claude to cite specific passages in responses. This functionality was not exposed through the AI SDK, limiting users who want to build RAG applications with proper source attribution.

https://platform.claude.com/docs/en/build-with-claude/search-results
https://platform.claude.com/docs/en/build-with-claude/citations#custom-content-documents

Summary

Adds support for Anthropic's search result blocks and custom content documents, enabling Claude to cite specific passages from user-provided content.

Search results can be passed as file parts with provider options specifying type: 'search_result' along with a source URL. Custom content documents use source: { type: 'content', content: [...] } for pre-chunked text that Claude can cite directly.

Two new citation location types are now parsed in responses: search_result_location and content_block_location. These return as source parts with provider metadata containing the cited text, source URL, search result index, and block ranges.

Includes streaming support with full test coverage for both generateText and streamText flows. Documentation updated with usage examples and configuration options.

Manual Verification

Ran the example files to verify end-to-end functionality:

  • pnpm tsx src/generate-text/anthropic-search-results.ts
  • pnpm tsx src/stream-text/anthropic-search-results.ts
  • pnpm tsx src/generate-text/anthropic-custom-content-documents.ts
  • pnpm tsx src/stream-text/anthropic-custom-content-documents.ts

Confirmed that:

  • Search results are sent correctly to the Anthropic API
  • Citations are returned and parsed with correct metadata
  • Streaming works with citations appearing in real-time
% pnpm tsx src/generate-text/anthropic-custom-content-documents.ts
Response: The document contains a first custom chunk and a second custom chunk.

[1] "First custom chunk." (blocks 0-1)
Title: Custom Content Doc

[2] "Second custom chunk." (blocks 1-2)
Title: Custom Content Doc
% pnpm tsx src/stream-text/anthropic-custom-content-documents.ts
The document contains two simple custom chunks. 

[1] "First custom chunk." (blocks 0-1)
Title: Custom Content Doc
The first is labeled as "First custom chunk." 

[2] "Second custom chunk." (blocks 1-2)
Title: Custom Content Doc
The second is labeled as "Second custom chunk."

Total citations: 2
% pnpm tsx src/generate-text/anthropic-search-results.ts
Response: Based on the search results provided, I can see there is information about quarterly revenue growth and additional context from the same search result. However, the search results only contain brief snippets without specific details, so I cannot provide a more comprehensive summary of the actual content or findings.

[1] "Search result snippet about quarterly revenue growth." (search_result 0, blocks 0-1)
Source: https://example.com/search/result1
Title: Quarterly Results

[2] "Additional context from the same search result." (search_result 1, blocks 0-1)
Source: https://example.com/search/result2
Title: Additional Context
% pnpm tsx src/stream-text/anthropic-search-results.ts
Based on the search results provided, I can see there is 

[1] "Search result snippet about quarterly revenue growth." (search_result 0, blocks 0-1)
Source: https://example.com/search/result1
Title: Quarterly Results
information about quarterly revenue growth and 

[2] "Additional context from the same search result." (search_result 1, blocks 0-1)
Source: https://example.com/search/result2
Title: Additional Context
additional context from the same search result. 

However, the search results only contain brief snippet descriptions without the actual detailed content, so I cannot provide a comprehensive summary of the specific findings, data, or conclusions. To give you a meaningful summary, I would need access to the full content of these search results.

Total citations: 2

Checklist

  • Tests have been added / updated (for bug fixes / features)
  • Documentation has been added / updated (for bug fixes / features)
  • 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)

Future Work

Add support for returning search_result blocks from tool calls. Need to double-check if custom content documents can be returned from tool calls. Will be done in a subsequent PR.

@ghost ghost added ai/provider related to a provider package. Must be assigned together with at least one `provider/*` label provider/anthropic Issues related to the @ai-sdk/anthropic provider labels Jan 22, 2026
@lithdew lithdew changed the title feat(anthropic): add search result blocks and custom content citations feat(anthropic): search result blocks and custom content documents Jan 22, 2026
@lithdew
lithdew force-pushed the main branch 2 times, most recently from 325c94a to d63bbb1 Compare January 22, 2026 21:43
Comment thread packages/anthropic/src/convert-to-anthropic-messages-prompt.ts
Comment thread packages/anthropic/src/anthropic-messages-options.ts
@lithdew
lithdew force-pushed the main branch 2 times, most recently from c828c74 to 0edf425 Compare January 23, 2026 15:24
@lithdew

lithdew commented Jan 23, 2026

Copy link
Copy Markdown
Author

cc @aayush-kapoor @lgrammel

Adds support for Anthropic's search result blocks and custom content
documents, enabling Claude to cite specific passages from user-provided
content.

Search results can be passed as file parts with provider options
specifying `type: 'search_result'` along with a source URL. Custom
content documents use `source: { type: 'content', content: [...] }`
for pre-chunked text that Claude can cite directly.

Two new citation location types are now parsed in responses:
`search_result_location` and `content_block_location`. These return
as `source` parts with provider metadata containing the cited text,
source URL, search result index, and block ranges.

Includes streaming support with full test coverage for both
generateText and streamText flows. Documentation updated with
usage examples and configuration options.
@lithdew

lithdew commented Feb 12, 2026

Copy link
Copy Markdown
Author

Any chance for review? 👀

@gr2m

gr2m commented Jul 28, 2026

Copy link
Copy Markdown
Collaborator

Superseded by #16740.

@gr2m gr2m closed this Jul 28, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

ai/provider related to a provider package. Must be assigned together with at least one `provider/*` label provider/anthropic Issues related to the @ai-sdk/anthropic provider

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants