Skip to content

Instantly share code, notes, and snippets.

@gurmeetsaran
Created March 26, 2025 19:34
Show Gist options
  • Save gurmeetsaran/5779805a0f761d627282720af16615db to your computer and use it in GitHub Desktop.
Save gurmeetsaran/5779805a0f761d627282720af16615db to your computer and use it in GitHub Desktop.
Generate Python Model from avsc file
from dataclasses_avroschema import ModelGenerator, ModelType
model_generator = ModelGenerator()
schema = {
"type": "record",
"namespace": "com.kubertenes",
"name": "AvroDeployment",
"fields": [
{"name": "image", "type": "string"},
{"name": "replicas", "type": "int"},
{"name": "port", "type": "int"},
],
}
result = model_generator.render_module(
schemas=[schema],
model_type=ModelType.AVRODANTIC.value,
)
# save the result in a file
with open("models.py", mode="+w") as f:
f.write(result)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment