init_guardrails(config_path): loads your NeMo configuration and returns an initializedLLMRailsGuardedAIGatewayModel: an Agno-compatible model that runs guardrail checks before each model responsebb-ai-sdk guardrails init: scaffolds a starter guardrails configuration
Prerequisites
Install the BB AI SDK with guardrails dependencies:GuardedAIGatewayModel works with Agno-based agents. If you use this wrapper, ensure your project includes Agno. The wrapper supports adding input rails to your Agno agent.Quick start
Scaffold the guardrails configuration
Create a starter guardrails configuration folder:This creates:Optional flags:
Set your guardrails model configuration
Update The key part is
guardrails/config/config.yml with your model and agent_id:engine: aigateway, which uses the SDK provider that init_guardrails registers.Initialize guardrails in your app
LLMRails engine ready for sync and async generation.How runtime blocking works
WhenGuardedAIGatewayModel receives a request:
- It forwards
systemandusermessages to NeMo guardrails. - If NeMo returns the configured refusal message, the SDK returns that refusal immediately.
- If NeMo doesn’t block the request, the SDK forwards it to AI Gateway and the model responds normally.
guardrails.blocked = true
Custom refusal messages
NeMo blocks via thebot refuse to respond flow. You can override that flow in Colang:
GUARDRAILS_REFUSAL_MESSAGE to the exact same text so the SDK can reliably detect blocked responses:
Using NeMo Guardrails API to add input and output rails
Use the NeMoLLMRails API directly with the generate() or generate_async() methods to evaluate user inputs and model responses.
Configuration with input and output rails
Update yourconfig.yml to include both input and output rails:
rails.co:
main.co:
prompts.yml:
Using the NeMo API directly
CLI reference
bb-ai-sdk guardrails init
Scaffolds guardrails/config with template files.
| Option | Description |
|---|---|
--target-dir, -t | Target directory where guardrails/ is created. Default: current directory. |
--force, -f | Overwrite existing files |
Troubleshooting
ImportError for nemoguardrails
ImportError for nemoguardrails
Install the guardrails extra:
Guardrails are not blocking as expected
Guardrails are not blocking as expected
Check both of these:
- Your
config.ymlusesengine: aigateway GUARDRAILS_REFUSAL_MESSAGEexactly matches your custombot refuse to respondoutput
`agent_id` errors in guardrails model calls
`agent_id` errors in guardrails model calls
Verify that you set
agent_id correctly in:guardrails/config/config.yml, for NeMo provider model callsGuardedAIGatewayModel(..., agent_id=...), for protected model calls
Next steps
AI gateway
Learn how model requests are routed through the platform
Observability
Add tracing and monitor blocked requests
Get started
Build your first agent with SDK modules
Evaluation framework
Evaluate behavior and safety outcomes over datasets