API Reference¶
Auto-generated from source docstrings.
avro_datagen¶
avro_datagen
¶
Schema-driven data generator for Avro schemas with arg.properties hints.
RecordResolver(schema, seed=None)
¶
Resolves a single Avro record schema into generated dicts.
Maintains pools (for pool hints) across multiple generate() calls so that
pooled values are reused across records.
Source code in src/avro_datagen/resolver.py
generate()
¶
Generate one record. Fields resolved top-to-bottom.
SchemaValidationError(errors)
¶
Bases: ValueError
Raised when a schema fails validation.
Contains a list of error messages, one per issue found. The first message is also used as the exception's main message.
Source code in src/avro_datagen/validator.py
generate(schema_path, count, seed=None, now=None)
¶
Generate count fake records from an Avro schema file.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
schema_path
|
str | Path
|
Path to a .avsc file. |
required |
count
|
int
|
Number of records to generate. 0 means infinite. |
required |
seed
|
int | None
|
Optional seed for reproducible output. |
None
|
now
|
datetime | float | None
|
Optional clock anchor for relative bounds ("now", "-30d", "today", ...) and un-hinted timestamp/date fields. A timezone-aware datetime or epoch seconds. Defaults to 2026-01-01T00:00:00Z when seeded and the real clock otherwise, so reproducible output is a function of the pair (seed, now). |
None
|
Yields:
| Type | Description |
|---|---|
dict
|
dict — one record per iteration. |
Source code in src/avro_datagen/generator.py
load_schema(path)
¶
to_human_json(record, schema)
¶
Return a copy of record with temporal logical types in human form.
Walks the record alongside its schema, converting timestamp-millis,
timestamp-micros, date, time-millis and time-micros to ISO-8601
strings. Every other value is passed through unchanged.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
record
|
dict
|
A generated record (wire form). |
required |
schema
|
dict
|
The Avro record schema it was generated from. |
required |
Returns:
| Type | Description |
|---|---|
dict
|
A new dict — |
Source code in src/avro_datagen/json_format.py
validate(schema)
¶
Validate an Avro schema and return a list of warnings.
Errors (structural problems) raise SchemaValidationError. Warnings (unknown hint keys, etc.) are returned as a list.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
schema
|
dict | str | Path
|
Parsed schema dict or path to an .avsc file. |
required |
Returns:
| Type | Description |
|---|---|
list[str]
|
List of warning strings (empty if the schema is clean). |
Raises:
| Type | Description |
|---|---|
SchemaValidationError
|
if the schema has structural errors. |
Source code in src/avro_datagen/validator.py
generator¶
avro_datagen.generator
¶
Core generator — loads an Avro schema and yields fake records.
generate(schema_path, count, seed=None, now=None)
¶
Generate count fake records from an Avro schema file.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
schema_path
|
str | Path
|
Path to a .avsc file. |
required |
count
|
int
|
Number of records to generate. 0 means infinite. |
required |
seed
|
int | None
|
Optional seed for reproducible output. |
None
|
now
|
datetime | float | None
|
Optional clock anchor for relative bounds ("now", "-30d", "today", ...) and un-hinted timestamp/date fields. A timezone-aware datetime or epoch seconds. Defaults to 2026-01-01T00:00:00Z when seeded and the real clock otherwise, so reproducible output is a function of the pair (seed, now). |
None
|
Yields:
| Type | Description |
|---|---|
dict
|
dict — one record per iteration. |
Source code in src/avro_datagen/generator.py
resolver¶
avro_datagen.resolver
¶
Field resolver — maps Avro types + arg.properties to generated values.
This is the core engine. It walks an Avro schema top-to-bottom, resolving each
field's value based on its type, logical type, and arg.properties hints. Fields
are resolved in declaration order so that later fields can reference earlier ones
via ref and rules.
RecordResolver(schema, seed=None)
¶
Resolves a single Avro record schema into generated dicts.
Maintains pools (for pool hints) across multiple generate() calls so that
pooled values are reused across records.
Source code in src/avro_datagen/resolver.py
generate()
¶
Generate one record. Fields resolved top-to-bottom.
json_format¶
avro_datagen.json_format
¶
Post-processing: render wire-form records as human-readable JSON.
The generator emits the Avro wire representation of logical types — a
timestamp-millis is an int of epoch milliseconds. That is what a
schema-registry serde expects to receive after it encodes, but Kafka UIs with a
produce form (kafbat-ui, for one) want the human representation and do the
conversion themselves. Pasting a generated sample into such a form therefore
means hand-converting every timestamp.
to_human_json() is a pure post-processing walker over an already-generated
record. It never touches the generator or the producer path, both of which stay
wire-form.
This module deliberately imports nothing from the rest of the package —
resolver imports iso_utc from here, not the other way round.
iso_utc(dt, timespec='auto')
¶
to_human_json(record, schema)
¶
Return a copy of record with temporal logical types in human form.
Walks the record alongside its schema, converting timestamp-millis,
timestamp-micros, date, time-millis and time-micros to ISO-8601
strings. Every other value is passed through unchanged.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
record
|
dict
|
A generated record (wire form). |
required |
schema
|
dict
|
The Avro record schema it was generated from. |
required |
Returns:
| Type | Description |
|---|---|
dict
|
A new dict — |
Source code in src/avro_datagen/json_format.py
validator¶
avro_datagen.validator
¶
Schema validator -- checks Avro schemas for structural issues and common mistakes.
Validates:
- Required fields (name, type, fields for records)
- Valid primitive and logical types
- Logical type compatibility with base types (e.g. uuid must be on string)
- Known arg.properties hint keys (warns on unknown keys)
- Hints that are misplaced rather than misspelled: args/kwargs/locale
without a sibling faker, and hints nested inside the type object
- faker specs: the method exists, the spec shape is known
- range bounds match the field's (logical) type
- options entries can be encoded as the field's Avro type
- Rule conditions reference declared fields
- ref targets exist and appear before the referencing field
- pool hints: scalar or {size, per, seed} object form, per targets appear
before the pooled field, and pool/foreign_key mutual exclusivity
Validation is not mandatory -- generate() still works with any schema the
resolver accepts. Use validate() before generation for clearer errors, or call
it from the avro-datagen validate CLI subcommand.
SchemaValidationError(errors)
¶
Bases: ValueError
Raised when a schema fails validation.
Contains a list of error messages, one per issue found. The first message is also used as the exception's main message.
Source code in src/avro_datagen/validator.py
validate(schema)
¶
Validate an Avro schema and return a list of warnings.
Errors (structural problems) raise SchemaValidationError. Warnings (unknown hint keys, etc.) are returned as a list.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
schema
|
dict | str | Path
|
Parsed schema dict or path to an .avsc file. |
required |
Returns:
| Type | Description |
|---|---|
list[str]
|
List of warning strings (empty if the schema is clean). |
Raises:
| Type | Description |
|---|---|
SchemaValidationError
|
if the schema has structural errors. |
Source code in src/avro_datagen/validator.py
producer¶
avro_datagen.producer
¶
Kafka producer — generates records from an Avro schema and publishes to a topic.
build_producer_config(bootstrap_servers, security_protocol='', sasl_mechanism='', sasl_username='', sasl_password='', acks='all', linger_ms=5, batch_size=16384, compression_type='none')
¶
Build a confluent-kafka producer config dict from explicit params.
Source code in src/avro_datagen/producer.py
produce(schema_path, topic, producer_config, count=10, rate=None, seed=None, key_fn=None, on_delivery=None, on_progress=None)
¶
Generate records and publish them to a Kafka topic.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
schema_path
|
str | Path
|
Path to the .avsc schema file. |
required |
topic
|
str
|
Kafka topic to publish to. |
required |
producer_config
|
dict[str, str | int]
|
confluent-kafka Producer config dict. |
required |
count
|
int
|
Number of records (0 = infinite). |
10
|
rate
|
float | None
|
Records per second limit. None = unlimited. |
None
|
seed
|
int | None
|
Random seed for reproducibility. |
None
|
key_fn
|
Callable[[dict[str, Any]], str | None] | None
|
Callable to extract a message key from the record. |
None
|
on_delivery
|
Callable[[Any, Any], None] | None
|
Per-message delivery callback (err, msg). |
None
|
on_progress
|
Callable[[int, dict[str, Any]], None] | None
|
Called after each record with (index, record). |
None
|
Returns:
| Type | Description |
|---|---|
dict[str, int | float]
|
dict with produced, errors, elapsed_s keys. |
Source code in src/avro_datagen/producer.py
54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 | |
cli¶
avro_datagen.cli
¶
CLI entry point — generate fake data to stdout as JSON lines.