Python: Propagate conversation_id in Agent-as-Tool scenarios#3889
Python: Propagate conversation_id in Agent-as-Tool scenarios#3889giles17 wants to merge 4 commits intomicrosoft:mainfrom
conversation_id in Agent-as-Tool scenarios#3889Conversation
conversation_id in Agent-as-Tool scenariosconversation_id in Agent-as-Tool scenarios
Python Test Coverage Report •
Python Unit Test Overview
|
||||||||||||||||||||||||||||||||||||||||||||||||||
There was a problem hiding this comment.
Pull request overview
This PR implements conversation ID propagation in Agent-as-Tool scenarios to enable correlation of multi-agent interactions. When a parent agent invokes a sub-agent as a tool, the parent's conversation_id is now automatically forwarded to the sub-agent via additional_function_arguments with the key parent_conversation_id. This solves issue #3411 where multi-agent conversations couldn't be correlated using a single identifier.
Changes:
- Modified
as_tool()wrapper to extract parent'sconversation_idand pass it to sub-agents viaadditional_function_arguments - Updated
_auto_invoke_functionto forwardconversation_idto tools instead of filtering it out - Added logic to populate
additional_function_arguments["conversation_id"]when chat API returns a conversation ID - Added comprehensive test coverage for the new functionality
- Included a well-documented sample demonstrating the feature
Reviewed changes
Copilot reviewed 10 out of 10 changed files in this pull request and generated 3 comments.
Show a summary per file
| File | Description |
|---|---|
python/packages/core/agent_framework/_agents.py |
Modified as_tool() wrapper to propagate parent's conversation_id to sub-agent via additional_function_arguments with key parent_conversation_id |
python/packages/core/agent_framework/_tools.py |
Removed conversation_id from filter list in _auto_invoke_function to allow forwarding; added conversation_id to additional_function_arguments when received from chat API |
python/packages/core/tests/core/test_as_tool_kwargs_propagation.py |
Added tests for conversation_id propagation via options and handling when absent |
python/packages/core/tests/core/test_agents.py |
Added tests for conversation_id propagation in agent-as-tool scenarios |
python/packages/core/tests/core/test_tools.py |
Added tests for conversation_id forwarding and internal kwargs filtering |
python/samples/getting_started/tools/agent_as_tool_conversation_id_propagation.py |
New comprehensive sample demonstrating conversation_id propagation with observability middleware |
python/samples/getting_started/tools/README.md |
Added entry documenting the new sample |
python/packages/core/agent_framework/_mcp.py |
Code formatting improvements (multi-line json.dumps calls) |
python/packages/core/agent_framework/azure/_responses_client.py |
Code formatting improvements (line breaking) |
python/packages/core/tests/core/test_mcp.py |
Alphabetical import reordering |
| # start its own conversation, not try to continue the parent's. | ||
| # Merge into any existing options/additional_function_arguments to avoid replacing them. | ||
| existing_options = kwargs.get("options") | ||
| run_options: dict[str, Any] | None = dict(existing_options) if isinstance(existing_options, dict) else None |
There was a problem hiding this comment.
If we're doing all these extra dict calls only for typing, then we really shouldn't be going this, ignore instead or actually raise a issue if the value isn't right, but this is either does nothing, or it will raise a strange error because it can't create the dict. This goes for all of these later on as well.
Motivation and Context
When using the Agent-as-Tool pattern (a coordinator agent invoking sub-agents as tools), the parent agent's
conversation_idis not propagated to sub-agents. This makes it impossible to correlate the full multi-agent interaction chain using a single identifier for persistence or observability.Solution
Leverage the existing
**kwargsforwarding pipeline (additional_function_arguments→_auto_invoke_function→ tool**kwargs) to propagateconversation_idfrom parent to child agents.Closes #3411
Description
Contribution Checklist