Error Handling
How Rdakt AI handles detection failures and configuration errors
Rdakt AI is designed to be safe by default. When something goes wrong during detection or anonymization, the middleware's behavior is controlled by the error policy.
Error Policies
warn_and_forward (Default)
If detection or anonymization fails, the middleware:
- Logs the error
- Forwards the original, unanonymized request to the LLM
- Fires the
on_errorcallback if configured
This is fail-open behavior. Your application continues working even if Rdakt AI encounters an unexpected issue.
on_error: warn_and_forwardblock
If detection or anonymization fails, the middleware:
- Logs the error
- Raises the exception
- The request is not sent to the LLM
This is fail-closed behavior. Use this when data leakage is worse than downtime.
on_error: blockAudit Mode
Audit mode is a special mode where Rdakt AI detects PII but does not anonymize it:
mode: auditIn audit mode:
- Detection runs normally
- Entities are logged and callbacks fire
- The original text is forwarded to the LLM unchanged
- No deanonymization occurs on responses
Use audit mode to assess what PII your application sends to LLMs before enabling anonymization.
Error Callback
Handle errors programmatically with the on_error callback:
def handle_error(error: Exception, stage: str) -> None:
print(f"Rdakt error in {stage}: {error}")
# Log to your monitoring system
middleware = RdaktMiddleware(
on_error=handle_error,
)The stage parameter indicates where the error occurred (e.g., "detection", "anonymization").
What Can Go Wrong
| Scenario | Default behavior | With block |
|---|---|---|
| Regex pattern fails | Forward unanonymized | Raise exception |
| NER model not installed | Forward unanonymized | Raise exception |
| Session store unreachable | Forward unanonymized | Raise exception |
| Invalid config value | Raise ValueError at init | Raise ValueError at init |
Configuration validation errors always raise immediately, regardless of error policy — they indicate a programmer mistake, not a runtime condition.