Pydantic-Avro

0.10.0 · active · verified Sat Apr 11

Pydantic-Avro is a Python library designed to convert Pydantic classes into Avro schemas, and vice-versa, allowing for seamless integration between Pydantic models and Avro data formats. It is actively maintained with frequent updates, with the latest stable version being 0.10.0.

Warnings

Install

Imports

Quickstart

This example demonstrates how to define a Pydantic model inheriting from `AvroBase` and then generate its corresponding Avro schema. It also shows how to use `pydantic.Field` for adding descriptions and overriding default Avro type mappings (e.g., `datetime` to a 'string' Avro type explicitly).

import json
from typing import Optional
from pydantic import Field
from pydantic_avro.base import AvroBase

class User(AvroBase):
    id: int
    name: str = Field(..., description="User's full name")
    email: Optional[str] = None
    is_active: bool = True
    creation_date: str = Field(..., avro_type="string", description="Date of user creation in ISO format")

# Generate Avro schema
avro_schema = User.avro_schema()
print(json.dumps(avro_schema, indent=2))

view raw JSON →