DetectHallucination
A heuristic check for two common hallucination signals:
- Uncertainty language: “I think”, “probably”, “as far as I know”, “not sure”.
- Placeholder data:
example.comURLs,user@example.comemails,"John Doe".
It does not check facts. For that, use LlmJudge with a grounding criterion.
Parameters
Section titled “Parameters”| Parameter | Type | Default | Description |
|---|---|---|---|
check_uncertainty |
bool |
True |
Flag hedging language |
check_placeholders |
bool |
True |
Flag placeholder data |
on_fail |
'retry' | 'raise' | 'warn' |
'retry' |
See below |
Examples
Section titled “Examples”from pydantic_ai import Agentfrom pydantic_ai_guardrails import DetectHallucination
agent = Agent( 'openai:gpt-4o', instructions='Answer from the provided records only. Say "unknown" if the records do not say.', capabilities=[DetectHallucination(check_uncertainty=False, on_fail='warn')],)Failure behaviour
Section titled “Failure behaviour”on_fail='retry'(default): raisesModelRetry. The model sees the reason and tries again, up to the agent’s output retry budget (Agent(retries={'output': N})).on_fail='raise': raisesOutputBlockedwith.guardrailand.reason.on_fail='warn': logs to thepydantic_ai_guardrailslogger and returns the output unchanged.