You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
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.
github-actionsbot
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
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: