Custom Guardrail

Create custom Python functions for pre-call and post-call filtering to identify and restrict specific patterns or data types

You can write arbitrary Python code to implement guardrail logic that Content Filters and LLM Judge cannot cover, for example, stateful checks, external API calls, business-rule validation, or any other custom logic. These user-defined Python functions enable the detection and filtering of specific programmatic behaviors or data structures as required.

Figure 1. Custom Guardrails tab
Each custom guardrail is a Python module with up to two functions:
Table 1.
Function Error behaviour
class GuardrailBlockedError(Exception):
Raise this to block the LLM call
def pre_call(messages: list[dict], model: str, kwargs: dict) -> list[dict]:
Called before the LLM request. Return (optionally modified) messages or raise GuardrailBlockedError
def post_call(response: dict, messages: list[dict], model: str) -> dict:
Called after the LLM response. Return (optionally modified) response or raise GuardrailBlockedError

Custom guardrails run in an isolated Python virtual environment with their own requirements.txt, and they cannot interfere with the main workflow environment.