> For the complete documentation index, see [llms.txt](https://docs.fusion.vectra.ai/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://docs.fusion.vectra.ai/api-recipes/recipes/sanitize-context-label-values.md).

# 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
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## 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, and the optional `goal` query parameter:

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

`ask` is the immediate question: it should be specific, self-contained, and written in natural language.
`goal` is optional and describes the broader end goal you are ultimately trying to accomplish on behalf of the user. GitBook uses it to tailor the answer towards what is most useful for that goal.

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.
