Skip to main content

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 .co file
  • Package: A folder of .co files, potentially with subfolders
Modules and packages can be imported.

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:
Examples:

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 said
    • user expressed greeting
    • user clicked button
  • Bot flows (actions to take): Use imperative form
    • bot say
    • bot express greeting
    • bot refuse to respond

The main Flow

Every Colang script must have a main flow as the entry point:

Flow Parameters

Flows can accept parameters:
Parameters can have default values:

Return Values

Flows can return values:

Flow Events

Flows generate lifecycle events:
  • Started(): When a flow begins
  • Finished(): When a flow completes successfully
  • Failed(): When a flow fails
Match flow events:
Or match by flow name:

Keywords and Operators

Flow Control Keywords

start

Start a flow or action without waiting:

await

Start a flow/action and wait for completion:
The await keyword is optional when calling flows:

match

Wait for an event:
Match with parameters:
Match and capture:

send

Send an event:

activate

Activate a flow to run in the background:
Activated flows watch for their patterns throughout the conversation.

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 AND
  • or: Logical OR
  • not: Logical NOT

The Generation Operator (…)

The ... operator invokes the LLM for dynamic generation:
Example:
The LLM evaluates the instruction and returns the result.

The as Keyword

Store a reference:
Capture event data:

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

Use global 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 something
  • GestureUserActionFinished(gesture="..."): User made a gesture

Bot Events

  • StartUtteranceBotAction(script="..."): Bot should say something
  • StartGestureBotAction(gesture="..."): Bot should make a gesture

Flow Events

  • FlowStarted(flow_id="..."): A flow started
  • FlowFinished(flow_id="..."): A flow finished
  • FlowFailed(flow_id="..."): A flow failed

Matching Events

Match specific events:
Match any instance:
Match and capture:

Sending Events

Actions

Actions are external functions that perform operations.

Action Events

Actions generate lifecycle events:
  • ActionStarted: When an action begins
  • ActionFinished: When an action completes
  • ActionFailed: When an action fails

Using Actions

Start and wait for an action:
Start without waiting:

Parallel Actions

Start multiple actions simultaneously:

Imports

Import modules from the standard library or custom packages:
Currently, Colang 2.0 only supports module/package-level imports (not individual flows).

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:
This is equivalent to:

Advanced Features

Parallel Flows

Multiple flows can run simultaneously:

Pattern Matching

Advanced event matching:

Flow Lifetime

Flows have a parent-child relationship:
A flow is stopped when:
  • It completes naturally
  • Its parent flow stops
  • It’s explicitly aborted

Real Examples

Example 1: Simple Greeting

From examples/v2_x/tutorial/hello_world_1/rails.co:

Example 2: LLM-Powered Bot

From examples/v2_x/tutorial/guardrails_1/rails.co:

Example 3: Multi-Modal Interaction

From examples/v2_x/language_reference/actions/dialog_pattern/main.co:

Example 4: Simple ABC Bot

From examples/bots/abc_v2/main.co:
With rails (rails.co):

Best Practices

  1. Use descriptive flow names following naming conventions
  2. Import required modules at the top of each file
  3. Define a main flow as the entry point
  4. Use activate for background flows
  5. Leverage the ... operator for dynamic LLM decisions
  6. Add docstrings to document complex flows
  7. Use global variables sparingly
  8. Test flows incrementally during development
  9. Organize flows by purpose (greetings, safety, capabilities, etc.)
  10. 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