Skip to content

Anthropic citations and citations_delta lost in streaming instrumentation #1661

@braintrust-bot

Description

@braintrust-bot

Summary

Anthropic's citations feature adds a citations array to text content blocks in responses and streams citations_delta events during streaming. The auto-instrumentation plugin's streaming aggregation drops all citation data because:

  1. citations_delta is not recognized as a delta type — only text_delta and input_json_delta are handled
  2. Reconstructed text blocks are built as { type: "text", text: accumulated } without preserving any citations array

In non-streaming mode, the full content array passes through extractOutput and citations survive on the text blocks. The gap is specifically in the streaming path.

What instrumentation is missing

Plugin streaming aggregation (js/src/instrumentation/plugins/anthropic-plugin.ts)

In aggregateAnthropicStreamChunks, the content_block_delta handler (lines 174–193) only recognizes:

  • text_delta → accumulates text
  • input_json_delta → accumulates JSON

citations_delta events are silently ignored. When finalizeContentBlock reconstructs the text block, it creates { ...contentBlock, text: accumulated } — the citations array is never populated.

Vendor types (js/src/vendor-sdk-types/anthropic.ts)

AnthropicStreamEvent's content_block_delta union (lines 112–114) only includes:

  • { type: "text_delta"; text: string }
  • { type: "input_json_delta"; partial_json: string }

Missing: { type: "citations_delta"; citation: { type: string; cited_text: string; document_index: number; ... } }

Wrapper streaming (js/src/wrappers/anthropic.ts)

The wrapper's streamNextProxy also only handles text_delta and input_json_delta in its content_block_delta processing, so citations are lost in the wrapper path as well.

Impact

Users enabling citations on Anthropic documents (plain text, PDFs, custom content) and using streaming will see spans where all citation source references are silently dropped. This affects:

  • Document-grounded RAG applications using citations for verification
  • Web search tool responses (citations are always enabled for web search results)
  • Any workflow relying on citation data for evaluation or debugging

Braintrust docs status

Braintrust Anthropic integration docs do not mention citations (not_found).

Upstream reference

  • Anthropic Citations docs: https://platform.claude.com/docs/en/docs/build-with-claude/citations
  • Citations are a stable feature, supported on all active models except Haiku 3
  • Streaming format: citations_delta events contain individual citation objects to append to the current text block's citations array
  • Citation types: char_location, page_location, content_block_location, web_search_result_location
  • cited_text does not count toward output tokens

Local files inspected

  • js/src/instrumentation/plugins/anthropic-plugin.ts (lines 174–193: delta handling, lines 255–302: finalizeContentBlock)
  • js/src/vendor-sdk-types/anthropic.ts (lines 102–122: AnthropicStreamEvent)
  • js/src/wrappers/anthropic.ts (streaming delta handler)
  • e2e/scenarios/anthropic-instrumentation/scenario.impl.mjs (no citations test cases)

Metadata

Metadata

Assignees

No one assigned

    Labels

    bot-automationIssues generated by an agent automation

    Type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions