> 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

Python code example of how to ensure invalid characters are not part of a context label value being sent to the context labels API.

{% 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 by asking a question.

Perform an HTTP GET request on the following URL with the `ask` and `goal` query parameters:

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

`ask` is the immediate question: it should be specific, self-contained, and written in natural language.
`goal` is what the user is ultimately trying to achieve, the reason they need the answer. Sharing it helps GitBook give you a better, more relevant answer. A goal is most helpful when it describes the outcome the user wants rather than restating the question. For example, with `ask=how do I create an API token`, a goal like `automate deployments from our CI pipeline` lets GitBook tailor the answer to that use case.

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.
