Action Decorator
The @action decorator is used to mark functions or classes as actions that can be called from Colang flows.
action
def action (
is_system_action : bool = False ,
name : Optional[ str ] = None ,
execute_async : bool = False ,
output_mapping : Optional[Callable[[Any], bool ]] = None
) -> Callable[[T], T]
Flag indicating if the action is a system action.
The name to associate with the action. If not provided, uses the function name.
Whether the function should be executed in async mode.
output_mapping
Optional[Callable[[Any], bool]]
A function to interpret the action’s result. It accepts the return value and returns True if the output is not safe.
Example - Basic Action
Example - Async Action
Example - Action with Output Mapping
Example - System Action
from nemoguardrails.actions import action
@action ()
def get_weather ( city : str ) -> str :
"""Get weather information for a city."""
return f "The weather in { city } is sunny."
ActionResult
Data class representing the result of an action execution.
@dataclass
class ActionResult :
return_value: Optional[Any] = None
events: Optional[List[ dict ]] = None
context_updates: Optional[ dict ] = field( default_factory = dict )
The value returned by the action.
The events that should be added to the event stream.
Updates made to the context by this action.
Example - Returning ActionResult
Example - Generating Events
from nemoguardrails.actions import action, ActionResult
@action ()
def process_order ( order_id : str ) -> ActionResult:
"""Process an order and update context."""
# Process the order
status = "completed"
return ActionResult(
return_value = { "order_id" : order_id, "status" : status},
events = [{ "type" : "OrderProcessed" , "order_id" : order_id}],
context_updates = { "last_order_id" : order_id}
)
Action Parameters
Actions can access various parameters automatically injected by the runtime:
The current context dictionary containing variables.
The rails configuration object.
The knowledge base instance (if configured).
Example - Using Injected Parameters
Example - Using LLM in Action
from nemoguardrails.actions import action
from nemoguardrails import RailsConfig
@action ()
def custom_search ( query : str , context : dict , kb , config : RailsConfig) -> str :
"""Search knowledge base with context awareness."""
user_name = context.get( "user_name" , "User" )
# Search the knowledge base
results = kb.search(query, max_results = config.custom_data.get( "max_results" , 5 ))
return f "Hello { user_name } , here are the results: { results } "
Registering Actions
Actions can be registered with the LLMRails instance:
Example - Register Action
Example - Register with Custom Name
from nemoguardrails import RailsConfig, LLMRails
from nemoguardrails.actions import action
@action ()
def my_custom_action ( param : str ) -> str :
return f "Processed: { param } "
config = RailsConfig.from_path( "config" )
rails = LLMRails(config)
rails.register_action(my_custom_action)
Using Actions in Colang
Once registered, actions can be called from Colang flows:
Example - Call Action
Example - Action with Result
define flow handle weather query
user expressed interest in weather
$city = "New York"
$weather = execute get_weather(city=$city)
bot $weather
Best Practices
Use Type Hints : Always use type hints for action parameters and return values
Handle Errors : Use try-except blocks to handle potential errors gracefully
Return ActionResult : Use ActionResult when you need to update context or emit events
Async When Needed : Use execute_async=True for I/O-bound operations
Document Actions : Provide clear docstrings explaining what the action does