# validation

Python API: `luxonis_ml.utils.validation`

Human-readable rendering of pydantic validation errors.

Pydantic's default `ValidationError` message is accurate but noisy: it repeats the raw error type, the truncated input value, and
a documentation URL for every problem, and it reports union failures once per union member. This module turns such an error into a
short list of
[ValidationProblem](https://docs.luxonis.com/software-v3/ai-inference/model-source/training/luxonis-ml/luxonis-ml-api-reference/utils/validation.md#validation-Classes-ValidationProblem)
entries and renders them either as plain text or as a [rich](https://rich.readthedocs.io/en/stable/) panel.

> **Example**
> ```python
from pydantic import ValidationError
from rich.console import Console

from luxonis_ml.utils.validation import render_validation_error

try:
    MyModel(**data)
except ValidationError as e:
    Console().print(render_validation_error(e))
```

The model that failed is recovered from the error's own traceback. Knowing it lets the formatter tell a field name from a mapping key or from one of pydantic's internal union member tags, and suggest the closest valid field name for a misspelled key. Pass `model` explicitly for an error that arrives without its traceback, or one raised by a `pydantic.TypeAdapter`, which validates something that need not be a model at all.

## Classes

| Name | Description |
| --- | --- |
| [`ValidationProblem`](https://docs.luxonis.com/software-v3/ai-inference/model-source/training/luxonis-ml/luxonis-ml-api-reference/utils/validation.md#validation-Classes-ValidationProblem) | A single validation failure, phrased for a human reader. |

### class `ValidationProblem`

```python
class ValidationProblem
```

A single validation failure, phrased for a human reader.

Attributes

| Name | Type | Description |
| --- | --- | --- |
| `hint` | `str | None` | Optional follow-up suggestion, such as the closest matching field name for a misspelled key. |
| `location` | `str` | Dotted path to the offending value, using `[i]` for sequence indices, e.g. `model.inputs[0].dtype`. Empty for a problem concerning the top-level value itself. |
| `message` | `str` | What is wrong, as a lowercase sentence fragment. A union whose members failed on their own fields spans several lines: a header, then one indented line per alternative. |
| `value` | `str | None` | Optional repr of the offending input value. |

## Functions

| Name | Description |
| --- | --- |
| [`format_validation_error`](https://docs.luxonis.com/software-v3/ai-inference/model-source/training/luxonis-ml/luxonis-ml-api-reference/utils/validation.md#validation-Functions-format_validation_error) | Format a validation error as readable plain text. |
| [`install_excepthook`](https://docs.luxonis.com/software-v3/ai-inference/model-source/training/luxonis-ml/luxonis-ml-api-reference/utils/validation.md#validation-Functions-install_excepthook) | Configure summaries for uncaught validation errors. |
| [`iter_validation_problems`](https://docs.luxonis.com/software-v3/ai-inference/model-source/training/luxonis-ml/luxonis-ml-api-reference/utils/validation.md#validation-Functions-iter_validation_problems) | Convert a validation error into humanized problems. |
| [`render_validation_error`](https://docs.luxonis.com/software-v3/ai-inference/model-source/training/luxonis-ml/luxonis-ml-api-reference/utils/validation.md#validation-Functions-render_validation_error) | Render a validation error as a [rich](https://rich.readthedocs.io/en/stable/) panel. |

### function `format_validation_error`

```python
def format_validation_error(
    error: ValidationError,
    *,
    model: type[BaseModel] | None = None,
    title: str | None = None,
) -> str
```

Format a validation error as readable plain text.

Args

| Name | Type | Description | Default |
| --- | --- | --- | --- |
| `error` | `ValidationError` | The validation error to format. | required |
| `model` | `type[BaseModel] | None` | Model that was validated. Used to resolve error locations and to suggest valid field names for misspelled keys. Defaults to the model recovered from the error's traceback. | `None` |
| `title` | `str | None` | Headline for the message. Defaults to a line naming the model that failed to validate. | `None` |

Returns

| Type | Description |
| --- | --- |
| `str` | A multi-line message with one indented block per problem. |

### function `install_excepthook`

```python
def install_excepthook(*, use_rich: bool = True, enabled: bool = True)
```

Configure summaries for uncaught validation errors.

Wraps `sys.excepthook` with one that first lets the previously installed hook run (so the traceback still shows where the error came from, and crash reporters still see it) and then prints a readable summary of the `ValidationError` as the last thing the reader sees. An existing Luxonis hook is reconfigured, or removed when `enabled` is False. Calling this more than once is harmless.

Args

| Name | Type | Description | Default |
| --- | --- | --- | --- |
| `use_rich` | `bool` | If True, the summary is a `rich` panel. If False, it is plain text. | `True` |
| `enabled` | `bool` | Whether validation error summaries are enabled. | `True` |

### function `iter_validation_problems`

```python
def iter_validation_problems(
    error: ValidationError,
    *,
    model: type[BaseModel] | None = None,
) -> Iterator[ValidationProblem]
```

Convert a validation error into humanized problems.

Union failures reported once per union member are collapsed into a single problem, and duplicate problems are dropped.

Args

| Name | Type | Description | Default |
| --- | --- | --- | --- |
| `error` | `ValidationError` | The validation error to inspect. | required |
| `model` | `type[BaseModel] | None` | Model that was validated. Used to resolve error locations and to suggest valid field names for misspelled keys. Defaults to the model recovered from the error's traceback. | `None` |

Returns

| Type | Description |
| --- | --- |
| `Iterator[ValidationProblem]` |  |

Yields

| Type | Description |
| --- | --- |
|  | One problem per distinct validation failure. |

### function `render_validation_error`

```python
def render_validation_error(
    error: ValidationError,
    *,
    model: type[BaseModel] | None = None,
    title: str | None = None,
) -> RenderableType
```

Render a validation error as a [rich](https://rich.readthedocs.io/en/stable/) panel.

Args

| Name | Type | Description | Default |
| --- | --- | --- | --- |
| `error` | `ValidationError` | The validation error to render. | required |
| `model` | `type[BaseModel] | None` | Model that was validated. Used to resolve error locations and to suggest valid field names for misspelled keys. Defaults to the model recovered from the error's traceback. | `None` |
| `title` | `str | None` | Headline shown in the panel border. Defaults to a line naming the model that failed to validate. | `None` |

Returns

| Type | Description |
| --- | --- |
| `RenderableType` | A renderable suitable for `rich.console.Console.print`. |
