python data_error ai_generated true

fastapi.exceptions.RequestValidationError: 1 validation error for ItemCreate body -> name field required (type=value_error.missing)

ID: python/fastapi-422-field-required-body

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

Version Compatibility

VersionStatusIntroducedDeprecatedNotes
0.100+ active — — —

Root Cause

Client sent a JSON body missing a field that the Pydantic model declares as required (no default).

generic

中文

客户端发送的 JSON 请求体缺少 Pydantic 模型声明为必需(无默认值)的字段。

Workarounds

  1. 92% success
    from typing import Optional
    class ItemCreate(BaseModel):
        name: Optional[str] = None
  2. 95% success
    requests.post(url, json={'name': 'x', 'price': 1.0})  # include all required fields

Dead Ends

Common approaches that don't work:

  1. 95% fail

    Response model affects serialization, not request validation.

  2. 80% fail

    Validation runs before your middleware can bypass it; also disables all validation.