Python: fix: @ai_function doesn't properly handle 'self' param#2266
Merged
eavanvalkenburg merged 3 commits intomicrosoft:mainfrom Nov 19, 2025
Merged
Python: fix: @ai_function doesn't properly handle 'self' param#2266eavanvalkenburg merged 3 commits intomicrosoft:mainfrom
eavanvalkenburg merged 3 commits intomicrosoft:mainfrom
Conversation
Contributor
There was a problem hiding this comment.
Pull Request Overview
This PR adds support for using the @ai_function decorator on class methods, fixing issue #1343. The implementation uses Python's descriptor protocol to properly bind class instances to decorated methods.
Key Changes:
- Implements the
__get__descriptor method to handle method binding when accessing decorated methods from class instances - Adds
_instanceattribute to track bound instances and properly injectselfduring function calls - Ensures that
selfandclsparameters are excluded from the generated parameter schemas
Reviewed Changes
Copilot reviewed 2 out of 2 changed files in this pull request and generated no comments.
| File | Description |
|---|---|
python/packages/core/agent_framework/_tools.py |
Implements descriptor protocol (__get__) for method binding and updates __call__ to inject bound instance |
python/packages/core/tests/core/test_tools.py |
Adds comprehensive tests for decorator usage in classes and verifies shared state behavior across multiple tool instances |
Member
Python Test Coverage Report •
Python Unit Test Overview
|
||||||||||||||||||||||||||||||
moonbox3
approved these changes
Nov 19, 2025
dmytrostruk
approved these changes
Nov 19, 2025
arisng
pushed a commit
to arisng/agent-framework
that referenced
this pull request
Feb 2, 2026
…soft#2266) * Fixes Python: @ai_function doesn't properly handle 'self' param Fixes microsoft#1343 * fix for declaration only funcs * fix mypy
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
Adds the ability to use the
@ai_functiondecorator within a class definition.Adds tests to validate, including tests of cross instance execution of the tools.
Fixes #1343
Description
Contribution Checklist