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:Real Example: User Intents
Fromexamples/configs/jailbreak_detection/flows.co:
Bot Messages
Bot message definition blocks define the utterances that should be associated with various bot message canonical forms:Bot Messages with Variables
You can include variables in bot messages:For advanced use cases, you can use other Jinja features like
{% if ... %} ... {% endif %}.Real Example: Bot Responses
Fromexamples/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
Fromexamples/bots/hello_world/rails.co:
Flows with Branching
Flows can contain conditional logic usingif and when:
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
Fromexamples/bots/hello_world/rails.co:
Subflows
Subflows are reusable pieces of conversational logic that must be called explicitly: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
Real Example: Action Execution
Keywords
Key Colang 1.0 keywords:define: Start a definition blockuser: Prefix for user message definitionsbot: Prefix for bot message definitionsflow: Define a conversation flowsubflow: Define a reusable subflowdo: Invoke a subflowexecute: Call a Python actionif/else: Conditional branchingwhen/else when: Event-based branchingstop: Halt flow execution
Comments
Use# for single-line comments:
Docstrings
Use triple quotes for flow documentation:Real-World Example: Safety Rails
Fromexamples/bots/abc/rails/disallowed.co:
Best Practices
- Use two-space indentation for consistency
- Name flows descriptively to indicate their purpose
- Group related definitions together in the same file
- Use subflows for reusable logic
- Add comments and docstrings to explain complex flows
- Keep user utterances varied to improve LLM matching
- Test flows thoroughly with different user inputs
Next Steps
Flows
Deep dive into flow patterns and advanced techniques
Examples
See complete real-world examples