# Reading FastAPI's 422 Validation Errors

When a request breaks a model's rules, FastAPI answers 422 with a detail list that has one entry per problem, whose loc says where the problem is, msg what's wrong, type what kind of problem it is and input what was sent, so an app can show each message beside its own box.

Apprentice · FastAPI · Python · Updated November 19, 2026

By Gregor Dzierzon

## After this lesson you can

- Read FastAPI's 422 answer, saying where each problem is, what it is and what was sent from loc, msg, type and input
- Turn a 422's detail list into messages a person can read beside each field of a form

## Key takeaways

- A 422 body has one key, detail, a list with one entry for each problem, every problem at once.
- loc is the problem's address, a list that starts with where it was found (body, query or path) and ends with the field's name.
- msg says what's wrong in words, type names the kind of problem for code to check, and input is the value that was sent.
- In a nested body, loc goes on into the inner object, and a number in it is a position in a list, counted from 0.
- A front end matches each entry's last loc item to a box on its form and shows the message there.

You'll read FastAPI's 422 answer from both sides. When an app you're building sends a form and the API refuses it, the app has to tell the person what to fix. When you try an API on its docs page and get 422 where you expected 201, you have to work out why. At first the answer looks like a wall of brackets; it's a tidy list, once you know its four keys.

## One entry per problem

Rosa sends Happy Tails an application with an empty name and an age of 15, to the model from *Field Rules: Lengths, Ranges and Choices*. The answer is 422, laid out here the way the docs page shows it:

```json
{
  "detail": [
    {
      "type": "string_too_short",
      "loc": ["body", "name"],
      "msg": "String should have at least 1 character",
      "input": "",
      "ctx": {"min_length": 1}
    },
    {
      "type": "greater_than_equal",
      "loc": ["body", "age"],
      "msg": "Input should be greater than or equal to 18",
      "input": 15,
      "ctx": {"ge": 18}
    }
  ]
}
```

The whole body is one key, `detail`, holding a list. Each entry in the list is one problem, and every problem is there at once, the way *Checking Input by Hand* collected them. Two broken rules, two entries.

## The keys in every entry

Each entry answers the questions you'd ask about a problem:

| Key | The question it answers | In the age entry |
|---|---|---|
| `loc` | Where is the problem? | `["body", "age"]`: in the body, the field `age` |
| `msg` | What's wrong, in words? | Input should be greater than or equal to 18 |
| `type` | What kind of problem is it, as a name code can check? | `greater_than_equal` |
| `input` | What was sent? | `15` |
| `ctx` | Which rule, with its numbers? (only some entries) | `{"ge": 18}` |

`loc` is short for *location*. Its first item says where in the request the problem was found: `"body"`, `"query"` for the query string (`["query", "limit"]` in *Field Rules: Lengths, Ranges and Choices*), or `"path"` for the address (`["path", "game_id"]` when `/games/abc` came in, in *Path Parameters and Their Types*). Its last item is the field's name.

`msg` is written for a developer, and its wording can change between Pydantic versions. `type` stays the same, so code that reacts to a kind of problem checks `type`. Some types you'll meet often:

- `missing`: a required field wasn't sent. Its `input` is the whole object that was sent, so you can see what it held.
- `int_parsing`: text that can't become a whole number, such as `"fifteen"`.
- `string_too_short`, `greater_than_equal`: a `Field` rule was broken.
- `literal_error`: a value that isn't one of a `Literal`'s choices.

## Deeper addresses

In a nested body, `loc` goes on into the inner objects. Rosa's application lists the pets already at home, with *Request Models with Pydantic*'s `CurrentPet`, whose `pet_type` is now `Literal["dog", "cat", "rabbit", "other"]`. Her second pet is a hamster:

```json
{"detail":[{"type":"literal_error","loc":["body","current_pets",1,"pet_type"],"msg":"Input should be 'dog', 'cat', 'rabbit' or 'other'","input":"hamster","ctx":{"expected":"'dog', 'cat', 'rabbit' or 'other'"}}]}
```

Read the `loc` from left to right: in the body, in `current_pets`, item 1, its `pet_type`. The number is a position in the list, counted from 0 as Python counts, so item 1 is the second pet, Nibbles the hamster.

## What a front end does with it

An app shows each message beside the box it belongs to, so it goes through `detail` and takes the last item of each `loc`, the field's name. In Python, with Rosa's two problems:

```python
detail = [
    {"type": "string_too_short", "loc": ["body", "name"], "msg": "String should have at least 1 character"},
    {"type": "greater_than_equal", "loc": ["body", "age"], "msg": "Input should be greater than or equal to 18"},
]
for problem in detail:
    field = problem["loc"][-1]
    print(field, "->", problem["msg"])
```

```text
name -> String should have at least 1 character
age -> Input should be greater than or equal to 18
```

`problem["loc"][-1]` (read "loc, item minus one") is the last item of the list. The messages are clear to you, but a form for the public usually swaps in its own words and the box's label. The next chapter's *One Error Shape for the Whole API* gives every error the API sends, 422s included, one shape of its own.

## Do it: turn a 422 into sentences

The Happy Tails website shows a short list of what to fix above the form. It gets the `detail` list from the API's 422, and it shows each field by the label on its box.

- `detail`: the list from a 422's body. Each entry has `"type"`, `"loc"`, `"msg"` and `"input"`, as FastAPI sends them.
- `LABELS`: given in the starter. It maps each field's name to its label: `"name"` to `"Your name"`, `"email"` to `"Your email"`, `"age"` to `"Your age"` and `"pet_type"` to `"Type of pet"`.

**Your job:** write the function `friendly_messages(detail)`. It returns a list of sentences, one for each entry, in the same order.

- The field is the last item of the entry's `loc`, and its label is `LABELS[field]`.
- When the entry's `type` is `"missing"`, the sentence is the label, a space and the words *is required.*, such as `"Your email is required."`.
- For any other type, it's the label, a colon and a space, then the entry's `msg`, such as `"Your age: Input should be greater than or equal to 18"`.
- An empty `detail` gives an empty list, `[]`.

Worked through: Rosa sent no email, an age of 15 and a dragon, and the API answered with three entries (the first test has them). The first is `missing` at `["body", "email"]`, so it gives `"Your email is required."`. The second, at `["body", "age"]`, gives `"Your age: Input should be greater than or equal to 18"`. The third, at `["body", "pet_type"]`, gives `"Type of pet: Input should be 'dog', 'cat' or 'rabbit'"`. So the list is those three sentences, in that order.

Practiced live on the lesson page: write the Python right there and check it.

## Check yourself

2 questions to try on the lesson page.

## So what

A 422 isn't a wall of brackets: it's a list of problems, each with its address, its words and the value that broke the rule, and that's everything an app needs to tell a person exactly what to fix.

## Key terms

- [422 Unprocessable Content](https://www.ask-codey.com/glossary/422-unprocessable-content)
- [Automatic Validation](https://www.ask-codey.com/glossary/automatic-validation)
- [Field Validation](https://www.ask-codey.com/glossary/field-validation)

## Related

- [Field Rules: Lengths, Ranges and Choices](https://www.ask-codey.com/lessons/fastapi-pydantic-field-rules)
- [Request Models with Pydantic](https://www.ask-codey.com/lessons/fastapi-pydantic-request-models)
- [Path Parameters and Their Types](https://www.ask-codey.com/lessons/fastapi-path-parameters)

Source: https://www.ask-codey.com/lessons/fastapi-validation-error-responses
