Creating Custom Actions
This section describes how to create custom actions in the actions.py file.
The @action Decorator
Use the @action decorator from nemoguardrails.actions to define custom actions:
Decorator Parameters
Custom Action Name
Override the default action name:
Call from Colang:
System Actions
When is_system_action=True, the action always runs locally, even when an actions_server_url is configured. This is important for actions that need access to special parameters like context, llm, config, and events, which are only injected for locally-run actions.
When no actions_server_url is configured, all actions run locally and receive special parameters regardless of the is_system_action setting. The flag only affects behavior when an actions server is in use.
Async Execution
When execute_async=True, the event processing loop does not wait for the action to complete before continuing. The action runs in the background, and the event processing loop retrieves the result later by polling. Use this setting for long-running operations when you do not need the result immediately.
This flag is only supported in the Colang 2.x runtime. In the Colang 1.0 runtime, it is stored in metadata but has no effect.
Rail Decisions
The @action decorator does not interpret an action’s return value as a safety decision. Ordinary custom actions can return strings, booleans, numbers, dictionaries, or other Python values for a Colang flow to consume explicitly.
When the action itself makes a rail decision, return a RailOutcome. It carries an explicit allow, block, or transform decision without relying on implicit boolean or numeric conventions.
If you previously used the removed output_mapping decorator parameter, follow the migration guide.
Function Parameters
Actions can accept parameters of the following types:
Basic Parameters
Call from Colang:
Optional Parameters with Defaults
Return Values
Actions can return various types:
Manifest-backed rail actions are the exception. They must return a RailOutcome.
Simple Return
Dictionary Return
Boolean Return for Flow Logic
A boolean remains application data until a Colang flow interprets it explicitly:
Branch on the value in the flow:
Error Handling
Handle errors gracefully within actions:
Example Actions
Input Validation Action
Add a Colang 1.0 flow to rails.co that consumes the decision:
Enable the flow as an input rail in config.yml:
Output Filtering Action
Add a Colang 1.0 flow to rails.co that consumes the decision:
Enable the flow as an output rail in config.yml:
External API Action
Related Topics
- Built-in Actions - Default actions in the library
- Action Parameters - Special parameters provided automatically
- Registering Actions - Different ways to register actions
- Outbound HTTP - Send external requests through the canonical client boundary