python data_error ai_generated true

pydantic.errors.EnumError: 值不是有效的枚举成员

pydantic.errors.EnumError: value is not a valid enumeration member

ID: python/pydantic-enum-value-invalid

其他格式: JSON · Markdown 中文 · English
80%修复率
86%置信度
0证据数
2024-02-14首次发现

版本兼容性

版本状态引入弃用备注
2.x active

根因分析

类型为枚举的字段接收到未在枚举中定义的值。

English

A field typed as an Enum receives a value not defined in the enumeration.

generic

解决方案

  1. 95% 成功率
    Use enum member: Color.RED
  2. 90% 成功率
    Add custom validator to map strings to enum

无效尝试

常见但无效的做法:

  1. 80% 失败

    Enum member type mismatch.

  2. 70% 失败

    Pydantic expects the value, not name.