Output Guardrail
Validate and constrain agent output with guardrails.
Output Guardrail.
1"""2Output Guardrail3=============================45Output Guardrail.6"""78from kern.agent import Agent9from kern.exceptions import CheckTrigger, OutputCheckError10from kern.models.openai import OpenAIResponses11from kern.run.agent import RunOutput121314def enforce_non_empty_output(run_output: RunOutput) -> None:15 """Reject empty or very short responses."""16 content = (run_output.content or "").strip()17 if len(content) < 20:18 raise OutputCheckError(19 "Output is too short to be useful.",20 check_trigger=CheckTrigger.OUTPUT_NOT_ALLOWED,21 )222324# ---------------------------------------------------------------------------25# Create Agent26# ---------------------------------------------------------------------------27agent = Agent(28 name="Output-Checked Agent",29 model=OpenAIResponses(id="gpt-5.2"),30 post_hooks=[enforce_non_empty_output],31)3233# ---------------------------------------------------------------------------34# Run Agent35# ---------------------------------------------------------------------------36if __name__ == "__main__":37 agent.print_response("Summarize the key ideas in clean architecture.", stream=True)Run the Example
1# Clone and setup repo2git clone https://github.com/kern-ai/kern.git3cd kern/cookbook/02_agents/08_guardrails45# Create and activate virtual environment6./scripts/demo_setup.sh7source .venvs/demo/bin/activate89python output_guardrail.py