Skip to content

Empty responses when enabling Bedrock citations for Anthropic models #9823

@lewwolfe

Description

@lewwolfe

Description

When using Anthropic models through AWS Bedrock with citations enabled, the SDK returns empty responses because it doesn't handle CitationsContentBlock types in the response stream.

Steps to Reproduce

  1. Configure the Vercel AI SDK to use an Anthropic model via Bedrock
  2. Enable citations in the provider options:
providerOptions: {
  bedrock: {
    citations: { enabled: true },
  },
}
  1. Make a request that would benefit from citations (e.g., with PDF documents)
  2. Observe that the response is empty

Expected Behavior

When citations are enabled, the SDK should:

  1. Parse CitationsContentBlock from the Bedrock response
  2. Extract the text content from within the citations block
  3. Return the complete response to the user (ideally with citation metadata)

Actual Behavior

The SDK doesn't properly process the message part if it is a Bedrock CitationsContentBlock and an empty part is returned.

Root Cause

Bedrock's response format changes when citations are enabled:

  • Without citations: Returns TextBlock objects
  • With citations: Returns CitationsContentBlock objects

The current implementation doesn't account for this alternative block type, causing the text content to be ignored.

Proposed Solution

Add handling for CitationsContentBlock in the response parser. The block structure typically includes:

  • Citation metadata
  • The actual text content
  • References to source documents

The parser should extract both the text and (optionally) preserve citation information for downstream use.

I'm in the process of creating a PR - Example code:

// citations
if (part.citationsContent) {
  for (const generatedContent of part.citationsContent.content) {
    // Push the citation generated content (text block) as text to prevent an empty response when citations are present
    content.push({
      type: 'text',
      text: generatedContent.text,
      // provide actual citations in providerMetadata
      providerMetadata: {
        bedrock: {
          citations: part.citationsContent.citations
        },
      },
    });
  }
}

Additional Context

Enabling citations significantly improves Anthropic models' ability to work with PDF documents and other source materials in Bedrock, making this feature valuable for RAG applications.

AI SDK Version

Example:

  • @ai-sdk/bedrock: latest

Code of Conduct

  • I agree to follow this project's Code of Conduct

Metadata

Metadata

Assignees

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions