{"id":3893,"library":"avro-gen","title":"Avro-gen","description":"Avro-gen is a Python library that generates Python classes from Avro schemas (.avsc files). It enables static type checking and simplifies the manipulation of Avro records within Python applications. The library integrates with `avro-python3` for reading and writing specific records. Its current version is 0.3.0, and releases appear to be infrequent, typically coinciding with feature additions or dependency updates.","status":"active","version":"0.3.0","language":"en","source_language":"en","source_url":"https://github.com/rbystrit/avro_gen","tags":["avro","schema","code-generation","type-safety","data-serialization"],"install":[{"cmd":"pip install avro-gen","lang":"bash","label":"Install stable version"}],"dependencies":[{"reason":"Core dependency for Avro schema parsing, validation, and specific record handling.","package":"avro-python3","optional":false}],"imports":[{"note":"While 'import avro_gen' works, the primary usage is via the command line interface: `python -m avro_gen.avro_gen`.","symbol":"avro_gen","correct":"import avro_gen"}],"quickstart":{"code":"import os\nimport subprocess\n\n# Create a dummy Avro schema file\nschema_content = '''\n{\n  \"type\": \"record\",\n  \"name\": \"User\",\n  \"namespace\": \"example.avro\",\n  \"fields\": [\n    {\"name\": \"name\", \"type\": \"string\"},\n    {\"name\": \"favorite_number\",  \"type\": [\"int\", \"null\"]},\n    {\"name\": \"favorite_color\", \"type\": [\"string\", \"null\"]}\n  ]\n}\n'''\n\nschema_file = 'user.avsc'\noutput_dir = 'generated_avro_classes'\n\nwith open(schema_file, 'w') as f:\n    f.write(schema_content)\n\n# Generate Python classes from the schema\n# For robust execution in CI/CD, consider passing paths explicitly\ntry:\n    print(f\"Generating classes for {schema_file} into {output_dir}\")\n    subprocess.run(['python', '-m', 'avro_gen.avro_gen', '-s', schema_file, '-o', output_dir], check=True)\n    print(\"Generation successful.\")\n\n    # Import and use the generated class\n    # The generated module structure follows the Avro namespace\n    import sys\n    sys.path.insert(0, os.path.abspath(output_dir))\n    from example.avro import User # Assuming output_dir/example/avro/__init__.py was created\n\n    user_instance = User(name=\"Alice\", favorite_number=7, favorite_color=\"blue\")\n    print(f\"Created user: {user_instance.name}, favorite number: {user_instance.favorite_number}, color: {user_instance.favorite_color}\")\n\n    user_instance_2 = User(name=\"Bob\", favorite_number=None) # Optional fields can be None\n    print(f\"Created user 2: {user_instance_2.name}, favorite number: {user_instance_2.favorite_number}\")\n\n    # Demonstrate type checking (if using a linter/IDE)\n    # user_instance.favorite_number = \"not an int\" # Would be a type error\n\nexcept subprocess.CalledProcessError as e:\n    print(f\"Error generating Avro classes: {e}\")\n    print(f\"Stderr: {e.stderr}\")\nexcept ImportError as e:\n    print(f\"Error importing generated class. Did generation succeed and sys.path updated correctly? Error: {e}\")\nfinally:\n    # Clean up generated files\n    import shutil\n    if os.path.exists(schema_file):\n        os.remove(schema_file)\n    if os.path.exists(output_dir):\n        shutil.rmtree(output_dir)\n","lang":"python","description":"This quickstart demonstrates how to define an Avro schema, use `avro-gen` to generate Python classes, and then instantiate and use those classes. It highlights the CLI-driven generation and the resulting module structure."},"warnings":[{"fix":"Be mindful of your Avro schema's `namespace` attribute and adapt your import statements accordingly, ensuring the `output_dir` is in `sys.path` or a known Python path.","message":"The generated Python package structure mirrors the Avro schema's `namespace`. If your schema has `\"namespace\": \"com.example.app\"`, the generated classes will reside in `output_dir/com/example/app/` requiring `from com.example.app import MyClass`.","severity":"gotcha","affected_versions":"0.1.0+"},{"fix":"Integrate the `avro-gen` command into your build process or CI/CD pipeline to ensure that generated classes are always up-to-date with your latest Avro schemas.","message":"Any changes to your Avro schema files (e.g., adding a new field, changing a type) require you to re-run the `avro-gen` command line tool to regenerate the Python classes. The library does not automatically detect or recompile schemas.","severity":"gotcha","affected_versions":"0.1.0+"},{"fix":"Always install `avro-gen` in a clean virtual environment or ensure `avro-python3` within its specified range (`>=1.10.0,<2.0.0`) is used.","message":"`avro-gen` relies on `avro-python3` for core Avro functionality. While `avro-gen` pins a compatible version range, using an incompatible or outdated `avro-python3` version in your environment can lead to unexpected errors during class generation or at runtime when handling specific records.","severity":"gotcha","affected_versions":"0.1.0+"}],"env_vars":null,"last_verified":"2026-04-11T00:00:00.000Z","next_check":"2026-07-10T00:00:00.000Z"}