Skip to main content

Action Decorator

The @action decorator is used to mark functions or classes as actions that can be called from Colang flows.

action

bool
default:"False"
Flag indicating if the action is a system action.
Optional[str]
The name to associate with the action. If not provided, uses the function name.
bool
default:"False"
Whether the function should be executed in async mode.
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.

ActionResult

Data class representing the result of an action execution.
Optional[Any]
The value returned by the action.
Optional[List[dict]]
The events that should be added to the event stream.
Optional[dict]
Updates made to the context by this action.

Action Parameters

Actions can access various parameters automatically injected by the runtime:
dict
The current context dictionary containing variables.
BaseLLM
The main LLM instance.
RailsConfig
The rails configuration object.
KnowledgeBase
The knowledge base instance (if configured).

Registering Actions

Actions can be registered with the LLMRails instance:

Using Actions in Colang

Once registered, actions can be called from Colang flows:

Best Practices

  1. Use Type Hints: Always use type hints for action parameters and return values
  2. Handle Errors: Use try-except blocks to handle potential errors gracefully
  3. Return ActionResult: Use ActionResult when you need to update context or emit events
  4. Async When Needed: Use execute_async=True for I/O-bound operations
  5. Document Actions: Provide clear docstrings explaining what the action does