Producing messages with the Python SDK
Last updated:
With the runtime
Publish a message with its URN and payload. The BabelQueue runtime builds the
canonical envelope and pushes it to your broker:
from babelqueue import BabelQueue
app = BabelQueue("redis://localhost:6379/0", queue="orders")
app.publish("urn:babel:orders:created", {
"order_id": 1042,
"amount": 99.90,
})
With the codec only
If you manage the broker connection yourself, build the envelope with
EnvelopeCodec and publish the JSON to your own client:
from babelqueue import EnvelopeCodec
env = EnvelopeCodec.make("urn:babel:orders:created", {"order_id": 1042, "amount": 99.90})
body = EnvelopeCodec.encode(env) # -> UTF-8 JSON string
# redis.rpush("queues:orders", body)
# / channel.basic_publish(body=body, ...)
The payload above is wrapped in the canonical schema_version: 1 envelope —
identical to what every other SDK emits, with meta.lang set to "python":
{
"job": "urn:babel:orders:created",
"trace_id": "7b3f9c2a-e41d-4f88-9b2a-1c0d5e6f7a8b",
"data": { "order_id": 1042, "amount": 99.90 },
"meta": { "id": "f1e2d3c4-b5a6-4789-90ab-cdef01234567", "queue": "orders", "lang": "python", "schema_version": 1, "created_at": 1749132727000 },
"attempts": 0
}
Extension (meta) keys
Pass your own meta keys to EnvelopeCodec.make with the keyword-only meta= argument:
env = EnvelopeCodec.make(
"urn:acme:orders:created", {"order_id": 1042},
queue="orders", meta={"ext": {"acme": {"tier": "gold"}}},
)
A key that collides with a core meta key (id, queue, lang, schema_version, created_at)
or with a forbidden key (max_retries, attempts, source, ts) raises EnvelopeKeyError;
pointer names the key, for example /meta/ts. EnvelopeCodec.encode raises the same error for an
envelope that still carries a forbidden key or the inbound-only urn alias, instead of dropping it.
Keep vendor data under a single meta.ext.<vendor> block and use snake_case keys
(ADR-0031). The entries are written next to the core meta keys and survive every retry,
dead-letter and redrive hop. See Extension keys in the
envelope spec.
Next: Consuming messages.