-
Notifications
You must be signed in to change notification settings - Fork 1.1k
Python: [Feature Branch] Added handling for conversation_id in Azure AI client #2098
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
dmytrostruk
merged 1 commit into
microsoft:feature-python-foundry-agents
from
dmytrostruk:foundry-agents-existing-conversation
Nov 11, 2025
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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
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
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
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
98 changes: 98 additions & 0 deletions
98
python/samples/getting_started/agents/azure_ai/azure_ai_with_existing_conversation.py
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,98 @@ | ||
| # Copyright (c) Microsoft. All rights reserved. | ||
| import asyncio | ||
| import os | ||
| from random import randint | ||
| from typing import Annotated | ||
|
|
||
| from agent_framework.azure import AzureAIClient | ||
| from azure.ai.projects.aio import AIProjectClient | ||
| from azure.identity.aio import AzureCliCredential | ||
| from pydantic import Field | ||
|
|
||
| """ | ||
| Azure AI Agent Existing Conversation Example | ||
|
|
||
| This sample demonstrates usage of AzureAIClient with existing conversation created on service side. | ||
| """ | ||
|
|
||
|
|
||
| def get_weather( | ||
| location: Annotated[str, Field(description="The location to get the weather for.")], | ||
| ) -> str: | ||
| """Get the weather for a given location.""" | ||
| conditions = ["sunny", "cloudy", "rainy", "stormy"] | ||
| return f"The weather in {location} is {conditions[randint(0, 3)]} with a high of {randint(10, 30)}°C." | ||
|
|
||
|
|
||
| async def example_with_client() -> None: | ||
| """Example shows how to specify existing conversation ID when initializing Azure AI Client.""" | ||
| print("=== Azure AI Agent With Existing Conversation and Client ===") | ||
| async with ( | ||
| AzureCliCredential() as credential, | ||
| AIProjectClient(endpoint=os.environ["AZURE_AI_PROJECT_ENDPOINT"], credential=credential) as project_client, | ||
| ): | ||
| # Create a conversation using OpenAI client | ||
| openai_client = await project_client.get_openai_client() | ||
| conversation = await openai_client.conversations.create() | ||
| conversation_id = conversation.id | ||
| print(f"Conversation ID: {conversation_id}") | ||
|
|
||
| async with AzureAIClient( | ||
| project_client=project_client, | ||
| # Specify conversation ID on client level | ||
| conversation_id=conversation_id, | ||
| ).create_agent( | ||
| name="BasicAgent", | ||
| instructions="You are a helpful agent.", | ||
| tools=get_weather, | ||
| ) as agent: | ||
| query = "What's the weather like in Seattle?" | ||
| print(f"User: {query}") | ||
| result = await agent.run(query) | ||
| print(f"Agent: {result.text}\n") | ||
|
|
||
| query = "What was my last question?" | ||
| print(f"User: {query}") | ||
| result = await agent.run(query) | ||
| print(f"Agent: {result.text}\n") | ||
|
|
||
|
|
||
| async def example_with_thread() -> None: | ||
| """This example shows how to specify existing conversation ID with AgentThread.""" | ||
| print("=== Azure AI Agent With Existing Conversation and Thread ===") | ||
| async with ( | ||
| AzureCliCredential() as credential, | ||
| AIProjectClient(endpoint=os.environ["AZURE_AI_PROJECT_ENDPOINT"], credential=credential) as project_client, | ||
| AzureAIClient(project_client=project_client).create_agent( | ||
| name="BasicAgent", | ||
| instructions="You are a helpful agent.", | ||
| tools=get_weather, | ||
| ) as agent, | ||
| ): | ||
| # Create a conversation using OpenAI client | ||
| openai_client = await project_client.get_openai_client() | ||
| conversation = await openai_client.conversations.create() | ||
| conversation_id = conversation.id | ||
| print(f"Conversation ID: {conversation_id}") | ||
|
|
||
| # Create a thread with the existing ID | ||
| thread = agent.get_new_thread(service_thread_id=conversation_id) | ||
|
|
||
| query = "What's the weather like in Seattle?" | ||
| print(f"User: {query}") | ||
| result = await agent.run(query, thread=thread) | ||
| print(f"Agent: {result.text}\n") | ||
|
|
||
| query = "What was my last question?" | ||
| print(f"User: {query}") | ||
| result = await agent.run(query, thread=thread) | ||
| print(f"Agent: {result.text}\n") | ||
|
|
||
|
|
||
| async def main() -> None: | ||
| await example_with_client() | ||
| await example_with_thread() | ||
|
|
||
|
|
||
| if __name__ == "__main__": | ||
| asyncio.run(main()) |
Oops, something went wrong.
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.
Uh oh!
There was an error while loading. Please reload this page.