Skip to content

Commit 551219c

Browse files
authored
Python: Feature/fix context provider lifecycle agentic mode (#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 <[email protected]>
1 parent c34af00 commit 551219c

File tree

2 files changed

+19
-17
lines changed
  • python
    • packages/core/agent_framework

2 files changed

+19
-17
lines changed

python/packages/core/agent_framework/_agents.py

Lines changed: 18 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1268,22 +1268,24 @@ async def _prepare_thread_and_messages(
12681268
thread_messages.extend(await thread.message_store.list_messages() or [])
12691269
context: Context | None = None
12701270
if self.context_provider:
1271-
async with self.context_provider:
1272-
context = await self.context_provider.invoking(input_messages or [], **kwargs)
1273-
if context:
1274-
if context.messages:
1275-
thread_messages.extend(context.messages)
1276-
if context.tools:
1277-
if chat_options.tools is not None:
1278-
chat_options.tools.extend(context.tools)
1279-
else:
1280-
chat_options.tools = list(context.tools)
1281-
if context.instructions:
1282-
chat_options.instructions = (
1283-
context.instructions
1284-
if not chat_options.instructions
1285-
else f"{chat_options.instructions}\n{context.instructions}"
1286-
)
1271+
# Note: We don't use 'async with' here because the context provider's lifecycle
1272+
# should be managed by the user (via async with) or persist across multiple invocations.
1273+
# Using async with here would close resources (like retrieval clients) after each query.
1274+
context = await self.context_provider.invoking(input_messages or [], **kwargs)
1275+
if context:
1276+
if context.messages:
1277+
thread_messages.extend(context.messages)
1278+
if context.tools:
1279+
if chat_options.tools is not None:
1280+
chat_options.tools.extend(context.tools)
1281+
else:
1282+
chat_options.tools = list(context.tools)
1283+
if context.instructions:
1284+
chat_options.instructions = (
1285+
context.instructions
1286+
if not chat_options.instructions
1287+
else f"{chat_options.instructions}\n{context.instructions}"
1288+
)
12871289
thread_messages.extend(input_messages or [])
12881290
if (
12891291
thread.service_thread_id

python/samples/getting_started/context_providers/azure_ai_search/README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@ Run `az login` if using Entra ID authentication.
6464
```env
6565
AZURE_SEARCH_ENDPOINT=https://myservice.search.windows.net
6666
AZURE_SEARCH_INDEX_NAME=my-index
67-
AZURE_AI_PROJECT_ENDPOINT=https://myproject.api.azureml.ms
67+
AZURE_AI_PROJECT_ENDPOINT=https://<resource-name>.services.ai.azure.com/api/projects/<project-name>
6868
AZURE_AI_MODEL_DEPLOYMENT_NAME=gpt-4o
6969
# Optional - omit to use Entra ID
7070
AZURE_SEARCH_API_KEY=your-search-key

0 commit comments

Comments
 (0)