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

Fastavro 1.7.0解析含enum类型的Avro Schema失败问题咨询

问题:fastavro 1.7.0无法识别Avro枚举类型?

我编写了如下代码:

import fastavro

schema = {
    "name": "event",
    "type": "record",
    "fields": [{"name": "event_type", "type": "enum", "symbols": ["START", "STOP"]}],
}

checker = fastavro.parse_schema(schema=schema)

运行后出现如下错误:

Traceback (most recent call last):
  File ".\test_schema.py", line 7, in <module>
    checker = fastavro.parse_schema(schema=schema)
  File "fastavro\_schema.pyx", line 121, in fastavro._schema.parse_schema
  File "fastavro\_schema.pyx", line 322, in fastavro._schema._parse_schema
  File "fastavro\_schema.pyx", line 381, in fastavro._schema.parse_field
  File "fastavro\_schema.pyx", line 191, in fastavro._schema._parse_schema
fastavro._schema_common.UnknownType: enum

运行环境:

  • fastavro 1.7.0
  • Windows 10 + Python 3.8.8
  • Linux RHEL 8 + Python 3.10

根据Avro枚举类型官方规范,该Schema格式符合要求,但fastavro无法识别enum类型。请问是操作有误,还是fastavro 1.7.0不支持Avro 1.9的枚举类型?


解答

问题出在fastavro 1.7.0对枚举类型的定义要求上,这个版本不支持你使用的简化内联写法,必须给枚举类型指定name字段,写成完整的嵌套结构。

修正后的代码如下:

import fastavro

schema = {
    "name": "event",
    "type": "record",
    "fields": [
        {
            "name": "event_type",
            "type": {
                "type": "enum",
                "name": "EventType",  # 必须添加枚举的名称
                "symbols": ["START", "STOP"]
            }
        }
    ],
}

checker = fastavro.parse_schema(schema=schema)

修改后即可正常解析。如果想使用Avro规范中的简化写法,建议升级fastavro到2.x及以上版本,新版本对Avro规范的兼容性更完善。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.07 00:01:07