Python: propagate as_tool() kwargs. Add sample for runtime context with as_tool kwargs and middleware.#2311
Merged
moonbox3 merged 2 commits intomicrosoft:mainfrom Nov 20, 2025
Merged
Conversation
Member
Contributor
There was a problem hiding this comment.
Pull Request Overview
This PR adds support for propagating runtime context (such as API tokens, session data, and tenant IDs) through the as_tool() delegation mechanism. Previously, as_tool() did not support passing kwargs to sub-agents, requiring users to rely on alternative patterns like closures with middleware.
Key changes:
- Enabled kwargs propagation through
as_tool()for hierarchical agent delegation - Added filtering to prevent duplicate
chat_optionskeyword arguments - Added comprehensive test coverage for the new functionality
Reviewed Changes
Copilot reviewed 7 out of 8 changed files in this pull request and generated 2 comments.
Show a summary per file
| File | Description |
|---|---|
| python/packages/core/agent_framework/_agents.py | Modified as_tool() to forward kwargs (except arg_name) to sub-agents; added chat_options filtering in run() and run_stream() methods |
| python/packages/core/agent_framework/_tools.py | Added _forward_runtime_kwargs attribute to AIFunction; updated invoke() and _auto_invoke_function() to conditionally propagate runtime kwargs |
| python/packages/core/agent_framework/observability.py | Added chat_options filtering in trace decorators to prevent duplicate keyword argument errors |
| python/packages/core/tests/core/test_as_tool_kwargs_propagation.py | Added comprehensive test suite covering kwargs forwarding, filtering, nested delegation, streaming, and isolation scenarios |
| python/packages/core/tests/core/test_tools.py | Added test to verify ai_function tools handle additional kwargs correctly |
| python/samples/getting_started/middleware/runtime_context_delegation.py | Added comprehensive sample demonstrating three patterns for runtime context delegation |
| python/samples/README.md | Added documentation entry for the new runtime context delegation sample |
| python/uv.lock | Version bump |
dmytrostruk
approved these changes
Nov 19, 2025
TaoChenOSU
approved these changes
Nov 20, 2025
arisng
pushed a commit
to arisng/agent-framework
that referenced
this pull request
Feb 2, 2026
…with as_tool kwargs and middleware. (microsoft#2311) * as tool kwargs * simplify
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
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
Motivation and Context
This was originally raised in #2251 and then addressed as part of #2290. The question revolved around being able to pass runtime context using
as_tool()which currently doesn't acceptkwargs. As an alternative, one could use a closure around middleware. Added a sample to show this as well.Description
kwargspassed toas_tool()Contribution Checklist