Skip to content

Python: fix: @ai_function doesn't properly handle 'self' param#2266

Merged
eavanvalkenburg merged 3 commits intomicrosoft:mainfrom
eavanvalkenburg:fix_1343
Nov 19, 2025
Merged

Python: fix: @ai_function doesn't properly handle 'self' param#2266
eavanvalkenburg merged 3 commits intomicrosoft:mainfrom
eavanvalkenburg:fix_1343

Conversation

@eavanvalkenburg
Copy link
Member

Motivation and Context

Adds the ability to use the @ai_function decorator within a class definition.
Adds tests to validate, including tests of cross instance execution of the tools.

Fixes #1343

Description

Contribution Checklist

  • The code builds clean without any errors or warnings
  • The PR follows the Contribution Guidelines
  • All unit tests pass, and I have added new tests where possible
  • Is this a breaking change? If yes, add "[BREAKING]" prefix to the title of the PR.

Copy link
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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 _instance attribute to track bound instances and properly inject self during function calls
  • Ensures that self and cls parameters 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

@markwallace-microsoft
Copy link
Member

markwallace-microsoft commented Nov 17, 2025

Python Test Coverage

Python Test Coverage Report •
FileStmtsMissCoverMissing
packages/core/agent_framework
   _tools.py6176988%225, 271, 322, 324, 487, 519–520, 621, 623, 636, 654, 668, 680, 685, 687, 694, 727, 776–778, 816, 839, 841–850, 859–865, 893, 903, 941, 950, 952, 955, 961, 1328–1332, 1432, 1501, 1602, 1608, 1649–1650, 1782, 1821–1822, 1850–1852, 1887–1888, 1951–1952, 1959–1960
TOTAL14985222885% 

Python Unit Test Overview

Tests Skipped Failures Errors Time
2041 129 💤 0 ❌ 0 🔥 39.985s ⏱️

@eavanvalkenburg eavanvalkenburg changed the title Python: @ai_function doesn't properly handle 'self' param Python: fix: @ai_function doesn't properly handle 'self' param Nov 17, 2025
@eavanvalkenburg eavanvalkenburg added this pull request to the merge queue Nov 19, 2025
Merged via the queue into microsoft:main with commit 34a00f1 Nov 19, 2025
23 checks passed
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
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

Status: Done

Development

Successfully merging this pull request may close these issues.

Python: @ai_function doesn't properly handle 'self' param

5 participants