# pydantic_core._pydantic_core.ValidationError: 1 validation error for Product
price
  输入应为有效的字符串 [type=string_type, input_value=19.99, input_type=float]

- **ID:** `python/pydantic-v2-string-type-coercion-strict`
- **领域:** python
- **类别:** type_error
- **验证级别:** ai_generated
- **修复率:** 80%

## 根因

模型配置了 strict=True（或字段使用了 StrictStr），因此 Pydantic v2 拒绝将 float 强制转换为字符串，即使宽松模式会接受。

## 版本兼容性

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

## 解决方案

1. **** (93% 成功率)
   ```
   Add a field_validator with mode='before' to coerce: `@field_validator('price', mode='before') @classmethod def coerce(cls, v): return str(v)`
   ```
2. **** (90% 成功率)
   ```
   Relax strictness per-field: `price: str = Field(strict=False)` while keeping the model strict.
   ```

## 无效尝试

- **** — Pushes type-handling logic to every call site; easy to miss one and re-trigger the error (60% 失败率)
- **** — Disables strictness everywhere, silently accepting bad data for unrelated fields (70% 失败率)
