Skip to content

Python: fix citation issue - #9436

Open
sophia-ramsey wants to merge 4 commits into
mainfrom
sramsey/python-citation
Open

sophia-ramsey wants to merge 4 commits into
mainfrom
sramsey/python-citation

Conversation

@sophia-ramsey

Copy link
Copy Markdown
Member

Motivation & Context

An internal bug reported that citations were not correctly being propagated with hosted agents. The citation would include the correct filename or source, but would not link to the annotation properly, instead registering as plain text.

Description & Review Guide

  • What are the major changes? Updated ResponsesHostServer to serialize URL and file citation annotations into Responses payloads for both streaming and non-streaming responses, with regression coverage for supported annotation types and edge cases.
  • What is the impact of these changes? Hosted agents now preserve citation metadata through the Python hosting boundary, allowing Teams and Copilot to render clickable citations instead of displaying unsupported raw citation markers.
  • What do you want reviewers to focus on? Edge cases or potential issues with any other wrappers or downstream agent services.

Related Issue

Fixes #9434

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: Used GPT-5.6 Sol for planning, implementation, and testing

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 balanced review requested due to automatic review settings October 15, 2026 23:51
@sophia-ramsey
sophia-ramsey deployed to github-app-auth October 15, 2026 23:51 — with GitHub Actions Active
@sophia-ramsey
sophia-ramsey deployed to github-app-auth October 15, 2026 23:51 — with GitHub Actions Active
@sophia-ramsey sophia-ramsey changed the title python: fix citation issue Python: fix citation issue Oct 15, 2026
@sophia-ramsey
sophia-ramsey deployed to github-app-auth October 15, 2026 23:52 — with GitHub Actions Active
@agent-framework-automation agent-framework-automation Bot added the python Usage: [Issues, PRs], Target: Python label Oct 15, 2026

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.

🟡 Changes recommended

Citation offsets can be shifted incorrectly, and ordinary streaming introduces quadratic text copying.

2 open findings
What changed in this PR

Preserves citation metadata across the Python hosting boundary so downstream clients can render clickable citations, addressing #9434.

Changes:

  • Converts framework citations into Responses annotations.
  • Includes annotations in streaming and non-streaming output.
  • Adds conversion and response regression tests.
File Description
python/​packages/​foundry_hosting/​tests/​test_responses.py Tests citation conversion, edge cases, and response consistency.
python/​packages/​foundry_hosting/​agent_framework_foundry_hosting/​_responses.py Converts and propagates citation annotations.

🧠 Review effort: Balanced


💡 Add a code-review agent skill for context-aware, tailored reviews. Learn more in the docs.

Comment thread python/packages/foundry_hosting/agent_framework_foundry_hosting/_responses.py Outdated
Comment thread python/packages/foundry_hosting/agent_framework_foundry_hosting/_responses.py Outdated
@github-code-quality

github-code-quality Bot commented Oct 15, 2026 •

Copy link
Copy Markdown
Contributor

Code coverage overview

Languages: Python

Python / code-coverage/python

main edc2427 sramsey/python-citat... 63f108a +/-
92% 92% 0%
View coverage of the 2 most impacted files.
File main edc2427 sramsey/python-citat... 63f108a +/-
packages/anthro..._chat_client.py 96% 96% 0%
packages/foundr...g/_responses.py 88% 89% +1%

ℹ️ Calculations represent line coverage. Results may change as calculations are updated.


Updated October 24, 2026 00:07 UTC

@github-actions github-actions Bot 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.

MAF Automated Review — Iteration 1

Result: Findings reported
Scope: full PR (1 commit(s)): 0da89b5958e8
Model: gpt-5.6-sol

Overview

The review found 3 verified inline finding(s).

Reviewed the supplied pull-request change set across correctness, security/reliability, architecture, and failure behavior.
3 verified findings remained after source verification (3 medium) across 1 file. Details are attached to the affected lines below.

Affected areas: python/packages/foundry_hosting/agent_framework_foundry_hosting/_responses.py

Comment thread python/packages/foundry_hosting/agent_framework_foundry_hosting/_responses.py Outdated
Comment thread python/packages/foundry_hosting/agent_framework_foundry_hosting/_responses.py Outdated
@sophia-ramsey
sophia-ramsey deployed to github-app-auth October 16, 2026 22:29 — with GitHub Actions Active
cit["snippet"] = citation.cited_text
if citation.file_id:
cit["file_id"] = citation.file_id
cit.setdefault("annotated_regions", [])

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Just for my own ignorance, why aren't we mapping annotated_regions anymore here?

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

In the current code, annotated_regions refers to the information location in the source document, but downstream was being interpreted as the positions in the generated response, so I took it out so it would not be forwarded to the citation. The source locations are still in raw_representation.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

This seems strange to me as well, annotations should be about locations in source documents IIRC, and relying on raw_representation is also not ideal, since those do not survive (de)ser. I also don't know why this change is related to the Foundry Hosting, was the original issue about using Anthropic within FHA?

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

I updated the PR to preserve the original Anthropic behavior, but I added a check for "annotated_regions_coordinate_space": "source" to indicate that the annotated_regions refer to the location in the source document instead of locations for the generated response. Does this seem like an appropriate way to handle the citations?

@jpalvarezl Jose Alvarez (jpalvarezl) left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Approve with a small nit

Comment thread python/packages/foundry_hosting/agent_framework_foundry_hosting/_responses.py Outdated
@sophia-ramsey
sophia-ramsey deployed to github-app-auth October 23, 2026 23:31 — with GitHub Actions Active
@agent-framework-automation agent-framework-automation Bot added the documentation Usage: [Issues, PRs], Target: documentation in the code base and learn docs label Oct 23, 2026
@sophia-ramsey
sophia-ramsey force-pushed the sramsey/python-citation branch from ea1a03c to 63f108a Compare October 24, 2026 00:02
@sophia-ramsey
sophia-ramsey deployed to github-app-auth October 24, 2026 00:02 — with GitHub Actions Active

This branch was successfully deployed

1 active deployment
github-app-auth — 63f108af Deployed Oct 24, 2026 by sophia-ramsey via add_label #26062
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

documentation Usage: [Issues, PRs], Target: documentation in the code base and learn docs python Usage: [Issues, PRs], Target: Python

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Python: [Bug]: Annotations not propagated correctly

5 participants