Skip to content
SkPy

Semantic Kernel Middleware (Python)

Guardrails, redaction, approval and telemetry in Semantic Kernel Python using kernel filters.

Verified against semantic-kernel 1.45.0 (checked 2026-11-19).

Semantic Kernel’s middleware mechanism is filters: async functions that wrap a step and call await next(context) to continue. There are three kinds:

Filter typeWrapsTypical uses
FilterTypes.PROMPT_RENDERINGRendering a prompt template, before it is sentRedaction, prompt-injection checks, adding policy text (variables are already HTML-encoded by the template; see input encoding)
FilterTypes.FUNCTION_INVOCATIONEvery kernel function call (prompt or native)Logging, timing, caching, input validation, output redaction
FilterTypes.AUTO_FUNCTION_INVOCATIONEach function the model asks to call during function callingApproval gates, blocking tools, stopping the tool loop

Filters run for agents too, because agents invoke functions through their kernel.

import re
from semantic_kernel import Kernel
from semantic_kernel.filters import FilterTypes, PromptRenderContext
kernel = Kernel()
SSN = re.compile(r"\b\d{3}-\d{2}-\d{4}\b")
@kernel.filter(FilterTypes.PROMPT_RENDERING)
async def redact_prompt(context: PromptRenderContext, next):
await next(context) # render first
context.rendered_prompt = SSN.sub("***-**-****", context.rendered_prompt or "")

Output redaction and timing on function calls

Section titled “Output redaction and timing on function calls”
import time, logging
from semantic_kernel.filters import FunctionInvocationContext
from semantic_kernel.functions import FunctionResult
log = logging.getLogger("sk")
@kernel.filter(FilterTypes.FUNCTION_INVOCATION)
async def time_and_redact(context: FunctionInvocationContext, next):
start = time.perf_counter()
try:
await next(context)
finally:
log.info("%s.%s took %.0f ms", context.function.plugin_name, context.function.name,
(time.perf_counter() - start) * 1000)
if context.result is not None:
# Native functions return their own value; prompt functions return chat message
# content objects — str() gives the text in both cases.
context.result = FunctionResult(
function=context.function.metadata,
value=SSN.sub("***-**-****", str(context.result)),
)

To short-circuit — for example a cache hit — set context.result and return without calling next.

from semantic_kernel.filters import AutoFunctionInvocationContext
SENSITIVE = {"delete_account", "send_payment"}
@kernel.filter(FilterTypes.AUTO_FUNCTION_INVOCATION)
async def require_approval(context: AutoFunctionInvocationContext, next):
if context.function.name in SENSITIVE and not await approved_by_human(context):
context.function_result = FunctionResult(
function=context.function.metadata,
value="The user declined this action. Do not retry it.",
)
return # skip the call; the model sees the message
await next(context)
async def approved_by_human(context: AutoFunctionInvocationContext) -> bool:
print(f"Approve {context.function.name}({dict(context.arguments or {})})? [y/N]")
return input().strip().lower() == "y"

Set context.terminate = True to stop the whole function-calling loop instead of just skipping one call.

  • Filters of the same type run in registration order, each wrapping the next (onion model).
  • kernel.add_filter(FilterTypes.FUNCTION_INVOCATION, fn) is the non-decorator form; kernel.remove_filter(...) removes one.
  • Build a fresh kernel per request (or per tenant) if filters carry request-specific state.

Semantic Kernel emits OpenTelemetry spans for function invocations and model calls, and a duration histogram per function (Python has no token-usage metric; the production guide records usage from a filter). Configure a tracer provider and exporter as usual, then add attributes from a filter:

from opentelemetry import trace
@kernel.filter(FilterTypes.FUNCTION_INVOCATION)
async def tag_span(context: FunctionInvocationContext, next):
span = trace.get_current_span()
span.set_attribute("app.tenant", (context.arguments or {}).get("tenant", "unknown"))
await next(context)

Set SEMANTICKERNEL_EXPERIMENTAL_GENAI_ENABLE_OTEL_DIAGNOSTICS=true to emit GenAI semantic-convention spans for model calls (..._SENSITIVE=true also records prompts and completions — development only).

  • Filters are plain async functions: call them directly with a hand-built context and a fake next.
  • For integration tests, register the filters on a kernel whose chat service is a stub and assert on what reaches the service.

DateVersionChanges
2026-10-211.45.0Rewritten on the current filter API; redaction, timing and the approval gate run against a stub chat service.