# fastapi.exceptions.RequestValidationError: 422: 字段必填

- **ID:** `python/fastapi-requestvalidationerror-query-parameter-missing`
- **领域:** python
- **类别:** type_error
- **验证级别:** ai_generated
- **修复率:** 80%

## 根因

请求中未提供必需的查询参数或路径参数。

## 版本兼容性

| 版本 | 状态 | 引入 | 弃用 |
|------|------|------|------|
| 3.x | active | — | — |

## 解决方案

1. **Use Optional type with default None.** (95% 成功率)
   ```
   from typing import Optional
from fastapi import FastAPI, Query
app = FastAPI()
@app.get('/items/')
async def read_items(q: Optional[str] = Query(None)):
    return {'q': q}
   ```
2. **Set default value directly.** (90% 成功率)
   ```
   @app.get('/items/')
async def read_items(q: str = 'default'):
    return {'q': q}
   ```

## 无效尝试

- **Adding a default value of None without Optional type.** — Without Optional, FastAPI still treats it as required and will raise validation error if missing. (60% 失败率)
- **Using Query(None) without Optional.** — Query(None) sets default to None but type hint still requires the field, causing validation error. (70% 失败率)
