Skip to main content

pronto-kafka

Async Kafka producer/consumer helpers for FastAPI (and any async Python) projects. Wraps aiokafka with a module-level producer, context-manager consumers, and ready-made FastAPI lifespan hooks.

Installation

pip install pronto-kafka

Quick start — FastAPI producer

from fastapi import FastAPI
from pronto_kafka.v1 import make_producer_lifespan, send

app = FastAPI(lifespan=make_producer_lifespan("KAFKA_BOOTSTRAP_SERVERS"))

@app.post("/orders")
async def create_order(order: dict):
    import json
    await send("orders", json.dumps(order).encode())
    return {"status": "queued"}

Set your bootstrap servers in the environment:

KAFKA_BOOTSTRAP_SERVERS=localhost:9092

Pass any env var name to make_producer_lifespan — MY_KAFKA_SERVERS, whatever fits your project.

Quick start — consumer

from pronto_kafka.v1 import create_consumer_from_env

async def process_orders():
    async with create_consumer_from_env("orders", group_id="order-svc") as consumer:
        async for msg in consumer:
            print(msg.topic, msg.value)

Quick start — consumer as FastAPI background task

from fastapi import FastAPI
from pronto_kafka.v1 import make_consumer_lifespan

async def handle(msg):
    print(f"received: {msg.value}")

app = FastAPI(
    lifespan=make_consumer_lifespan(
        "orders",
        group_id="order-svc",
        handler=handle,
    )
)

Producer API

make_producer_lifespan(env_var="KAFKA_BOOTSTRAP_SERVERS", **kwargs)

Returns a FastAPI-compatible lifespan that starts the producer on startup and stops it on shutdown. Extra kwargs are forwarded to AIOKafkaProducer.

init_producer(bootstrap_servers, **kwargs)

Start the module-level producer with an explicit bootstrap-servers string.

init_producer_from_env(env_var="KAFKA_BOOTSTRAP_SERVERS", **kwargs)

Start the producer by reading bootstrap servers from an environment variable. Raises RuntimeError if the variable is missing or empty.

close_producer()

Stop and tear down the module-level producer.

get_producer() -> AIOKafkaProducer

Return the active producer instance. Raises RuntimeError if not initialised.

send(topic, value=None, key=None, headers=None, partition=None, **kwargs) -> RecordMetadata

Convenience wrapper around producer.send_and_wait(...). Raises RuntimeError if the producer is not initialised.

meta = await send("events", b'{"type": "login"}', key=b"user-42")
print(meta.topic, meta.partition, meta.offset)

Consumer API

create_consumer(*topics, group_id, bootstrap_servers, **kwargs)

Async context manager that yields a started AIOKafkaConsumer. Stops the consumer on exit.

async with create_consumer("orders", group_id="svc", bootstrap_servers="localhost:9092") as consumer:
    async for msg in consumer:
        process(msg)

create_consumer_from_env(*topics, group_id, env_var="KAFKA_BOOTSTRAP_SERVERS", **kwargs)

Same as create_consumer but reads bootstrap servers from an environment variable.

make_consumer_lifespan(*topics, group_id, env_var="KAFKA_BOOTSTRAP_SERVERS", handler, **kwargs)

Returns a FastAPI-compatible lifespan that runs a consumer loop as an asyncio background task. Supply a handler async function that receives each ConsumerRecord.

async def handle(msg):
    await process(msg.value)

app = FastAPI(lifespan=make_consumer_lifespan("orders", group_id="svc", handler=handle))

Manual lifespan (without helpers)

from contextlib import asynccontextmanager
from fastapi import FastAPI
from pronto_kafka.v1 import init_producer_from_env, close_producer

@asynccontextmanager
async def lifespan(app):
    await init_producer_from_env("KAFKA_BOOTSTRAP_SERVERS")
    yield
    await close_producer()

app = FastAPI(lifespan=lifespan)

Development

pip install -e ".[dev]"
pytest

Integration tests run automatically when KAFKA_BOOTSTRAP_SERVERS is set (e.g. a local Docker Kafka), otherwise they are skipped.

Versioning

Versions are derived from git tags via hatch-vcs.

git tag v0.1.0
hatch build

Metadata

Release files for pronto-kafka 0.0.1

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for pronto-kafka 0.0.1
File Size Uploaded Provenance
pronto_kafka-0.0.1.tar.gz 10.6 kB With provenance Details

Built distribution (wheel)

Table of built distributions (wheels) for pronto-kafka 0.0.1
File Interpreter ABI Platform
pronto_kafka-0.0.1-py3-none-any.whl Python 3 none any Details

Total release size: 17.5 kB

Release files / pronto_kafka-0.0.1.tar.gz

Download URL pronto_kafka-0.0.1.tar.gz
Size 10.6 kB
Tags Source
SHA-256 checksum
How to use checksums
e04930194110c3f435d7051366b43e80280294f31fd7bcc14dda0d7fb22d5698
BLAKE2b-256 checksum
How to use checksums
5b10cdf8273ca5eb0d571a2ec7f27281c6da0422e42a59eacad51e2f387cf4c8
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/6.1.0 CPython/3.13.12

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on May 25, 2026.

Transparency log

Release files / pronto_kafka-0.0.1-py3-none-any.whl

Download URL pronto_kafka-0.0.1-py3-none-any.whl
Size 6.9 kB
Tags Python 3
SHA-256 checksum
How to use checksums
6b194a154ae17024245f93c2de179407a1c73c88e0aea0f7aefbeb59cdf96deb
BLAKE2b-256 checksum
How to use checksums
2e43549279565da319aca8c129c3cc80ead51e9c5d64b313ee7ed175f81150a8
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/6.1.0 CPython/3.13.12

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on May 25, 2026.

Transparency log

Security for pronto-kafka 0.0.1

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

Full provenance

All 2 files in this release have been uploaded with provenance metadata.

Select from 2 files

Attestation for pronto_kafka-0.0.1.tar.gz

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on May 25, 2026.

Transparency log

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

To learn more about protecting yourself from malicious actors, visit our package security documentation.

Release history Release notifications | RSS feed

This release

0.0.1 This release

2 release files Full provenance

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page