-
Notifications
You must be signed in to change notification settings - Fork 1.3k
Python: Add propagate_session to as_tool() for session sharing in agent-as-tool scenarios #4439
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
+191
−5
Merged
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
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
93 changes: 93 additions & 0 deletions
93
python/samples/02-agents/tools/agent_as_tool_with_session_propagation.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,93 @@ | ||
| # Copyright (c) Microsoft. All rights reserved. | ||
|
|
||
| import asyncio | ||
| from collections.abc import Awaitable, Callable | ||
|
|
||
| from agent_framework import AgentContext, AgentSession | ||
| from agent_framework.openai import OpenAIResponsesClient | ||
| from dotenv import load_dotenv | ||
|
|
||
| load_dotenv() | ||
|
|
||
| """ | ||
| Agent-as-Tool: Session Propagation Example | ||
| Demonstrates how to share an AgentSession between a coordinator agent and a | ||
| sub-agent invoked as a tool using ``propagate_session=True``. | ||
| When session propagation is enabled, both agents share the same session object, | ||
| including session_id and the mutable state dict. This allows correlated | ||
| conversation tracking and shared state across the agent hierarchy. | ||
| The middleware functions below are purely for observability — they are NOT | ||
| required for session propagation to work. | ||
| """ | ||
|
|
||
|
|
||
| async def log_session( | ||
| context: AgentContext, | ||
| call_next: Callable[[], Awaitable[None]], | ||
| ) -> None: | ||
| """Agent middleware that logs the session received by each agent. | ||
| NOT required for session propagation — only used to observe the flow. | ||
| If propagation is working, both agents will show the same session_id. | ||
| """ | ||
| session: AgentSession | None = context.session | ||
| agent_name = context.agent.name or "unknown" | ||
| session_id = session.session_id if session else None | ||
| state = dict(session.state) if session else {} | ||
| print(f" [{agent_name}] session_id={session_id}, state={state}") | ||
| await call_next() | ||
|
|
||
|
|
||
| async def main() -> None: | ||
| print("=== Agent-as-Tool: Session Propagation ===\n") | ||
|
|
||
| client = OpenAIResponsesClient() | ||
|
|
||
| # --- Sub-agent: a research specialist --- | ||
| # The sub-agent has the same log_session middleware to prove it receives the session. | ||
| research_agent = client.as_agent( | ||
| name="ResearchAgent", | ||
| instructions="You are a research assistant. Provide concise answers.", | ||
| middleware=[log_session], | ||
| ) | ||
|
|
||
| # propagate_session=True: the coordinator's session will be forwarded | ||
| research_tool = research_agent.as_tool( | ||
| name="research", | ||
| description="Research a topic and return findings", | ||
| arg_name="query", | ||
| arg_description="The research query", | ||
| propagate_session=True, | ||
| ) | ||
|
|
||
| # --- Coordinator agent --- | ||
| coordinator = client.as_agent( | ||
| name="CoordinatorAgent", | ||
| instructions="You coordinate research. Use the 'research' tool to look up information.", | ||
| tools=[research_tool], | ||
| middleware=[log_session], | ||
| ) | ||
|
|
||
| # Create a shared session and put some state in it | ||
| session = coordinator.create_session() | ||
| session.state["request_source"] = "demo" | ||
| print(f"Session ID: {session.session_id}") | ||
| print(f"Session state before run: {session.state}\n") | ||
|
|
||
| query = "What are the latest developments in quantum computing?" | ||
| print(f"User: {query}\n") | ||
|
|
||
| result = await coordinator.run(query, session=session) | ||
|
|
||
| print(f"\nCoordinator: {result}\n") | ||
| print(f"Session state after run: {session.state}") | ||
| print( | ||
| "\nIf both agents show the same session_id above, session propagation is working." | ||
| ) | ||
|
|
||
|
|
||
| if __name__ == "__main__": | ||
| asyncio.run(main()) |
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.