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.
pip install pydantic-avroVerified import paths — ran on the pinned version, not inferred.
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).
Standardize your application's Pydantic version. If migrating to V2, update all models to V2 syntax. If using both, consult Pydantic's migration guide for interoperability strategies and ensure `pydantic-avro`'s behavior is as expected with mixed environments.
As a workaround, consider using a `Union` with a constant `str` for single-literal cases, or explicitly defining an `enum` Avro type if appropriate, until the issue is resolved.
Upgrade to `pydantic-avro` version `0.9.0` or later to ensure proper handling of list and tuple types in Avro schema generation.
Review the documentation for the `mode` parameter in `AvroBase.avro_schema()` and incorporate it if you require more control or specific Avro schema generation behaviors.
Migrate any custom type conversion logic to use the `avro_type` argument within `pydantic.Field` for cleaner and officially supported type overrides.
Simplify the Pydantic model's complex types, for example, by breaking down deeply nested unions or tuples into simpler, directly supported types, or by ensuring enum values are strings if an enum is involved. Upgrading `pydantic-avro` to the latest version might also resolve issues with previously unsupported complex types, as fixes have been implemented for some of these cases.
Modify the Avro schema to ensure unique names for fields and nested records, or manually adjust the generated Pydantic model to resolve the naming collision. A proposed fix is to include parent names in the generated class naming scheme to ensure uniqueness.
Install the library using pip: `pip install pydantic-avro`.