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

quart-schema响应验证报错:Cannot load dataclass导致500错误求助

问题原因与解决方法

错误的核心原因是:你返回的是普通字典{"msg": "info"},但@validate_response(Info, 200)装饰器要求返回值必须是Info类的实例。quart-schema在验证时会尝试将返回的响应数据加载为指定的Info类型,字典无法直接完成这个加载过程,因此抛出Cannot load <class 'schema.Info'>错误。

解决方法

  • 直接返回Info类的实例,替换原返回语句即可:
    return Info(msg="info"), 200
    
    修改后的完整代码:
    from dataclasses import dataclass
    
    from quart import Quart
    from quart_schema import QuartSchema, validate_response
    
    app = Quart(__name__)
    QuartSchema(app)
    
    @dataclass
    class Info:
        msg: str
    
    @app.get("/info")
    @validate_response(Info, 200)
    async def info() -> tuple[Info, int]:
        return Info(msg="info"), 200
    

内容的提问来源于stack exchange,提问作者Spitzer

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.06.30 09:43:21