できない.dev

FastAPI「422 Unprocessable Entity」の原因と直し方

422 Unprocessable Entity は、応答の detail の loc と msg を読み、値を型ヒントに合わせれば直る。
必須項目の欠落(Field required)・型の不一致・JSON 以外での送信が主な原因だ。

公開: 更新:

症状できない
curl -s -w '\n%{http_code}\n' -d "name=pen&price=100" http://127.0.0.1:8000/items
{"detail":[{"type":"model_attributes_type","loc":["body"],"msg":"Input should be a valid dictionary or object to extract fields from","input":"name=pen&price=100"}]}
422
解決後できた
curl -s -w '\n%{http_code}\n' -H "Content-Type: application/json" -d '{"name":"pen","price":100}' http://127.0.0.1:8000/items
{"name":"pen","price":100}
200

2026-10-29 に python:3.12(Python 3.12.16 / Git 2.47.3) で実際に打って取った出力。検証環境の詳細

要約

FastAPI の API を呼ぶと、処理に入る前に 422 Unprocessable Entity が返る。

{"detail":[{"type":"missing","loc":["query","q"],"msg":"Field required","input":null}]}

FastAPI は関数の引数に書いた型ヒントでリクエストの値を検証し、合わなければ RequestValidationError を出して 422 を返す(Handling Errors(新しいタブで開く))。
関数の中の処理は実行されない。
loc の 1 つ目が値の場所(path・query・body)、2 つ目が項目名で、msg が理由だ。
上の例は、必須のクエリパラメータ q を付けずに呼んだときの応答になる。
この記事の応答は Python 3.12・FastAPI 0.143.0・Pydantic 2.14.1 で実際に返ったものだ。

実行例

python:3.12 で uvicorn を立てて curl で呼ぶと、必須の q の欠落、/items/abc、フォーム形式のボディ、包んだ JSON のどれも 422 が返った。
q の既定値、Content-Type: application/json の JSON、Body(embed=True) の受け口に直すと 200 になった。

$ curl -s -w '\n%{http_code}\n' http://127.0.0.1:8000/search
{"detail":[{"type":"missing","loc":["query","q"],"msg":"Field required","input":null}]}
422
$ curl -s -w '\n%{http_code}\n' http://127.0.0.1:8000/items/abc
{"detail":[{"type":"int_parsing","loc":["path","item_id"],"msg":"Input should be a valid integer, unable to parse string as an integer","input":"abc"}]}
422
$ curl -s -w '\n%{http_code}\n' http://127.0.0.1:8000/items/100
{"item_id":100}
200
$ curl -s -w '\n%{http_code}\n' -d "name=pen&price=100" http://127.0.0.1:8000/items
{"detail":[{"type":"model_attributes_type","loc":["body"],"msg":"Input should be a valid dictionary or object to extract fields from","input":"name=pen&price=100"}]}
422
$ curl -s -w '\n%{http_code}\n' -H "Content-Type: application/json" -d '{"item":{"name":"pen","price":100}}' http://127.0.0.1:8000/items
{"detail":[{"type":"missing","loc":["body","name"],"msg":"Field required","input":{"item":{"name":"pen","price":100}}},{"type":"missing","loc":["body","price"],"msg":"Field required","input":{"item":{"name":"pen","price":100}}}]}
422
$ curl -s -w '\n%{http_code}\n' http://127.0.0.1:8000/find
{"q":null,"limit":10}
200
$ curl -s -w '\n%{http_code}\n' "http://127.0.0.1:8000/find?limit=x"
{"detail":[{"type":"int_parsing","loc":["query","limit"],"msg":"Input should be a valid integer, unable to parse string as an integer","input":"x"}]}
422
$ curl -s -w '\n%{http_code}\n' -H "Content-Type: application/json" -d '{"name":"pen","price":100}' http://127.0.0.1:8000/items
{"name":"pen","price":100}
200
$ curl -s -w '\n%{http_code}\n' -H "Content-Type: application/json" -d '{"item":{"name":"pen","price":100}}' http://127.0.0.1:8000/wrapped
{"name":"pen","price":100}
200
$ curl -s -w '\n%{http_code}\n' -H "Content-Type: application/json" -d '{"name":"pen","price":100}' http://127.0.0.1:8000/wrapped
{"detail":[{"type":"missing","loc":["body","item"],"msg":"Field required","input":null}]}
422

— 2026-10-29 時点の出力

検証環境Python 3.12.16 / Git 2.47.3 / 2026-10-29 検証
検証日
実行環境
python:3.12 Debian GNU/Linux 13 (trixie)
バージョン
  • Python 3.12.16
  • Git 2.47.3

この記事の「実行例」は、上記の環境で実際にコマンドを実行して得られた出力をそのまま掲載しています。 再現手順はリポジトリの検証スクリプトとして管理し、定期的に再実行して出力を更新しています。

解決策

1. detail を読んで必須かどうかを決める

loc に出た項目を送るか、送らなくてもよい引数には既定値を付ける。

@app.get("/search")
def search(q: str | None = None, limit: int = 10):
    return {"q": q, "limit": limit}

この形なら /search は 200 で返る。
limit=x のように型に合わない値は、既定値があっても 422 になる。

2. JSON として送る

await fetch("/items", {
  method: "POST",
  headers: { "Content-Type": "application/json" },
  body: JSON.stringify({ name: "pen", price: 100 }),
});

3. 包んで送るなら Body(embed=True)

クライアントが {"item": {...}} の形で送る仕様なら、受ける側を合わせる(Body - Multiple Parameters(新しいタブで開く))。

from typing import Annotated
from fastapi import Body
 
@app.post("/items")
def create_item(item: Annotated[Item, Body(embed=True)]):
    return item

こうすると包んだ形は 200 になり、包まずに送った形のほうが ["body","item"] の missing になる。
どちらか一方にそろえる。

よくある原因

  1. 必須の値が無い。
    既定値の無いクエリパラメータは必須になる。
    q: str を付け忘れると上の missing になり、ボディのモデルの項目が欠けると loc が ["body","price"] になる。
  2. 型に合わない値を送っている。
    item_id: int に abc を送ると int_parsing で「Input should be a valid integer」になる。
    "1.5" も整数にできないので同じエラーだが、"100" のような文字列は整数に変換されて通る。
  3. JSON で送っていない。
    モデルで受けるボディは JSON として読まれる(Request Body(新しいタブで開く))。
    フォーム形式で送ったり、JSON の文字列を Content-Type: text/plain で送ったりすると、loc が ["body"] の「Input should be a valid dictionary or object to extract fields from」になる。
    末尾のカンマなど JSON として壊れていると json_invalid になる。
  4. ボディの包み方が違う。
    モデルが 1 つだけなら、FastAPI はその項目を JSON の最上位で受け取る。
    {"item": {...}} と包んで送ると、name と price の両方が missing になる。