Skip to content

LLM Judge

The llm_judge guardrail uses another LLM to evaluate response quality against specified criteria.

from pydantic_ai_guardrails.guardrails.output import llm_judge
from pydantic_ai_guardrails import GuardedAgent
from pydantic_ai_guardrails.guardrails.output import llm_judge
guarded_agent = GuardedAgent(
agent,
output_guardrails=[
llm_judge(
criteria='Is the response helpful and accurate?',
threshold=0.7,
),
],
)
ParameterTypeDefaultDescription
criteriastr | list[str]RequiredEvaluation criteria
thresholdfloat0.7Minimum passing score (0.0-1.0)
judge_modelstrSame as agentModel to use for judging
mode'score' | 'binary''score'Evaluation mode
guardrail = llm_judge(
criteria='Is the response helpful and addresses the user question?',
threshold=0.7,
)
guardrail = llm_judge(
criteria=[
'Is the response factually accurate?',
'Is the tone professional?',
'Does it directly answer the question?',
],
threshold=0.7,
)
# Use a faster/cheaper model for judging
guardrail = llm_judge(
criteria='Is this response appropriate?',
judge_model='openai:gpt-4o-mini',
threshold=0.8,
)
# Pass/fail instead of scored
guardrail = llm_judge(
criteria='Does this response contain medical advice?',
mode='binary', # Returns 0 or 1
threshold=0.5, # 0.5 = must pass
)

When triggered, returns:

{
'tripwire_triggered': True,
'message': 'Response failed quality evaluation',
'severity': 'medium',
'metadata': {
'score': 0.45,
'threshold': 0.7,
'criteria': ['Is the response helpful?'],
},
'suggestion': 'Provide a more complete and helpful response that directly addresses the question.',
}
  • Quality assurance: Ensure responses meet quality standards
  • Brand voice: Verify tone and style guidelines
  • Compliance: Check for policy-violating content
  • Accuracy: Evaluate factual correctness
  • Completeness: Ensure thorough answers

LLM Judge works great with auto-retry:

guarded_agent = GuardedAgent(
agent,
output_guardrails=[
llm_judge(
criteria='Is the response professional and helpful?',
threshold=0.8,
),
],
max_retries=2, # Let LLM improve quality
)

The judge’s feedback helps the LLM understand what to improve.

criteria=[
'Does the response address the customer issue?',
'Is the tone empathetic and professional?',
'Are next steps clearly provided?',
]
criteria=[
'Is the explanation technically accurate?',
'Are code examples correct and runnable?',
'Is the language clear and unambiguous?',
]
criteria='Does this response comply with content policies and avoid harmful content?'