Colang 2.0 Language Reference
This is the complete language reference for Colang 2.0, covering all syntax, semantics, and standard library features.Introduction
Colang is an event-based modeling language for designing highly flexible conversational interactions. Colang 2.0 is designed as a mix of natural language and Python, making it accessible to developers familiar with Python. Colang scripts are interpreted by a Python runtime that is part of NeMo Guardrails.File Structure
- Script: Any Colang code
- Module: A single
.cofile - Package: A folder of
.cofiles, potentially with subfolders
Basic Syntax
Indentation
Colang uses indentation to define code blocks (like Python). Use 2 spaces for indentation (recommended).Comments
Single-line comments start with#:
Docstrings
Flows can have docstrings using triple quotes:Flows
Flow Definition
Flows are the core building blocks. Syntax:Flow Names
Flow names can contain lowercase letters, numbers, underscores, and whitespace characters.The keywords
and, or, and as cannot be used in flow names and must be escaped with a leading underscore (e.g., this _and that).Flow Naming Conventions
-
User flows (events from outside): Use past tense
user saiduser expressed greetinguser clicked button
-
Bot flows (actions to take): Use imperative form
bot saybot express greetingbot refuse to respond
The main Flow
Every Colang script must have amain flow as the entry point:
Flow Parameters
Flows can accept parameters:Return Values
Flows can return values:Flow Events
Flows generate lifecycle events:Started(): When a flow beginsFinished(): When a flow completes successfullyFailed(): When a flow fails
Keywords and Operators
Flow Control Keywords
start
Start a flow or action without waiting:await
Start a flow/action and wait for completion:await keyword is optional when calling flows:
match
Wait for an event:send
Send an event:activate
Activate a flow to run in the background:return
Return a value from a flow:abort
Stop the current flow immediately:Conditional Keywords
if/else
Conditional branching:when/or when
Event-based branching:Colang 2.0 uses
or when instead of Colang 1.0’s else when.Logical Operators
and: Logical ANDor: Logical ORnot: Logical NOT
The Generation Operator (…)
The... operator invokes the LLM for dynamic generation:
The as Keyword
Store a reference:Variables
All variables start with$:
Variable Types
- String:
$name = "Alice" - Integer:
$count = 42 - Float:
$score = 0.95 - Boolean:
$is_safe = True - Complex types: Lists, dictionaries (from action returns)
Global Variables
Useglobal to declare variables accessible across flows:
Expressions
Colang 2.0 supports:- Arithmetic:
$total = $price * $quantity - Comparison:
$is_greater = $a > $b - Array indexing:
$first = $items[0] - Property access:
$email = $user.email - Length function:
$count = len($items) - String interpolation:
$message = "Hello, {$name}!"
Events
Event Structure
Events have a name and optional parameters:Common Events
User Events
UtteranceUserActionFinished(final_transcript="..."): User said somethingGestureUserActionFinished(gesture="..."): User made a gesture
Bot Events
StartUtteranceBotAction(script="..."): Bot should say somethingStartGestureBotAction(gesture="..."): Bot should make a gesture
Flow Events
FlowStarted(flow_id="..."): A flow startedFlowFinished(flow_id="..."): A flow finishedFlowFailed(flow_id="..."): A flow failed
Matching Events
Match specific events:Sending Events
Actions
Actions are external functions that perform operations.Action Events
Actions generate lifecycle events:ActionStarted: When an action beginsActionFinished: When an action completesActionFailed: When an action fails
Using Actions
Start and wait for an action:Parallel Actions
Start multiple actions simultaneously:Imports
Import modules from the standard library or custom packages:Standard Library
core Module
Basic flows for user and bot interactions:llm Module
LLM-driven interaction flows:guardrails Module
Input and output checking:timing Module
Time-based flows:avatars Module
Interactive avatar control:utils Module
Utility flows:Decorators
Colang 2.0 supports decorators for flows.@active Decorator
Mark a flow as active from the start:Advanced Features
Parallel Flows
Multiple flows can run simultaneously:Pattern Matching
Advanced event matching:Flow Lifetime
Flows have a parent-child relationship:- It completes naturally
- Its parent flow stops
- It’s explicitly aborted
Real Examples
Example 1: Simple Greeting
Fromexamples/v2_x/tutorial/hello_world_1/rails.co:
Example 2: LLM-Powered Bot
Fromexamples/v2_x/tutorial/guardrails_1/rails.co:
Example 3: Multi-Modal Interaction
Fromexamples/v2_x/language_reference/actions/dialog_pattern/main.co:
Example 4: Simple ABC Bot
Fromexamples/bots/abc_v2/main.co:
rails.co):
Best Practices
- Use descriptive flow names following naming conventions
- Import required modules at the top of each file
- Define a
mainflow as the entry point - Use
activatefor background flows - Leverage the
...operator for dynamic LLM decisions - Add docstrings to document complex flows
- Use global variables sparingly
- Test flows incrementally during development
- Organize flows by purpose (greetings, safety, capabilities, etc.)
- Handle edge cases with appropriate error flows
Migration from Colang 1.0
Key changes:
See the Migration Guide for details.
Next Steps
Getting Started
Build your first Colang 2.0 bot
Migration Guide
Migrate from Colang 1.0
Overview
Colang 2.0 features and changes
v1 Syntax
Colang 1.0 reference