You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

升级Pydantic版本后遭遇基类字段被非注解属性覆盖报错的问题求助

升级Pydantic版本后遭遇基类字段被非注解属性覆盖报错的问题求助

我最近升级了Pydantic的版本,结果代码直接跑不起来了,报了这个错误:

Field 'allowed_type' defined on a base class
was overridden by a non-annotated attribute. All field
definitions, including overrides, require a type
annotation.

我的代码如下:
枚举类定义:

class AllowedType(str, Enum):
    YES = "YES"
    NO = "NO"

模型类定义:

class APIResponse(BaseModel):
    allowed_type: AllowedType
    def to_json(self):
        return json.dumps(
            {
                "allowed_type": self.allowed_type,
            }
        )

我尝试了Pydantic官方针对这个错误给出的所有解决方案,但都没起作用,有没有大佬能帮我排查下问题出在哪?

备注:内容来源于stack exchange,提问作者Tanu

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.04.14 08:44:33