Skip to content
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

Open
Druid-of-Luhn opened this issue Jan 23, 2025 · 2 comments · May be fixed by #10286
Labels
bug Something isn't working kernel Issues or pull requests impacting the core kernel python Pull requests for the Python Semantic Kernel

Comments

@Druid-of-Luhn
Copy link

Describe the bug

The KernelPlugin.from_directory method says (emphasis mine):

A .py file is parsed and a plugin created,
the functions within as [sic] then combined with any other functions found.
The python file needs to contain a class with one or more kernel_function decorated methods.
If this class has a __init__ method, it will be called with the arguments provided in the
class_init_arguments dictionary, the key needs to be the same as the name of the class,
with the value being a dictionary of arguments to pass to the class (using kwargs).

If a .py file is present in the directory, and it has a class with no kernel_function decorated methods, the class will still be initialised when loading the plugin. Since the class is not expected to be initialised, no class_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 a prompts.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:

from pathlib import Path
from semantic_kernel import Kernel

class Example:
    def __init__(self, kernel):
        this_directory = Path(__file__).parent
        kernel.add_plugin(plugin_name=this_directory, parent_directory=this_directory.parent)

if __name__ == "__main__":
    Example(Kernel())

Expected behavior

Since the class has no kernel_function decorated methods, I would expect it to be skipped entirely.

Screenshots

Stack trace:

Traceback (most recent call last):
  File "C:\Users\...\test.py", line 10, in <module>
    Example(Kernel())
  File "C:\Users\...\test.py", line 7, in __init__
    kernel.add_plugin(plugin_name=this_directory, parent_directory=this_directory.parent)
  File "C:\Users\...\.venv\Lib\site-packages\semantic_kernel\functions\kernel_function_extension.py", line 95, in add_plugin
    self.plugins[plugin_name] = KernelPlugin.from_directory(
                                ^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\Users\...\.venv\Lib\site-packages\semantic_kernel\functions\kernel_plugin.py", line 325, in from_directory
    cls.from_python_file(
  File "C:\Users\...\.venv\Lib\site-packages\semantic_kernel\functions\kernel_plugin.py", line 401, in from_python_file
    instance = getattr(module, name)(**class_init_arguments.get(name, {}) if class_init_arguments else {})
               ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
TypeError: Example.__init__() missing 1 required positional argument: 'kernel'

Platform

  • OS: Windows
  • IDE: gVim + PowerShell
  • Language: Python
  • Source: pip package version 1.19.0

Additional context

@Druid-of-Luhn Druid-of-Luhn added the bug Something isn't working label Jan 23, 2025
@markwallace-microsoft markwallace-microsoft added python Pull requests for the Python Semantic Kernel triage labels Jan 23, 2025
@github-actions github-actions bot changed the title Bug: KernelPlugin.from_directory initialises Python classes with no @kernel_function methods Python: Bug: KernelPlugin.from_directory initialises Python classes with no @kernel_function methods Jan 23, 2025
@moonbox3
Copy link
Contributor

Thanks for reporting @Druid-of-Luhn. I agree with your expected behavior. We'll have a look.

@moonbox3 moonbox3 added kernel Issues or pull requests impacting the core kernel and removed triage labels Jan 23, 2025
@moonbox3 moonbox3 linked a pull request Jan 24, 2025 that will close this issue
4 tasks
@moonbox3
Copy link
Contributor

Here's my proposed fix: #10286

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working kernel Issues or pull requests impacting the core kernel python Pull requests for the Python Semantic Kernel
Projects
Status: No status
Development

Successfully merging a pull request may close this issue.

3 participants