-
Notifications
You must be signed in to change notification settings - Fork 2k
Auto-finalize ResponseStream on iteration completion #4478
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
Merged
Changes from 1 commit
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
04f1cc3
Add multi-turn streaming sample and rename multi-turn samples
giles17 969952e
Auto-finalize ResponseStream on iteration completion
giles17 f939d3a
README fix
giles17 b1f3f87
Fix SIM102 lint: combine nested if statements
giles17 0b3e919
Merge branch 'main' into multi-turn-samples
giles17 6b9bc9a
Merge branch 'main' into multi-turn-samples
giles17 27e990e
Merge branch 'main' into multi-turn-samples
giles17 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
File renamed without changes.
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,75 @@ | ||
| # Copyright (c) Microsoft. All rights reserved. | ||
|
|
||
| import asyncio | ||
| import os | ||
|
|
||
| from agent_framework.azure import AzureOpenAIResponsesClient | ||
| from azure.identity import AzureCliCredential | ||
| from dotenv import load_dotenv | ||
|
|
||
| # Load environment variables from .env file | ||
| load_dotenv() | ||
|
|
||
| """ | ||
| Multi-Turn Streaming — Combine streaming output with session history | ||
|
|
||
| This sample shows how to stream responses while maintaining conversation | ||
| history across turns. The key is calling get_final_response() after | ||
| iterating the stream, which persists messages to the session. | ||
|
|
||
| Without get_final_response(), the session history is not updated and | ||
| the agent will not remember previous turns. | ||
|
|
||
| Environment variables: | ||
| AZURE_AI_PROJECT_ENDPOINT — Your Azure AI Foundry project endpoint | ||
| AZURE_OPENAI_RESPONSES_DEPLOYMENT_NAME — Model deployment name (e.g. gpt-4o) | ||
| """ | ||
|
|
||
|
|
||
| async def main() -> None: | ||
| # 1. Create the client and agent. | ||
| credential = AzureCliCredential() | ||
| client = AzureOpenAIResponsesClient( | ||
| project_endpoint=os.environ["AZURE_AI_PROJECT_ENDPOINT"], | ||
| deployment_name=os.environ["AZURE_OPENAI_RESPONSES_DEPLOYMENT_NAME"], | ||
| credential=credential, | ||
| ) | ||
|
|
||
| agent = client.as_agent( | ||
| name="ConversationAgent", | ||
| instructions="You are a friendly assistant. Keep your answers brief.", | ||
| ) | ||
|
|
||
| # 2. Create a session to maintain conversation history. | ||
| session = agent.create_session() | ||
|
|
||
| # 3. First turn — stream the response, then finalize to save history. | ||
| print("Agent: ", end="") | ||
| stream = agent.run("My name is Alice and I love hiking.", session=session, stream=True) | ||
| async for chunk in stream: | ||
| if chunk.text: | ||
| print(chunk.text, end="", flush=True) | ||
| await stream.get_final_response() # Persists messages to the session | ||
| print("\n") | ||
|
|
||
| # 4. Second turn — the agent remembers context from the first turn. | ||
| print("Agent: ", end="") | ||
| stream = agent.run("What do you remember about me?", session=session, stream=True) | ||
| async for chunk in stream: | ||
| if chunk.text: | ||
| print(chunk.text, end="", flush=True) | ||
| await stream.get_final_response() | ||
| print() | ||
|
|
||
|
|
||
| if __name__ == "__main__": | ||
| asyncio.run(main()) | ||
|
|
||
| """ | ||
| Sample output: | ||
|
|
||
| Agent: Hi Alice! That's awesome — hiking is such a great way to stay active | ||
| and enjoy nature. Do you have a favorite trail or spot you like to hike? | ||
|
|
||
| Agent: You're Alice, and you love hiking! | ||
| """ | ||
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
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.