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
guardrails/config/config.ymlwith your model andagent_id:The key part isengine: aigateway, which uses the SDK provider thatinit_guardrailsregisters. -
Initialize guardrails in your app.
This loads your NeMo configuration and returns an
LLMRailsengine ready for sync and async generation. -
Protect an Agno model with guardrails.
Use this model in your Agno agent. Before each response, the model runs a NeMo guardrail check.
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
Configure the four guardrails files:-
Update
config.yml. Include both input and output rails: -
Define the rail flows. Add them in
rails.co: -
Add the main conversation flow. Put it in
main.co: -
Add the prompts. Put the corresponding prompts in
prompts.yml:
Using the NeMo API directly
CLI reference
bb-ai-sdk guardrails init
Scaffolds guardrails/config with template 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