# Sanitize context label values

{% stepper %}
{% step %}

### Check that replacement character is valid

If a replacement other than the default \_ is specified, ensure that is valid itself

```python
def sanitize_label(self, label, replacement_char="_", max_label_len=80):
        if not label:
            raise ValueError("Empty label string cannot be sanitized")

        if replacement_char != "_":
            valid_char_pattern = r"^[a-zA-Z0-9 ._/\\\-#~:()]$"
            if not re.match(valid_char_pattern, replacement_char):
                raise ValueError("Invalid replacement character")
```

{% endstep %}

{% step %}

### Strip leading/trailing spaces

Labels can not start or end with blank space

```python
        label = label.strip()
```

{% endstep %}

{% step %}

### Replace invalid characters

If any invalid characters are found, replace with the replacement character specified

```python
        # Include the backslash as an invalid character in the pattern
        invalid_label_chars = re.compile(r"[^a-zA-Z0-9 ._/\\\-#~:()]|\\")
        label = invalid_label_chars.sub(replacement_char, label)
```

{% endstep %}

{% step %}

### Truncate a label at maximum length

If the label is longer than the limit, truncate it

```python
        if len(label) > max_label_len:
            label = label[:max_label_len]

        return label
```

{% endstep %}
{% endstepper %}


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://docs.fusion.vectra.ai/api-recipes/recipes/sanitize-context-label-values.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
