Skip to content

Python: Azure AI Search provider improvements - EmbeddingGenerator, async context manager, KB message handling#4212

Merged
eavanvalkenburg merged 3 commits intomicrosoft:mainfrom
eavanvalkenburg:fix_3001
Feb 25, 2026
Merged

Python: Azure AI Search provider improvements - EmbeddingGenerator, async context manager, KB message handling#4212
eavanvalkenburg merged 3 commits intomicrosoft:mainfrom
eavanvalkenburg:fix_3001

Conversation

@eavanvalkenburg
Copy link
Member

Summary

This PR improves the Azure AI Search context provider with several fixes and enhancements:

Key Changes

  • EmbeddingGenerator support for semantic mode — The provider now accepts SupportsGetEmbeddings instances in addition to plain async callables for the embedding_function parameter, enabling richer embedding workflows.
  • Async context manager improvements — Added a dedicated close() method that properly cleans up all clients (search, index, and retrieval), and refactored __aexit__ to delegate to it.
  • Streamlined KB message conversion — Extracted _prepare_messages_for_kb_search and _parse_messages_from_kb_response helpers that handle both text and image content types between framework Message objects and KB SDK types.
  • Rich reference-to-annotation mapping — KB retrieval references are now fully captured as Annotation objects with all available metadata: doc_key, reranker_score, source_data, reference_type, sensitivity_label, and the raw reference object.
  • Both search paths return Message objects_semantic_search and _agentic_search now both return list[Message], simplifying the before_run hook.
  • Sample updates

Testing

  • All existing tests updated to reflect the new return types
  • Added comprehensive tests for _prepare_messages_for_kb_search, _parse_messages_from_kb_response, and _parse_references_to_annotations

Fixes #3001
Supersedes #3333

Copy link
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull request overview

This PR enhances the Azure AI Search context provider with several improvements to support richer embedding workflows, proper resource cleanup, and better knowledge base message handling.

Changes:

  • Added support for SupportsGetEmbeddings instances in addition to plain async callables for the embedding_function parameter
  • Implemented proper async context manager cleanup with a dedicated close() method that resets internal state flags
  • Refactored KB message conversion with dedicated helper methods (_prepare_messages_for_kb_search, _parse_messages_from_kb_response, _parse_references_to_annotations)
  • Changed return types from list[str] to list[Message] for both search paths, with rich annotation support for KB references

Reviewed changes

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

File Description
python/packages/azure-ai-search/agent_framework_azure_ai_search/_context_provider.py Core changes: Added SupportsGetEmbeddings support, close() method, message conversion helpers, fixed k_nearest_neighbors→k parameter, and unified return types to Message objects
python/packages/azure-ai-search/tests/test_aisearch_context_provider.py Updated all test assertions for new list[Message] return type, added comprehensive test classes for the three new helper methods
python/samples/02-agents/context_providers/azure_ai_search/azure_ai_with_search_context_semantic.py Added optional embedding client configuration for hybrid search demonstration
python/samples/02-agents/context_providers/azure_ai_search/azure_ai_with_search_context_agentic.py Updated default retrieval settings and added annotation display in streaming output

@markwallace-microsoft
Copy link
Member

markwallace-microsoft commented Feb 24, 2026

Python Test Coverage

Python Test Coverage Report •
FileStmtsMissCoverMissing
packages/azure-ai-search/agent_framework_azure_ai_search
   _context_provider.py3341097%111–112, 283, 334–335, 458–459, 635–636, 683
TOTAL21633310085% 

Python Unit Test Overview

Tests Skipped Failures Errors Time
4346 246 💤 0 ❌ 0 🔥 1m 19s ⏱️

eavanvalkenburg and others added 3 commits February 24, 2026 17:02
- Use separate variable for embeddings result to avoid mypy type reassignment error
- Fix test_vectorized_query_with_embedding_function: use real async function
  instead of AsyncMock which falsely matches SupportsGetEmbeddings protocol

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
@eavanvalkenburg eavanvalkenburg added this pull request to the merge queue Feb 25, 2026
Merged via the queue into microsoft:main with commit 4530504 Feb 25, 2026
29 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Python: I am unable to submit a second question when using AzureAISearchContextProvider.

6 participants