> ## Documentation Index
> Fetch the complete documentation index at: https://fastql.vachagan.dev/llms.txt
> Use this file to discover all available pages before exploring further.

# Pydantic integration

> Derive GraphQL types and inputs from Pydantic models, with validation.

The optional Pydantic integration derives a GraphQL object or input type from a
Pydantic v2 model — mapping its fields, optionality, and defaults — and runs the
model's validators on input coercion. It lives behind the `[pydantic]` extra and is
never imported by the core.

```bash theme={null}
pip install mygenx-fastql[pydantic]
```

```python theme={null}
from pydantic import BaseModel, field_validator
from fastql import Field, Query
from fastql.pydantic import pydantic_input, pydantic_type


class UserModel(BaseModel):
    id: int
    name: str
    nickname: str | None = None


class SignUpInput(BaseModel):
    age: int

    @field_validator("age")
    @classmethod
    def adult(cls, value: int) -> int:
        if value < 18:
            raise ValueError("must be 18 or older")
        return value


pydantic_type(UserModel, name="User")      # GraphQL output type
pydantic_input(SignUpInput, name="SignUp") # GraphQL input object
```

When a Pydantic-backed input is supplied, the coerced values are constructed through
the model so its validators run; a validation failure surfaces as a GraphQL error in
the response rather than crashing execution. Both helpers also work as decorators.
