Skip to content

Python: forward Azure AI Search query-source identity - #6915

Closed
Karunanidhi Mishra (kmishra1204) wants to merge 6 commits into
microsoft:mainfrom
kmishra1204:python-azure-search-query-source-auth
Closed

Python: forward Azure AI Search query-source identity#6915
Karunanidhi Mishra (kmishra1204) wants to merge 6 commits into
microsoft:mainfrom
kmishra1204:python-azure-search-query-source-auth

Conversation

@kmishra1204

@kmishra1204 Karunanidhi Mishra (kmishra1204) commented Jul 4, 2026

Copy link
Copy Markdown

Summary

  • add an optional query_source_credential to AzureAISearchContextProvider
  • forward the caller's Azure AI Search authorization token on agentic Knowledge Base retrieval requests
  • document the query-time identity option, add regression coverage, and record the change in the Python changelog

This addresses the Python side of #6864. The option is explicit so existing API key and managed identity flows keep their current behavior unless a caller supplies an async query-source credential.

Validation

  • uv run --project python --dev --no-sync pytest -q python/packages/azure-ai-search/tests/test_aisearch_context_provider.py::TestAgenticSearch::test_minimal_reasoning_returns_results python/packages/azure-ai-search/tests/test_aisearch_context_provider.py::TestAgenticSearch::test_query_source_credential_forwards_authorization_token python/packages/azure-ai-search/tests/test_aisearch_context_provider.py::TestAgenticSearch::test_query_source_credential_requires_async_credential
  • uv run --project python --dev --no-sync ruff check python/packages/azure-ai-search/agent_framework_azure_ai_search/_context_provider.py python/packages/azure-ai-search/tests/test_aisearch_context_provider.py
  • uv run --project python --dev --no-sync pyright python/packages/azure-ai-search/agent_framework_azure_ai_search/_context_provider.py
  • git diff --check -- python/CHANGELOG.md python/packages/azure-ai-search/README.md python/packages/azure-ai-search/agent_framework_azure_ai_search/_context_provider.py python/packages/azure-ai-search/tests/test_aisearch_context_provider.py

Copilot AI review requested due to automatic review settings July 4, 2026 22:31
@giles17 Giles Odigwe (giles17) added documentation Usage: [Issues, PRs], Target: documentation in the code base and learn docs python Usage: [Issues, PRs], Target: Python labels Jul 4, 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.

Pull request overview

Adds an opt-in way for Python agentic Azure AI Search retrieval to forward a caller’s query-time identity (Entra token) to Knowledge Base retrieval requests, enabling document-level permission trimming scenarios without changing existing API key/managed identity defaults.

Changes:

  • Added an optional query_source_credential parameter to AzureAISearchContextProvider and forwarded its token to KnowledgeBaseRetrievalClient.retrieve(...) via x_ms_query_source_authorization.
  • Documented query-time identity forwarding in the package README.
  • Added regression tests covering both default behavior (no header) and forwarding behavior (header present + correct scope).

Reviewed changes

Copilot reviewed 3 out of 3 changed files in this pull request and generated 2 comments.

File Description
python/packages/azure-ai-search/agent_framework_azure_ai_search/_context_provider.py Adds query_source_credential, obtains the Search scope token, and forwards it on agentic retrieval calls.
python/packages/azure-ai-search/tests/test_aisearch_context_provider.py Adds assertions and a new test verifying the authorization header forwarding behavior.
python/packages/azure-ai-search/README.md Documents the query-time user identity option and shows how to pass query_source_credential.

@kmishra1204

Copy link
Copy Markdown
Author

Updated in bd48f59 to address the async credential feedback: the docs now say query_source_credential must be an async Azure credential, _query_source_authorization() raises a clear TypeError if a sync credential is passed, and there is regression coverage for that misuse case.

@github-actions

github-actions Bot commented Jul 6, 2026

Copy link
Copy Markdown
Contributor

Python Test Coverage

Python Test Coverage Report •
FileStmtsMissCoverMissing
packages/azure-ai-search/agent_framework_azure_ai_search
   _context_provider.py3961795%95–96, 121–124, 547, 625–626, 753–754, 844, 849, 854, 953–954, 1002
TOTAL44017530387% 

Python Unit Test Overview

