Python: Enhance Azure AI Search Citations with Document URLs in Foundry V2#4028
Open
giles17 wants to merge 4 commits intomicrosoft:mainfrom
Open
Python: Enhance Azure AI Search Citations with Document URLs in Foundry V2#4028giles17 wants to merge 4 commits intomicrosoft:mainfrom
giles17 wants to merge 4 commits intomicrosoft:mainfrom
Conversation
…ry V2 (Responses API) Override _parse_response_from_openai and _parse_chunk_from_openai in RawAzureAIClient to extract get_urls from azure_ai_search_call_output items and enrich url_citation annotations with document-specific URLs. - Non-streaming: first pass collects get_urls, post-processes annotations - Streaming: captures search output state, enriches url_citation events (also handles url_citation annotation type not handled by base class) - Updated V2 sample to demonstrate citation URL extraction - Added 14 unit tests covering extraction, enrichment, and edge cases Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
Member
Python Test Coverage Report •
Python Unit Test Overview
|
||||||||||||||||||||||||||||||
Contributor
There was a problem hiding this comment.
Pull request overview
Enhances the Azure AI (Foundry V2 / Responses API) integration to surface Azure AI Search per-document REST URLs (get_url) on citation annotations, for both non-streaming and streaming responses.
Changes:
- Added extraction/mapping/enrichment utilities in the Azure AI Responses client to attach
get_urlto citation annotations. - Overrode non-streaming and streaming parsing to capture Azure AI Search
get_urlsand enrich citations. - Updated the Azure AI Search sample and added a dedicated set of unit tests for this behavior.
Reviewed changes
Copilot reviewed 3 out of 3 changed files in this pull request and generated 3 comments.
| File | Description |
|---|---|
| python/samples/02-agents/providers/azure_ai/azure_ai_with_azure_ai_search.py | Demonstrates reading enriched citation additional_properties.get_url for non-streaming and streaming runs. |
| python/packages/azure-ai/agent_framework_azure_ai/_client.py | Implements extraction of get_urls from azure_ai_search_call_output items and citation enrichment in response parsing (including streaming). |
| python/packages/azure-ai/tests/test_azure_ai_client.py | Adds unit tests for URL extraction, mapping, enrichment, and streaming behavior/state handling. |
…sponse - Remove all direct openai/pydantic imports from _client.py - Override _inner_get_response instead of _parse_response_from_openai/_parse_chunk_from_openai - Use closure-local state for streaming instead of instance-level _streaming_search_get_urls - Add _build_url_citation_content helper for streaming url_citation handling - Fix mypy errors by using str(value or '') for Annotation TypedDict fields - Fix docstring to say 'citation' instead of 'url_citation' - Update tests to match new approach Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
The azure_ai_search_call_output item only has populated output data (including get_urls) in the response.output_item.done event, not in the response.output_item.added event. Also removed the search_get_urls guard on url_citation handling so annotations are always produced even if get_urls haven't been captured yet. Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Enriches Azure AI Search
url_citationannotations with per-document REST API URLs (get_url) for the Foundry V2 (Responses API) path. Previously,url_citationannotations only contained the search service base URL, making it hard for users to identify which specific document was referenced.This is the V2 counterpart to PR #2066 which solved the same problem for Foundry V1 (Assistants API).
Resolves #2496
Problem
When using Azure AI Search as a tool with the Responses API, the
url_citationannotation in the assistant's response only contains the search service base URL (e.g.,https://search.example.com/), not the document-specific URL. The actual per-document URLs exist in theazure_ai_search_call_outputresponse items underoutput.get_urls[].Solution
Override
_inner_get_responseinRawAzureAIClientto post-process both streaming and non-streaming responses:Non-streaming: Wraps the awaitable to extract
get_urlsfromraw_representation.outputafter the base class parses the response, then enriches citation annotations.Streaming: Registers a transform hook on
ResponseStreamthat:get_urlsfromazure_ai_search_call_outputitems (viaresponse.output_item.addedandresponse.output_item.doneevents — the data is only fully populated in thedoneevent)url_citationannotations (which the base class doesn't handle in streaming) by creating properContentobjects with enrichedAnnotationentriesHow users access citations
Changes
packages/azure-ai/agent_framework_azure_ai/_client.py— Added_inner_get_responseoverride, helper methods (_extract_azure_search_urls,_get_search_doc_url,_enrich_annotations_with_search_urls,_build_url_citation_content)packages/azure-ai/tests/test_azure_ai_client.py— Added 14 unit tests covering helpers, non-streaming enrichment, streaming hook registration, streaming URL capture and annotation enrichmentsamples/02-agents/providers/azure_ai/azure_ai_with_azure_ai_search.py— Updated sample demonstrating citation extraction for both streaming and non-streaming