Python: Feature/fix context provider lifecycle agentic mode#2650
Merged
moonbox3 merged 7 commits intomicrosoft:mainfrom Dec 8, 2025
Merged
Conversation
Member
Python Test Coverage Report •
Python Unit Test Overview
|
||||||||||||||||||||||||||||||
Contributor
There was a problem hiding this comment.
Pull request overview
This PR fixes a critical bug in the context provider lifecycle management that prevented multi-turn conversations when using AzureAISearchContextProvider in agentic mode (Knowledge Base retrieval). The bug caused the second and subsequent queries to fail because the retrieval client connection was being closed after each query.
Key Changes:
- Removed the
async with self.context_providerwrapper in_prepare_thread_and_messagesthat was incorrectly closing context provider resources after each query - Added a clarifying comment explaining that context provider lifecycle should be managed by the user
- Corrected the Azure AI Foundry project endpoint format in documentation
Reviewed changes
Copilot reviewed 2 out of 2 changed files in this pull request and generated 1 comment.
| File | Description |
|---|---|
python/packages/core/agent_framework/_agents.py |
Removed the async with wrapper around context_provider.invoking() that was causing premature resource cleanup after each query, and added a comment explaining the lifecycle management approach |
python/samples/getting_started/context_providers/azure_ai_search/README.md |
Fixed the Azure AI Foundry project endpoint URL format from incorrect AzureML format to correct AI Foundry format |
eavanvalkenburg
approved these changes
Dec 5, 2025
moonbox3
approved these changes
Dec 8, 2025
arisng
pushed a commit
to arisng/agent-framework
that referenced
this pull request
Feb 2, 2026
…t#2650) * refactor KB for index creation logic * add user agent header for tracking * fix mypy issues * fix agentic mode bug for search context provider --------- Co-authored-by: farzad528 <farzad528@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.
Motivation and Context
When using
AzureAISearchContextProviderin agentic mode (Knowledge Base retrieval), the second and subsequent queries fail with:This bug prevents users from having multi-turn conversations with agents that use Knowledge Base-powered RAG. The first query works, but any follow-up questions fail because the
KnowledgeBaseRetrievalClientconnection is prematurely closed.Root Cause: The
ChatAgent._prepare_thread_and_messagesmethod wraps the context provider invocation withasync with self.context_provider:, which calls__aexit__after each query. This closes stateful resources (like the retrieval client) after the first query completes.Why semantic mode was unaffected: The
SearchClientused in semantic mode is stateless - each search is independent. The agentic mode'sKnowledgeBaseRetrievalClientmaintains a persistent connection that gets closed by the erroneousasync withwrapper.Description
Changes in
python/packages/core/agent_framework/_agents.py:Removed the
async with self.context_provider:wrapper in_prepare_thread_and_messages. The context provider's lifecycle should be managed by the user (viaasync with ChatAgent(...) as agent) or persist across multiple invocations - not be opened/closed on every query.Before (incorrect):
After (correct):
This aligns with how
AggregateContextProviderworks - it enters providers once in__aenter__and exits once in__aexit__, whileinvoking()does not useasync with.Additional fix in
python/samples/getting_started/context_providers/azure_ai_search/README.md:Corrected the Azure AI Foundry project endpoint format in documentation from
https://myproject.api.azureml.mstohttps://<resource-name>.services.ai.azure.com/api/projects/<project-name>.Contribution Checklist