Dataclasses Avro Schema

0.66.3 · active · verified Sat Apr 11

Dataclasses Avro Schema is a Python library that enables the generation of Avro schemas from Python dataclasses, Pydantic models, and Faust Records. It also provides functionalities for serializing and deserializing Python instances with these Avro schemas. The library is actively maintained, with frequent releases, and is currently at version 0.66.3.

Warnings

Install

Imports

Quickstart

This quickstart demonstrates how to define a dataclass with `AvroModel`, including enums, lists, and dictionaries. It shows how to generate the Avro schema, serialize a Python instance to Avro binary format, and then deserialize it back into a Python object.

import dataclasses
import enum
import typing
from dataclasses_avroschema import AvroModel


class FavoriteColor(enum.Enum):
    BLUE = "Blue"
    YELLOW = "Yellow"
    GREEN = "Green"


@dataclasses.dataclass
class User(AvroModel):
    "An User"
    name: str
    age: int
    pets: typing.List[str]
    accounts: typing.Dict[str, int]
    favorite_color: FavoriteColor
    country: str = "Argentina"
    address: typing.Optional[str] = None

    class Meta:
        namespace = "User.v1"
        aliases = ["user-v1", "super user"]

# Generate Avro schema
avro_schema = User.avro_schema()
print("Avro Schema:")
print(avro_schema)

# Create an instance
user_instance = User(
    name="John Doe",
    age=30,
    pets=["dog", "cat"],
    accounts={"bank": 1000, "crypto": 500},
    favorite_color=FavoriteColor.BLUE,
    country="USA",
    address="123 Main St"
)

# Serialize to Avro binary
serialized_data = user_instance.serialize()
print("\nSerialized data (bytes):", serialized_data)

# Deserialize from Avro binary
deserialized_user = User.deserialize(serialized_data)
print("\nDeserialized user:", deserialized_user)

view raw JSON →