docs/decisions/0043-filters-exception-handling.md
In .NET version of Semantic Kernel, when kernel function throws an exception, it will be propagated through execution stack until some code will catch it. To handle exception for kernel.InvokeAsync(function), this code should be wrapped in try/catch block, which is intuitive approach how to deal with exceptions.
Unfortunately, try/catch block is not useful for auto function calling scenario, when a function is called based on some prompt. In this case, when function throws an exception, message Error: Exception while invoking function. will be added to chat history with tool author role, which should provide some context to LLM that something went wrong.
There is a requirement to have the ability to override function result - instead of throwing an exception and sending error message to AI, it should be possible to set some custom result, which should allow to control LLM behavior.
IFunctionFilter interfaceAbstraction:
public interface IFunctionFilter
{
void OnFunctionInvoking(FunctionInvokingContext context);
void OnFunctionInvoked(FunctionInvokedContext context);
// New method
void OnFunctionException(FunctionExceptionContext context);
}
Disadvantages:
IExceptionFilter interfaceNew interface will allow to receive exception objects, cancel exception or rethrowing new type of exception. This option can be also added later as filter on a higher level for global exception handling.
Abstraction:
public interface IExceptionFilter
{
// ExceptionContext class will contain information about actual exception, kernel function etc.
void OnException(ExceptionContext context);
}
Usage:
public class MyFilter : IFunctionFilter, IExceptionFilter
{
public void OnFunctionInvoking(FunctionInvokingContext context) { }
public void OnFunctionInvoked(FunctionInvokedContext context) { }
public void OnException(ExceptionContext context) {}
}
Advantages:
IExceptionFilter API in ASP.NET.Disadvantages:
IFunctionFilter interfaceIn IFunctionFilter.OnFunctionInvoked method, it's possible to extend FunctionInvokedContext model by adding Exception property. In this case, as soon as OnFunctionInvoked is triggered, it will be possible to observe whether there was an exception during function execution.
If there was an exception, users could do nothing and the exception will be thrown as usual, which means that in order to handle it, function invocation should be wrapped with try/catch block. But it will be also possible to cancel that exception and override function result, which should provide more control over function execution and what is passed to LLM.
Abstraction:
public sealed class FunctionInvokedContext : FunctionFilterContext
{
// other properties...
public Exception? Exception { get; private set; }
}
Usage:
public class MyFilter : IFunctionFilter
{
public void OnFunctionInvoking(FunctionInvokingContext context) { }
public void OnFunctionInvoked(FunctionInvokedContext context)
{
// This means that exception occurred during function execution.
// If we ignore it, the exception will be thrown as usual.
if (context.Exception is not null)
{
// Possible options to handle it:
// 1. Do not throw an exception that occurred during function execution
context.Exception = null;
// 2. Override the result with some value, that is meaningful to LLM
context.Result = new FunctionResult(context.Function, "Friendly message instead of exception");
// 3. Rethrow another type of exception if needed - Option 1.
context.Exception = new Exception("New exception");
// 3. Rethrow another type of exception if needed - Option 2.
throw new Exception("New exception");
}
}
}
Advantages:
IActionFilter API in ASP.NET.Disadvantages:
context.Exception = null or context.Exception = new AnotherException(), instead of using native try/catch approach.IFunctionFilter signature by adding next delegate.This approach changes the way how filters work at the moment. Instead of having two Invoking and Invoked methods in filter, there will be only one method that will be invoked during function execution with next delegate, which will be responsible to call next registered filter in pipeline or function itself, in case there are no remaining filters.
Abstraction:
public interface IFunctionFilter
{
Task OnFunctionInvocationAsync(FunctionInvocationContext context, Func<FunctionInvocationContext, Task> next);
}
Usage:
public class MyFilter : IFunctionFilter
{
public async Task OnFunctionInvocationAsync(FunctionInvocationContext context, Func<FunctionInvocationContext, Task> next)
{
// Perform some actions before function invocation
await next(context);
// Perform some actions after function invocation
}
}
Exception handling with native try/catch approach:
public async Task OnFunctionInvocationAsync(FunctionInvocationContext context, Func<FunctionInvocationContext, Task> next)
{
try
{
await next(context);
}
catch (Exception exception)
{
this._logger.LogError(exception, "Something went wrong during function invocation");
// Example: override function result value
context.Result = new FunctionResult(context.Function, "Friendly message instead of exception");
// Example: Rethrow another type of exception if needed
throw new InvalidOperationException("New exception");
}
}
Advantages:
IAsyncActionFilter and IEndpointFilter API in ASP.NET.Invoking/Invoked) - this allows to keep invocation context information in one method instead of storing it on class level. For example, to measure function execution time, Stopwatch can be created and started before await next(context) call and used after the call, while in approach with Invoking/Invoked methods the data should be passed between filter actions in other way, for example setting it on class level, which is harder to maintain.context.Cancel = true). To cancel the operation, simply don't call await next(context).Disadvantages:
await next(context) manually in all filters. If it's not called, next filter in pipeline and/or function itself won't be called.Proceed with Option 4 and apply this approach to function, prompt and function calling filters.