-
Notifications
You must be signed in to change notification settings - Fork 3.7k
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
Python: Bug: KernelPlugin.from_directory initialises Python classes with no @kernel_function methods #10280
Comments
Thanks for reporting @Druid-of-Luhn. I agree with your expected behavior. We'll have a look. |
4 tasks
Here's my proposed fix: #10286 |
github-merge-queue bot
pushed a commit
that referenced
this issue
Jan 24, 2025
### Motivation and Context This PR addresses an issue in the `KernelPlugin.from_directory()` method where classes without any `@kernel_function` decorated methods are still being initialized during plugin loading. The fix makes sure that only classes with at least one `@kernel_function` decorated method are instantiated and included as plugins. Classes without `@kernel_function` methods are now skipped entirely. <!-- Thank you for your contribution to the semantic-kernel repo! Please help reviewers and future users, providing the following information: 1. Why is this change required? 2. What problem does it solve? 3. What scenario does it contribute to? 4. If it fixes an open issue, please link to the issue here. --> ### Description This PR: - Updates the `from_python_file` to inspect each class for `@kernel_function` decorated methods before instantiation. - Updates the `add_plugin` method `plugin_name` to make sure it is indeed a string, per the type hint. - Adds a unit test to exercise the new behavior - Closes #10280 <!-- Describe your changes, the overall approach, the underlying design. These notes will help understanding how your code works. Thanks! --> ### Contribution Checklist <!-- Before submitting this PR, please make sure: --> - [X] The code builds clean without any errors or warnings - [X] The PR follows the [SK Contribution Guidelines](https://github.com/microsoft/semantic-kernel/blob/main/CONTRIBUTING.md) and the [pre-submission formatting script](https://github.com/microsoft/semantic-kernel/blob/main/CONTRIBUTING.md#development-scripts) raises no violations - [X] All unit tests pass, and I have added new tests where possible - [X] I didn't break anyone 😄
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Describe the bug
The
KernelPlugin.from_directory
method says (emphasis mine):If a
.py
file is present in the directory, and it has a class with nokernel_function
decorated methods, the class will still be initialised when loading the plugin. Since the class is not expected to be initialised, noclass_init_arguments
are set and it will cause an error.The use case for this is lazily calling
kernel.add_plugin(plugin_name=Path(__file__).parent, parent_directory=Path(__file__).parent.parent)
to pick up aprompts.yaml
file that is sitting next to the code file. The code file that calls this is also picked up as a plugin, and causes an error since its__init__
parameters were not passed.I can and will use
KernelFunctionFromPrompt.from_yaml
instead, since I only want to load the yaml file, but the behaviour still surprised me. I do understand that checking the class definition for decorators before initialising it might be too much/not possible.To Reproduce
Run the following script:
Expected behavior
Since the class has no
kernel_function
decorated methods, I would expect it to be skipped entirely.Screenshots
Stack trace:
Platform
Additional context
The text was updated successfully, but these errors were encountered: