python data_error ai_generated true

422 Unprocessable Entity: {"detail":[{"type":"missing","loc":["body","email"],"msg":"Field required"}]}

ID: python/fastapi-422-missing-body

Also available as: JSON · Markdown · 中文
80%Fix Rate
86%Confidence
0Evidence
2024-02-11First Seen

Version Compatibility

VersionStatusIntroducedDeprecatedNotes
0.100+ active — — —

Root Cause

The client sent a JSON body missing a required field, or sent form data where JSON was expected, so Pydantic validation rejects the request.

generic

中文

客户端发送的 JSON 正文缺少必填字段,或发送了表单数据而期望 JSON,导致 Pydantic 验证拒绝请求。

Workarounds

  1. 90% success
    class UserIn(BaseModel):
        email: EmailStr
        name: str = ''
  2. 85% success
    from fastapi import Form
    @app.post('/u')
    async def create(email: str = Form(...)): ...

Dead Ends

Common approaches that don't work:

  1. 85% fail

    Hides the real cause; clients still send invalid payloads and data is dropped.

  2. 70% fail

    Still required in Pydantic v2 unless a default is provided.