---
sidebar:
  label: roboto.templating.substitution
  order: 0
title: roboto.templating.substitution
---
## Module Contents

### MappingResolver

```python
class roboto.templating.substitution.MappingResolver(values: Mapping[str, Any])
```

`from roboto.templating import MappingResolver`

[Source](https://github.com/roboto-ai/roboto-python-sdk/blob/main/src/roboto/templating/substitution.py#L44-L59)

Variable resolver that reads each placeholder's text from a dictionary keyed by name.

It converts each value with `str()`. A name missing from the dictionary, or whose value is `None`, leaves the placeholder as written.

**Parameters**

- **values** (`Mapping[str, Any]`): Values keyed by placeholder name, dots included, such as `{"dataset.id": "ds_12345"}`.

#### MappingResolver.resolve()

```python
def resolve(name: str) -> Optional[str]
```

[Source](https://github.com/roboto-ai/roboto-python-sdk/blob/main/src/roboto/templating/substitution.py#L57-L59)

**Parameters**

- **name** (`str`)

**Returns**

- `Optional[str]`

### PLACEHOLDER_RE

```python
roboto.templating.substitution.PLACEHOLDER_RE
```

`from roboto.templating import PLACEHOLDER_RE`

[Source](https://github.com/roboto-ai/roboto-python-sdk/blob/main/src/roboto/templating/substitution.py#L10-L10)

Regular expression that matches a `{{name}}` placeholder and captures its name in group 1.

Whitespace inside the braces is allowed. A name starts with a letter or underscore and continues with letters, digits, underscores and dots. A dotted name such as `dataset.id` is one name: the `VariableResolver` decides what it expands to.

### VARIABLE_NAME_RE

```python
roboto.templating.substitution.VARIABLE_NAME_RE
```

`from roboto.templating import VARIABLE_NAME_RE`

[Source](https://github.com/roboto-ai/roboto-python-sdk/blob/main/src/roboto/templating/substitution.py#L18-L18)

Regular expression a template variable's name must match.

It accepts the names `PLACEHOLDER_RE` captures, so every declared variable can be written as a `{{name}}` placeholder. [`TemplateVariable`](/docs/reference/python-sdk/roboto/ai/agent/record#roboto.ai.agent.record.TemplateVariable) checks its `name` against it.

### VariableResolver

```python
class roboto.templating.substitution.VariableResolver
```

`from roboto.templating import VariableResolver`

[Source](https://github.com/roboto-ai/roboto-python-sdk/blob/main/src/roboto/templating/substitution.py#L27-L41)

Bases: `Protocol`

Source of the text `{{name}}` placeholders expand to in `substitute`.

`MappingResolver` reads the text from a dictionary, and [`EventNamespace`](/docs/reference/python-sdk/roboto/domain/triggers/namespace#roboto.domain.triggers.namespace.EventNamespace) reads it from the records of a platform event.

#### VariableResolver.resolve()

```python
def resolve(name: str) -> Optional[str]
```

[Source](https://github.com/roboto-ai/roboto-python-sdk/blob/main/src/roboto/templating/substitution.py#L34-L41)

Return the text a placeholder expands to, or `None` to leave the placeholder as written.

**Parameters**

- **name** (`str`): Name written between `{{` and `}}`, dots included and surrounding whitespace removed, such as `dataset.id`.

**Returns**

- `Optional[str]`

### collect_placeholders()

```python
def roboto.templating.substitution.collect_placeholders(node: Any) -> set[str]
```

`from roboto.templating import collect_placeholders`

[Source](https://github.com/roboto-ai/roboto-python-sdk/blob/main/src/roboto/templating/substitution.py#L62-L77)

Return the name of every `{{name}}` placeholder in the strings of a JSON-compatible value.

A placeholder in a dictionary key raises instead of being collected, because expanding two keys to the same text would merge their entries.

**Parameters**

- **node** (`Any`): Value to scan: a string, or dictionaries and lists nested to any depth. Values of other types hold no placeholders.

**Raises**

- `ValueError`: A dictionary key contains a `{{name}}` placeholder.

**Returns**

- `set[str]`

### substitute()

```python
def roboto.templating.substitution.substitute(
    node: Any,
    resolver: VariableResolver,
) -> Any
```

`from roboto.templating import substitute`

[Source](https://github.com/roboto-ai/roboto-python-sdk/blob/main/src/roboto/templating/substitution.py#L97-L122)

Return a copy of a JSON-compatible value with each `{{name}}` placeholder in its strings expanded.

A string may hold several placeholders, the same one more than once, and other text around them. A placeholder the resolver returns `None` for stays as written, and dictionary keys are never expanded. Expanded text is not scanned again, so a value that itself contains `{{name}}` appears as written. To require every placeholder to expand, check the names `collect_placeholders` returns before calling this.

**Parameters**

- **node** (`Any`): Value to expand: a string, or dictionaries and lists nested to any depth. Values of other types are returned unchanged.
- **resolver** (`VariableResolver`): Resolver that supplies the text each placeholder expands to.

**Returns**

- `Any`

**Usage**

```python
from roboto.templating import MappingResolver, substitute
resolver = MappingResolver({"dataset.id": "ds_12345"})
substitute({"prompt": "Summarize {{dataset.id}} for {{ team }}"}, resolver)
# {'prompt': 'Summarize ds_12345 for {{ team }}'}
```
