The Init Function for NeMo Guardrails

View as Markdown

If config.py contains an init function, it is called during LLMRails initialization. Use it to set up shared resources and register action parameters.

The init function must be synchronous (def init, not async def init). The framework calls it without await, so an async function would silently do nothing.

Any top-level code in config.py runs at import time, before init() is called. This can be used for provider registration that does not require the LLMRails instance.

Basic Usage

1from nemoguardrails import LLMRails
2
3def init(app: LLMRails):
4 # Initialize database connection
5 db = DatabaseConnection()
6
7 # Register as action parameter (available to all actions)
8 app.register_action_param("db", db)

Registering Action Parameters

Action parameters registered in config.py are automatically injected into actions that declare them. The runtime matches parameters by name, i.e., the parameter name in the action must match the name used during registration.

config.py:

1import os
2
3from nemoguardrails import LLMRails
4
5def init(app: LLMRails):
6 # Initialize shared resources
7 db = DatabaseConnection(host="localhost", port=5432)
8 api_client = ExternalAPIClient(api_key=os.environ.get("API_KEY"))
9
10 # Register as action parameters
11 app.register_action_param("db", db)
12 app.register_action_param("api_client", api_client)

actions.py:

1from nemoguardrails.actions import action
2
3@action()
4async def fetch_user_data(user_id: str, db=None):
5 """The 'db' parameter is injected from config.py."""
6 return await db.get_user(user_id)
7
8@action()
9async def call_external_service(query: str, api_client=None):
10 """The 'api_client' parameter is injected from config.py."""
11 return await api_client.search(query)

Built-in Action Parameters

In addition to parameters you register, the runtime automatically injects these built-in parameters into any action that declares them:

ParameterTypeDescription
configRailsConfigThe full rails configuration object
contextdictThe current conversation context
eventslistThe event history
llmLLM instanceThe main LLM (auto-registered during initialization)
llm_task_managerLLMTaskManagerManages LLM task execution

See Custom Data for details on accessing config.custom_data inside actions.

Accessing the Configuration

The app parameter provides access to the full configuration:

1def init(app: LLMRails):
2 # Access the RailsConfig object
3 config = app.config
4
5 # Access custom data from config.yml
6 custom_settings = config.custom_data
7
8 # Access model configurations
9 models = config.models

Example: Database Connection

1import os
2import psycopg2
3from nemoguardrails import LLMRails
4
5def init(app: LLMRails):
6 # Create connection pool
7 conn = psycopg2.connect(
8 host=os.environ.get("DB_HOST", "localhost"),
9 database=os.environ.get("DB_NAME", "mydb"),
10 user=os.environ.get("DB_USER", "user"),
11 password=os.environ.get("DB_PASSWORD"),
12 )
13
14 app.register_action_param("db_conn", conn)

Example: API Client Initialization

1import os
2import httpx
3from nemoguardrails import LLMRails
4
5def init(app: LLMRails):
6 # Get API key from custom_data in config.yml
7 api_key = os.environ.get("API_KEY") or app.config.custom_data.get("api_key")
8
9 # Create HTTP client with authentication
10 client = httpx.AsyncClient(
11 base_url="https://api.example.com",
12 headers={"Authorization": f"Bearer {api_key}"}
13 )
14
15 app.register_action_param("http_client", client)