Skip to content

Python: Surface Anthropic citations_delta annotations when streaming and read document_title for char_location - #9898

Open
Zakaria Khchiche (Zakariakhchiche) wants to merge 2 commits into
microsoft:mainfrom
Zakariakhchiche:fix-anthropic-citations
Open

Zakaria Khchiche (Zakariakhchiche) wants to merge 2 commits into
microsoft:mainfrom
Zakariakhchiche:fix-anthropic-citations

Conversation

@Zakariakhchiche

@Zakariakhchiche Zakaria Khchiche (Zakariakhchiche) commented Oct 30, 2026 •

Copy link
Copy Markdown
Contributor

Motivation & Context

Citations returned by Anthropic are lost or broken in two cases:

  1. Streaming: each citation arrives as a citations_delta event on the open text block. _parse_contents_from_anthropic has no case for it, so streamed responses come back without any annotations, while the same request without streaming has them.
  2. char_location citations: the parser reads citation.title, but BetaCitationCharLocation exposes document_title. Document citations therefore have no title. This is the field issue raised in the review of Python: Surface Anthropic citations_delta annotations when streaming #8978.

Description & Review Guide

  • What are the major changes?
    • New citations_delta case: it emits an empty text content with merge_mode="append" when streaming, carrying the parsed annotation, so it merges into the text it belongs to.
    • _parse_citations_from_anthropic accepts a citations_delta block, reading its single citation.
    • The char_location branch reads document_title.
    • Two tests: one builds a real BetaCitationCharLocation and checks the title, snippet and span; one streams a text block with a char_location and a web_search_result_location citation and checks that both end up as annotations on the final text.
  • What is the impact of these changes? Streaming and non-streaming responses now expose the same citations. There is no API change.
  • What do you want reviewers to focus on? Whether an empty appended text is the preferred way to attach a streamed annotation to the open text block.

Related Issue

Fixes #8977.

No other open PR addresses this issue (#8978 was closed).

AI Assistance

  • No material AI assistance was used.
  • This is an AI-assisted contribution. I reviewed, understood, and verified all submitted content and accept responsibility for it.

AI assistance details:

Contribution Checklist

  • The code builds clean without any errors or warnings
  • All unit tests pass, and I have added new tests where possible
  • The PR follows the Contribution Guidelines
  • This PR links to an agreed issue with no competing open PR, or the Related Issue section documents a trivial-change or repository-automation exception.
  • This is not a breaking change. If it is a breaking change, add the breaking change label (or add "[BREAKING]" to the title prefix, before or after any language prefix) — a workflow keeps the label and title prefix in sync automatically.

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🟢 Approval recommended

There are no active review issues for the streamed-citation and document-title changes.

No open issues 🎉

What changed in this PR

This PR addresses #8977 by preserving Anthropic citations in streamed responses and correcting document citation titles in the Python client, without changing the public API.

Changes:

  • Handles citations_delta events using empty appended text content to carry annotations.
  • Reads document_title for char_location citations.
  • Adds regression tests for document citation metadata and streamed document/web citations.
File Description
python/​packages/​anthropic/​tests/​test_anthropic_client.py Adds regression coverage for citation titles and streamed annotations.
python/​packages/​anthropic/​agent_framework_anthropic/​_chat_client.py Parses streamed citation deltas and corrects document title extraction.

Review effort: Balanced

This branch was previously deployed

1 inactive deployment
github-app-auth — 266fe5a4 Deployed Oct 30, 2026 by Zakariakhchiche via team_check #8180
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

python Usage: [Issues, PRs], Target: Python

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Python: [Bug]: AnthropicClient drops citations in streaming mode (citations_delta ignored)

2 participants