{"id":1341,"library":"avro-python3","title":"Apache Avro (Python 3)","description":"Apache Avro (`avro-python3`) is the official Python 3 implementation of the Avro remote procedure call and data serialization framework. It enables defining language-agnostic data schemas and serializing data into a compact binary format, facilitating cross-language data exchange. The current stable version available on PyPI is 1.10.2, with releases happening periodically as part of the broader Apache Avro project.","status":"active","version":"1.10.2","language":"en","source_language":"en","source_url":"https://github.com/apache/avro/tree/master/lang/py","tags":["serialization","rpc","avro","data-format","apache","schema"],"install":[{"cmd":"pip install avro-python3","lang":"bash","label":"Install stable version"}],"dependencies":[{"reason":"Used for package metadata and build system; included as a runtime dependency in setup.py.","package":"setuptools","optional":false}],"imports":[{"note":"For parsing and working with Avro schemas.","symbol":"avro.schema","correct":"import avro.schema"},{"note":"For reading and writing Avro data using DatumReader/DatumWriter.","symbol":"avro.io","correct":"import avro.io"},{"note":"For working with Avro data files using DataFileReader/DataFileWriter.","symbol":"avro.datafile","correct":"import avro.datafile"}],"quickstart":{"code":"import avro.schema\nimport avro.io\nimport avro.datafile\nimport io\n\n# 1. Define the Avro schema\nschema_str = \"\"\"\n{\n    \"type\": \"record\",\n    \"name\": \"User\",\n    \"fields\": [\n        {\"name\": \"name\", \"type\": \"string\"},\n        {\"name\": \"favorite_number\", \"type\": [\"int\", \"null\"]},\n        {\"name\": \"favorite_color\", \"type\": [\"string\", \"null\"]}\n    ]\n}\n\"\"\"\nschema = avro.schema.parse(schema_str)\n\n# 2. Write data to a BytesIO object (simulating a file)\nwriter = avro.io.DatumWriter(schema)\nbytes_writer = io.BytesIO()\ndata_file_writer = avro.datafile.DataFileWriter(bytes_writer, writer, schema)\n\ndata_file_writer.append({\"name\": \"Alyssa\", \"favorite_number\": 256, \"favorite_color\": None})\ndata_file_writer.append({\"name\": \"Ben\", \"favorite_number\": 7, \"favorite_color\": \"red\"})\ndata_file_writer.close()\n\n# Get the serialized data\navro_data = bytes_writer.getvalue()\n\n# 3. Read data from the BytesIO object\nbytes_reader = io.BytesIO(avro_data)\nreader = avro.io.DatumReader(schema)\ndata_file_reader = avro.datafile.DataFileReader(bytes_reader, reader)\n\nread_records = [record for record in data_file_reader]\ndata_file_reader.close()\n\n# Print the read records\nprint(read_records)\n# Expected output:\n# [{'name': 'Alyssa', 'favorite_number': 256, 'favorite_color': None}, {'name': 'Ben', 'favorite_number': 7, 'favorite_color': 'red'}]","lang":"python","description":"This example demonstrates how to define an Avro schema, serialize Python dictionary data into the Avro binary format, and then deserialize it back into Python objects using an in-memory BytesIO stream to simulate file operations."},"warnings":[{"fix":"Always use `pip install avro-python3` and import directly from the `avro` namespace (e.g., `import avro.schema`).","message":"Ensure you install `avro-python3` for Python 3 projects. The older `avro` package (without the `-python3` suffix) is largely unmaintained for Python 3 and may cause compatibility issues or unexpected behavior. This distinction is crucial for modern Python development.","severity":"gotcha","affected_versions":"All versions of `avro-python3` when compared to the legacy `avro` package."},{"fix":"Familiarize yourself with the Avro specification's rules for schema evolution. Use `null` for new optional fields, ensure compatible type changes, and rigorously test schema updates across your ecosystem.","message":"Misunderstanding Avro's schema evolution rules (e.g., adding/removing fields, changing types, default values) can lead to data deserialization errors, especially when different versions of producers and consumers interact.","severity":"gotcha","affected_versions":"All Avro implementations, as this is a specification-level concern."},{"fix":"To inspect Avro data, use the `avro-python3` library itself to read and deserialize the data, or utilize Avro-specific tools that can render the binary content into a human-readable format like JSON.","message":"Avro data files (`.avro` extension) are binary and not human-readable. Attempting to inspect them directly with a text editor will yield unintelligible, garbled output, which can be confusing for new users.","severity":"gotcha","affected_versions":"All Avro implementations."}],"env_vars":null,"last_verified":"2026-04-09T00:00:00.000Z","next_check":"2026-07-08T00:00:00.000Z"}