Tests Skipped Failures Errors Time
8595 33 💤 0 ❌ 0 🔥 2m 18s ⏱️

@moonbox3 Evan Mattson (moonbox3) 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.

Please also have a look at the failing CI/CD (code quality) checks.

@kmishra1204
Karunanidhi Mishra (kmishra1204) force-pushed the python-azure-search-query-source-auth branch from 5e049aa to 0d860aa Compare July 7, 2026 05:41
@kmishra1204

Copy link
Copy Markdown
Author

Thanks for the CI note. I rebased this branch on current main and addressed the test typing failures from the Test Typing Checks (3.11) job.

What changed:

  • Stored mock_retrieval.retrieve.await_args in a local variable and asserted it is not None before reading .kwargs in the new Azure AI Search tests.
  • The rebase also picks up upstream #6944, which fixes the root README stargazers link that was failing markdown-link-check.

Validation run locally:

  • uv run poe test-typing -P azure-ai-search --checker ty --checker zuban -> passed
  • uv run --directory python pytest -m "not integration" packages\azure-ai-search\tests\test_aisearch_context_provider.py -k "query_source_credential or agentic_search" -> 3 passed
  • uv run poe lint -P azure-ai-search -> passed
  • uv run poe pyright -P azure-ai-search -> passed
  • git diff --check -> passed

I also ran uv run poe test-typing -P azure-ai-search; the locally installed environment stops in mypy on a NumPy stub requiring Python 3.12+, before checking this package. The CI mypy job was already green, and the CI failures called out in this PR were the ty/zuban diagnostics fixed above.

@kmishra1204
Karunanidhi Mishra (kmishra1204) force-pushed the python-azure-search-query-source-auth branch from 0d860aa to 38b5ac4 Compare July 7, 2026 06:51
Comment thread python/CHANGELOG.md Outdated
@kmishra1204

Copy link
Copy Markdown
Author

Rebased this branch onto current upstream main to clear the behind-base state.

Current head: 38b5ac45

Validation rerun locally:

  • uv run poe test-typing -P azure-ai-search --checker ty --checker zuban
  • uv run pytest packages/azure-ai-search/tests/test_aisearch_context_provider.py -k "query_source or credential or retrieve"
  • uv run poe lint -P azure-ai-search
  • git diff --check upstream/main...HEAD

Results:

  • Type checks passed (ty and zuban).
  • Targeted tests passed: 5 passed, 123 deselected.
  • Package lint passed.
  • Diff check passed.

Live status after push: code checks have no failures. The remaining visible gates are the merge gatekeeper still in progress and the queued license/cla policy check.

@kmishra1204

Copy link
Copy Markdown
Author

Updated in 1358f3bc for the review note on the retrieval kwarg.

Change made:

  • retrieve(...) now passes query_source_authorization=... instead of the generic x_ms_query_source_authorization kwarg.
  • The focused regression assertions now check the SDK-supported kwarg name.

Validation rerun:

  • uv run pytest packages/azure-ai-search/tests/test_aisearch_context_provider.py -k "query_source or credential or retrieve" -> 5 passed, 123 deselected
  • uv run poe lint -P azure-ai-search -> passed
  • uv run poe test-typing -P azure-ai-search --checker ty --checker zuban -> passed
  • git diff --check upstream/main...HEAD -> passed

@kmishra1204

Copy link
Copy Markdown
Author

Also removed the manual python/CHANGELOG.md entry in 97f8a36a, per the release-note guidance.

Current review-response commits:

  • 1358f3bc uses query_source_authorization instead of the generic x_ms_* kwarg.
  • 97f8a36a removes the manual changelog entry.

Validation after the latest push:

  • uv run pytest packages/azure-ai-search/tests/test_aisearch_context_provider.py -k "query_source or credential or retrieve" -> 5 passed, 123 deselected
  • git diff --check upstream/main...HEAD -> passed

I resolved the two review threads that these commits address.

@moonbox3

Copy link
Copy Markdown
Contributor

Karunanidhi Mishra (@kmishra1204) please accept the CLA

@moonbox3

Copy link
Copy Markdown
Contributor

Karunanidhi Mishra (@kmishra1204) ping to accept the CLA please

@moonbox3

Copy link
Copy Markdown
Contributor

We appreciate your work on this. We need to get this fix in, so superseded by #7278.

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.

4 participants