Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
75 changes: 75 additions & 0 deletions python/samples/01-get-started/03b_multi_turn_streaming.py
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
Comment thread
giles17 marked this conversation as resolved.
Outdated
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!
"""
3 changes: 2 additions & 1 deletion python/samples/01-get-started/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,8 @@ export AZURE_OPENAI_RESPONSES_DEPLOYMENT_NAME="gpt-4o" # optional, defaults to
|---|------|-------------------|
| 1 | [01_hello_agent.py](01_hello_agent.py) | Create your first agent and run it (streaming and non-streaming). |
| 2 | [02_add_tools.py](02_add_tools.py) | Define a function tool with `@tool` and attach it to an agent. |
| 3 | [03_multi_turn.py](03_multi_turn.py) | Keep conversation history across turns with `AgentThread`. |
| 3a | [03a_multi_turn.py](03a_multi_turn.py) | Keep conversation history across turns with `AgentSession`. |
| 3b | [03b_multi_turn_streaming.py](03b_multi_turn_streaming.py) | Multi-turn conversations with streaming responses. |
| 4 | [04_memory.py](04_memory.py) | Add dynamic context with a custom `ContextProvider`. |
| 5 | [05_first_workflow.py](05_first_workflow.py) | Chain executors into a workflow with edges. |
| 6 | [06_host_your_agent.py](06_host_your_agent.py) | Host a single agent with Azure Functions. |
Expand Down
3 changes: 2 additions & 1 deletion python/samples/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,8 @@ Start with `01-get-started/` and work through the numbered files:

1. **[01_hello_agent.py](./01-get-started/01_hello_agent.py)** — Create and run your first agent
2. **[02_add_tools.py](./01-get-started/02_add_tools.py)** — Add function tools with `@tool`
3. **[03_multi_turn.py](./01-get-started/03_multi_turn.py)** — Multi-turn conversations with `AgentThread`
3. **[03a_multi_turn.py](./01-get-started/03a_multi_turn.py)** — Multi-turn conversations with `AgentSession`
3b. **[03b_multi_turn_streaming.py](./01-get-started/03b_multi_turn_streaming.py)** — Multi-turn streaming conversations
4. **[04_memory.py](./01-get-started/04_memory.py)** — Agent memory with `ContextProvider`
5. **[05_first_workflow.py](./01-get-started/05_first_workflow.py)** — Build a workflow with executors and edges
6. **[06_host_your_agent.py](./01-get-started/06_host_your_agent.py)** — Host your agent via Azure Functions
Expand Down
Loading