> For clean Markdown of any page, append .md to the page URL.
> For a complete documentation index, see https://docs.nvidia.com/nemo/guardrails/llms.txt.
> For full documentation content, see https://docs.nvidia.com/nemo/guardrails/llms-full.txt.
> For AI client integration (Claude Code, Cursor, etc.), connect to the MCP server at https://docs.nvidia.com/nemo/guardrails/_mcp/server.

# nemoguardrails.cli.migration

## Module Contents

### Functions

| Name                                                                                                                             | Description                                                                                                           |
| -------------------------------------------------------------------------------------------------------------------------------- | --------------------------------------------------------------------------------------------------------------------- |
| [`_add_active_decorator`](#nemoguardrails-cli-migration-_add_active_decorator)                                                   | Adds an '@active' decorator above each flow id in the new lines.                                                      |
| [`_add_imports`](#nemoguardrails-cli-migration-_add_imports)                                                                     | -                                                                                                                     |
| [`_add_main_co_file`](#nemoguardrails-cli-migration-_add_main_co_file)                                                           | Add the main co file to the given file path.                                                                          |
| [`_append_colang_version`](#nemoguardrails-cli-migration-_append_colang_version)                                                 | Appends the Colang version "2.x" at the end of the config file.                                                       |
| [`_comment_rails_flows_in_config`](#nemoguardrails-cli-migration-_comment_rails_flows_in_config)                                 | -                                                                                                                     |
| [`_confirm_and_tag_replace`](#nemoguardrails-cli-migration-_confirm_and_tag_replace)                                             | -                                                                                                                     |
| [`_create_main_co_if_not_exists`](#nemoguardrails-cli-migration-_create_main_co_if_not_exists)                                   | Check if the main co file exists in the directory of the file.                                                        |
| [`_generate_main_flow`](#nemoguardrails-cli-migration-_generate_main_flow)                                                       | Adds a 'main' flow to the new lines that activates all other flows.                                                   |
| [`_generate_rails_flows`](#nemoguardrails-cli-migration-_generate_rails_flows)                                                   | Generates flow definitions from the list of flows.                                                                    |
| [`_get_co_files_to_process`](#nemoguardrails-cli-migration-_get_co_files_to_process)                                             | Get a list of .co files to process.                                                                                   |
| [`_get_config_files_to_process`](#nemoguardrails-cli-migration-_get_config_files_to_process)                                     | Get a list of .yaml or .yml files to process.                                                                         |
| [`_get_flow_ids`](#nemoguardrails-cli-migration-_get_flow_ids)                                                                   | Returns the flow ids in the content.                                                                                  |
| [`_get_flow_ids_from_newlines`](#nemoguardrails-cli-migration-_get_flow_ids_from_newlines)                                       | Returns the flow ids in the new lines.                                                                                |
| [`_get_rails_flows`](#nemoguardrails-cli-migration-_get_rails_flows)                                                             | Extracts the list of flows from the raw\_config dictionary.                                                           |
| [`_get_raw_config`](#nemoguardrails-cli-migration-_get_raw_config)                                                               | read the yaml file and get rails key                                                                                  |
| [`_globalize_variable_assignment`](#nemoguardrails-cli-migration-_globalize_variable_assignment)                                 | -                                                                                                                     |
| [`_is_anonymous_flow`](#nemoguardrails-cli-migration-_is_anonymous_flow)                                                         | Checks if the flow is an anonymous flow.                                                                              |
| [`_is_flow`](#nemoguardrails-cli-migration-_is_flow)                                                                             | Checks if the line is a flow definition. it is a flow definition if it starts with "flow" and other words after flow. |
| [`_process_co_files`](#nemoguardrails-cli-migration-_process_co_files)                                                           | Processes the Colang files in the given path.                                                                         |
| [`_process_config_files`](#nemoguardrails-cli-migration-_process_config_files)                                                   | Processes the config files in the given path.                                                                         |
| [`_process_sample_conversation_in_config`](#nemoguardrails-cli-migration-_process_sample_conversation_in_config)                 | Processes the sample\_conversation section in the config file.                                                        |
| [`_read_file_lines`](#nemoguardrails-cli-migration-_read_file_lines)                                                             | -                                                                                                                     |
| [`_remove_files_from_path`](#nemoguardrails-cli-migration-_remove_files_from_path)                                               | Remove files from the path.                                                                                           |
| [`_remove_rails_flows_from_config`](#nemoguardrails-cli-migration-_remove_rails_flows_from_config)                               | -                                                                                                                     |
| [`_revise_anonymous_flow`](#nemoguardrails-cli-migration-_revise_anonymous_flow)                                                 | Revises an anonymous flow by giving it a unique name.                                                                 |
| [`_set_colang_version`](#nemoguardrails-cli-migration-_set_colang_version)                                                       | Sets the 'colang\_version' in the given raw\_config.                                                                  |
| [`_validate_file`](#nemoguardrails-cli-migration-_validate_file)                                                                 | Validates the file after the conversion.                                                                              |
| [`_write_rails_flows_to_file`](#nemoguardrails-cli-migration-_write_rails_flows_to_file)                                         | Writes the rails flows to a file.                                                                                     |
| [`_write_to_file`](#nemoguardrails-cli-migration-_write_to_file)                                                                 | Writes new lines to a file. If the file does not exist, it is created.                                                |
| [`_write_transformed_content_and_rename_original`](#nemoguardrails-cli-migration-_write_transformed_content_and_rename_original) | Writes the transformed content to the file.                                                                           |
| [`convert_colang_1_syntax`](#nemoguardrails-cli-migration-convert_colang_1_syntax)                                               | Converts a co file from v1 format to v2.                                                                              |
| [`convert_colang_2alpha_syntax`](#nemoguardrails-cli-migration-convert_colang_2alpha_syntax)                                     | Convert a co file form v2-alpha to v2-beta                                                                            |
| [`convert_sample_conversation_syntax`](#nemoguardrails-cli-migration-convert_sample_conversation_syntax)                         | Converts the sample\_conversation section from the old format to the new format.                                      |
| [`migrate`](#nemoguardrails-cli-migration-migrate)                                                                               | Migrates all .co files in a directory from the old format to the new format.                                          |

### Data

[`_FILES_TO_EXCLUDE_ALPHA`](#nemoguardrails-cli-migration-_FILES_TO_EXCLUDE_ALPHA)

[`_LIBS_USED`](#nemoguardrails-cli-migration-_LIBS_USED)

### API

```python
nemoguardrails.cli.migration._add_active_decorator(
    new_lines: typing.List
) -> typing.List
```

Adds an '@active' decorator above each flow id in the new lines.

**Parameters:**

The lines to add the decorators to.

**Returns:** `List`

The lines with the decorators added.

```python
nemoguardrails.cli.migration._add_imports(
    new_lines: typing.List,
    libraries: typing.List[str]
) -> typing.List
```

```python
nemoguardrails.cli.migration._add_main_co_file(
    file_path: str,
    libraries: typing.Optional[typing.List[str]] = None
) -> bool
```

Add the main co file to the given file path.
Args:
file\_path (str): The path to the file to add the main co file to.
libraries (list\[str]): The list of libraries to import in the main co file.
Returns:
bool: True if the main co file was added successfully, False otherwise.

```python
nemoguardrails.cli.migration._append_colang_version(
    file_path
)
```

Appends the Colang version "2.x" at the end of the config file.

```python
nemoguardrails.cli.migration._comment_rails_flows_in_config(
    file_path
)
```

```python
nemoguardrails.cli.migration._confirm_and_tag_replace(
    line,
    original_line,
    name
)
```

```python
nemoguardrails.cli.migration._create_main_co_if_not_exists(
    file_path
)
```

Check if the main co file exists in the directory of the file.

If the main co file does not exist, it creates an empty main co file in the directory of the given file.

**Parameters:**

The path to the file to check for the main co file.

```python
nemoguardrails.cli.migration._generate_main_flow(
    new_lines: typing.List
) -> typing.List
```

Adds a 'main' flow to the new lines that activates all other flows.

The 'main' flow is added at the beginning of the new lines. It includes an 'activate' command for each flow id found in the new lines.

Examples:
new\_lines = \["flow my\_flow", "flow another\_flow"]
include\_main\_flow(new\_lines)

# '''flow main

# activate my\_flow

# activate another\_flow

**Parameters:**

The new lines to add the main flow to.

**Returns:** `List`

The new lines with the 'main' flow added at the beginning.

```python
nemoguardrails.cli.migration._generate_rails_flows(
    flows
)
```

Generates flow definitions from the list of flows.
Args:
flows (dict): The dictionary of flows.
Returns:
str: The flow definitions.

```python
nemoguardrails.cli.migration._get_co_files_to_process(
    path
)
```

Get a list of .co files to process.

**Parameters:**

The path to the file or directory to process.

**Returns:**

The list of .co files to process.

```python
nemoguardrails.cli.migration._get_config_files_to_process(
    path
)
```

Get a list of .yaml or .yml files to process.

**Parameters:**

The path to the file or directory to process.

**Returns:**

The list of .yaml or .yml files to process.

```python
nemoguardrails.cli.migration._get_flow_ids(
    content: str
) -> typing.List
```

Returns the flow ids in the content.

Args:
content (str): The content to search for flow ids.
Returns:
list: A list of flow ids.

Examples:
content = "flow my\_flow"
get\_flow\_ids(content)

# \['my\_flow']

content = "flow my\_flow is better than
flow another\_flow"
get\_flow\_ids(content)

# \['my\_flow is better than', 'another\_flow']

# flow user and flow bot are not considered as flow ids

content = "flow user express greeting"
get\_flow\_ids(content)

# \[]

```python
nemoguardrails.cli.migration._get_flow_ids_from_newlines(
    new_lines: typing.List
) -> typing.List
```

Returns the flow ids in the new lines.
Args:
new\_lines (list): The new lines to search for flow ids.
Returns:
list: A list of flow ids.
Examples:
new\_lines = \["flow my\_flow is better than", "flow another\_flow"]
get\_flow\_ids\_from\_newlines(new\_lines)

# \['my\_flow is better than', 'another\_flow']

```python
nemoguardrails.cli.migration._get_rails_flows(
    raw_config
)
```

Extracts the list of flows from the raw\_config dictionary.

**Parameters:**

The raw configuration dictionary.

**Returns:**

The list of flows.

```python
nemoguardrails.cli.migration._get_raw_config(
    config_path: str
)
```

read the yaml file and get rails key

```python
nemoguardrails.cli.migration._globalize_variable_assignment(
    line
)
```

```python
nemoguardrails.cli.migration._is_anonymous_flow(
    flow_content: str
) -> bool
```

Checks if the flow is an anonymous flow.

an anonymous flow is a flow that does not start with a name. so it only has define flow without a name.

Returns:
bool: True if the flow is anonymous, False otherwise.
Examples:

flow\_content = "flow  "
is\_anonymous\_flow(flow\_content)

# True

flow\_content = "flow my\_flow"
is\_anonymous\_flow(flow\_content)

# False

**Parameters:**

The content of the flow.

```python
nemoguardrails.cli.migration._is_flow(
    line: str
) -> bool
```

Checks if the line is a flow definition. it is a flow definition if it starts with "flow" and other words after flow.

```python
nemoguardrails.cli.migration._process_co_files(
    co_files_to_process: typing.List[str],
    from_version: typing.Literal['1.0', '2.0-alpha'],
    include_main_flow: bool,
    use_active_decorator: bool,
    validate: bool
) -> int
```

Processes the Colang files in the given path.

It converts the Colang files from the given version to the latest version and writes the changes to the files.

Returns:
int: The total number of files changed.

**Parameters:**

The list of Colang files to process.

The version of the Colang files to convert from.

Whether to include the main flow.

Whether to use the active decorator.

Whether to validate the files.

```python
nemoguardrails.cli.migration._process_config_files(
    config_files_to_process: typing.List[str]
) -> int
```

Processes the config files in the given path.

It extracts the rails flows from the config files and writes them to a new file.

**Parameters:**

The list of config files to process.

**Returns:** `int`

The total number of config files changed.

```python
nemoguardrails.cli.migration._process_sample_conversation_in_config(
    file_path: str
)
```

Processes the sample\_conversation section in the config file.

**Parameters:**

The path to the config file.

```python
nemoguardrails.cli.migration._read_file_lines(
    file_path
)
```

```python
nemoguardrails.cli.migration._remove_files_from_path(
    path,
    filenames: typing.List[str]
)
```

Remove files from the path.

**Parameters:**

The path to the directory to remove files from.

The list of filenames to remove.

```python
nemoguardrails.cli.migration._remove_rails_flows_from_config(
    raw_config
)
```

```python
nemoguardrails.cli.migration._revise_anonymous_flow(
    flow_content: str,
    first_message: str
) -> str
```

Revises an anonymous flow by giving it a unique name.
Args:
flow\_content (str): The content of the anonymous flow.
first\_message (str): The first message in the flow.
Returns:
str: The revised flow content with a unique name.

```python
nemoguardrails.cli.migration._set_colang_version(
    file_path: str,
    version: str
)
```

Sets the 'colang\_version' in the given raw\_config.

**Parameters:**

The path to the config file.

The version to set.

```python
nemoguardrails.cli.migration._validate_file(
    file_path,
    new_lines
)
```

Validates the file after the conversion.

It validates the file by parsing it and checking for any errors.

**Parameters:**

The path to the file to validate.

The new lines of the file after the conversion.

**Raises:**

* `Exception`: If the validation fails.

```python
nemoguardrails.cli.migration._write_rails_flows_to_file(
    file_path,
    rails_flows
)
```

Writes the rails flows to a file.
Args:
file\_path (str): The path to the file to write to.
rails\_flows (list): The rails flows to write to the file.

```python
nemoguardrails.cli.migration._write_to_file(
    file_path,
    new_lines
)
```

Writes new lines to a file. If the file does not exist, it is created.

**Parameters:**

The path to the file to write to.

The new lines to write to the file.

```python
nemoguardrails.cli.migration._write_transformed_content_and_rename_original(
    file_path,
    new_lines,
    co_extension = '.v1.co'
)
```

Writes the transformed content to the file.

```python
nemoguardrails.cli.migration.convert_colang_1_syntax(
    lines: typing.List[str]
) -> typing.List[str]
```

Converts a co file from v1 format to v2.

Returns:
List: The new lines of the file after successful migration.

**Parameters:**

The lines of the file to convert.

```python
nemoguardrails.cli.migration.convert_colang_2alpha_syntax(
    lines: typing.List[str]
) -> typing.List[str]
```

Convert a co file form v2-alpha to v2-beta

Returns:
List (str): The new lines of the file after successful migration.

**Parameters:**

The lines of the file to convert

```python
nemoguardrails.cli.migration.convert_sample_conversation_syntax(
    lines: typing.List[str]
) -> typing.List[str]
```

Converts the sample\_conversation section from the old format to the new format.

**Parameters:**

The lines of the sample\_conversation to convert.

**Returns:** `List[str]`

List\[str]: The new lines of the sample\_conversation after conversion.

```python
nemoguardrails.cli.migration.migrate(
    path,
    include_main_flow = False,
    use_active_decorator: bool = True,
    from_version: typing.Literal['1.0', '2.0-alpha'] = '1.0',
    validate: bool = True
)
```

Migrates all .co files in a directory from the old format to the new format.

**Parameters:**

The path to the directory containing the files to migrate.

Whether to add main flow to the files.

Whether to use the active decorator.

The version of the colang files to convert from. Any of '1.0' or '2.0-alpha'.

Whether to validate the files.

```python
nemoguardrails.cli.migration._FILES_TO_EXCLUDE_ALPHA = ['ccl.co', 'core_flo_library.co']
```

```python
nemoguardrails.cli.migration._LIBS_USED = set()
```