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.
Each custom guardrail is a Python module with up to two functions:
| Function | Error behaviour |
|---|---|
|
Raise this to block the LLM call |
|
Called before the LLM request. Return (optionally modified) messages or raise GuardrailBlockedError |
|
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.
