Nugget 18: Unit Test a Semantic Kernel Function - Would the LLM Call it?
During the past few months I have been working, at Dev-Nugget-Labs, on a AI-First application, WORKS Commons, WC.
WC uses LLMs in deterministic workflows as a source of signals. The intelligence and decision making reside in the system, not in the models.
But this Nugget is not about WC, nor about its deterministic workflows, or what they accomplish. This Nugget is about how to Unit Test a Semantic Kernel function.
Why?
Four questions:
- Will it cover the happy path.
- Would the Edge Cases be handled.
- Would the Plugin register with the Kernel correctly.
- Would the Plugin be called.
The Plugin
This is the Semantic Kernel Function the LLM will call based on instructions. The Unit Tests will mimic how LLMs will call the function.
Note that the plugin follows the architecture Microsoft recommends for Semantic Kernel: No business logic lives in the plugin.
This is what is supposed to happen:
LLM → Kernel → Plugin → Service → Repository
This separation ensures reusability: whether exposed via REST, gRPC, or another interface, the service logic stays intact while the plugin remains a thin adapter.
Furthermore, and this is important; if edge cases are not handled by the plugin then the LLM could "hallucinate" issuing "unpredicted" signals. This is what separates a good plugin from one that the system hopes the plugin is doing what it is supposed to.
The Unit Tests
- PluginIntegration_Kernel_Successfully_Registration
- PluginIntegration_WithKernel_ExecutesSuccessfully
- PluginIntegration_Returns_EmptyString_WithKernel
- PluginIntegration_Returns_EmptyString_ForNull_WithKernel
PluginIntegration_Kernel_Successfully_Registration answers the question—Would the plugin register with the Kernel. The unit test asserts the plugin exists after registration.
The main thing I like about the remaining tests is that the plugin is called in the same way the LLM would call it. This is important because it exercises the Semantic Kernel layer.
The plugin could have been called directly but this is not a real integration.
Takeaway
A Semantic Kernel plugin should be tested through the kernel itself, not in isolation. When your unit tests mimic the exact invocation path an LLM will use, you validate registration, edge‑case handling, and deterministic behavior. This protects your workflows from hallucinated signals and preserves the architecture where intelligence lives in the system — not in the model.
Comments
Post a Comment