Skip to main content

Colang 1.0 Syntax

This document provides a comprehensive guide to Colang 1.0 syntax, covering all core language elements.

Syntax Overview

Colang has a “pythonic” syntax where indentation is used as a syntactic element. The core syntax elements are:
  • Blocks: User messages, bot messages, and flows
  • Statements: Instructions within blocks
  • Expressions: Calculations and evaluations
  • Keywords: Special language constructs
  • Variables: Context storage with $ prefix
The recommended indentation in Colang is two spaces, not four.

User Messages

User message definition blocks define the canonical form that should be associated with various user utterances:
Each utterance example helps the LLM recognize when the user is expressing that intent.

Real Example: User Intents

From examples/configs/jailbreak_detection/flows.co:

Bot Messages

Bot message definition blocks define the utterances that should be associated with various bot message canonical forms:
If more than one utterance is specified, one will be chosen randomly.

Bot Messages with Variables

You can include variables in bot messages:
Alternatively, use Jinja syntax:
For advanced use cases, you can use other Jinja features like {% if ... %} ... {% endif %}.

Real Example: Bot Responses

From examples/bots/abc/rails/disallowed.co:

Flows

Flows represent how you want the conversation to unfold. They include sequences of user messages, bot messages, and potentially other events.

Basic Flow

Real Example: Simple Flow

From examples/bots/hello_world/rails.co:

Flows with Branching

Flows can contain conditional logic using if and when:
The if/else statement evaluates expressions with context variables. The when/else when statement branches based on the next user message or event.

When/Else When Pattern

Real Example: Off-Topic Rails

From examples/bots/hello_world/rails.co:

Subflows

Subflows are reusable pieces of conversational logic that must be called explicitly:
Invoke subflows using the do keyword followed by the subflow name.

Variables

References to context variables always start with a $ sign, e.g., $name. All variables are global and accessible in all flows.

Setting Variables

Variable Types

Variables are dynamically typed and can be:
  • Booleans: $is_authenticated = True
  • Integers: $count = 5
  • Floats: $score = 0.95
  • Strings: $name = "Alice"
  • Complex types (lists, dictionaries) from action return values

Using Variables in Conditions

Expressions

Expressions can be used to set values for context variables.

Supported Expressions

  • Arithmetic operations: $total = $price * $quantity
  • Array indexing: $first_item = $items[0]
  • Length function: $count = len($items)
  • Property accessor: $user_name = $user.name

Examples

Actions

Actions are custom functions available to be invoked from flows. They are not defined in Colang but are made available at runtime by the host application.

Executing Actions

All action parameters must be passed as keyword arguments (like in Python).

Real Example: Action Execution

Keywords

Key Colang 1.0 keywords:
  • define: Start a definition block
  • user: Prefix for user message definitions
  • bot: Prefix for bot message definitions
  • flow: Define a conversation flow
  • subflow: Define a reusable subflow
  • do: Invoke a subflow
  • execute: Call a Python action
  • if/else: Conditional branching
  • when/else when: Event-based branching
  • stop: Halt flow execution

Comments

Use # for single-line comments:

Docstrings

Use triple quotes for flow documentation:

Real-World Example: Safety Rails

From examples/bots/abc/rails/disallowed.co:

Best Practices

  1. Use two-space indentation for consistency
  2. Name flows descriptively to indicate their purpose
  3. Group related definitions together in the same file
  4. Use subflows for reusable logic
  5. Add comments and docstrings to explain complex flows
  6. Keep user utterances varied to improve LLM matching
  7. Test flows thoroughly with different user inputs

Next Steps

Flows

Deep dive into flow patterns and advanced techniques

Examples

See complete real-world examples