Skip to main content

aiokafka-foundation-kit

PyPI Python License CI codecov Docs

Async Kafka foundation library — settings, client factories, lifecycle helpers, topic management and DI wiring on top of aiokafka.

Features

  • AsyncIO-first — everything is built on aiokafka; there is no synchronous mirror
  • Protocol-based config — every entry point takes a typing.Protocol, so a Pydantic model, a dataclass or a stub all work
  • Pydantic settings — BaseKafkaProducerSettings / BaseKafkaConsumerSettings with validation, via the models extra
  • Lifecycle helpers — producer_lifecycle / consumer_lifecycle own start and stop, including on failure
  • Topic management — ensure_topics_async creates topics idempotently before a producer starts
  • Health checks — check_kafka_health_async probes broker reachability
  • JSON serialisation — dumps_bytes / loads_bytes, with transparent orjson acceleration
  • DI integration — Dishka providers and dependency-injector containers
  • OpenTelemetry — instrument_aiokafka wires the aiokafka instrumentor in one call
  • Type-safe — full annotations, checked with mypy

Installation

# Core functionality
pip install aiokafka-foundation-kit

# With all optional dependencies
pip install aiokafka-foundation-kit[all]

# Or specific features
pip install aiokafka-foundation-kit[models,orjson,dishka,dependency-injector,telemetry]

Quick Start

Producer

from aiokafka_foundation_kit import TopicConfig, producer_lifecycle
from aiokafka_foundation_kit.contrib.models import BaseKafkaProducerSettings

settings = BaseKafkaProducerSettings(bootstrap_servers="localhost:9092")
topics = [TopicConfig(name="events", num_partitions=3, replication_factor=1)]

async with producer_lifecycle(settings, topics=topics, auto_create_topics=True) as producer:
    # dicts are serialised to JSON; keys must already be bytes
    await producer.send_and_wait("events", {"event": "user_created", "user_id": "123"})

producer_lifecycle yields aiokafka's own AIOKafkaProducer, started, and stops it when the block exits — including when the block raises.

Consumer

from aiokafka_foundation_kit import consumer_lifecycle
from aiokafka_foundation_kit.contrib.models import BaseKafkaConsumerSettings

settings = BaseKafkaConsumerSettings(
    bootstrap_servers="localhost:9092",
    group_id="my-group",
)

async with consumer_lifecycle(settings, topics=("events",)) as consumer:
    async for message in consumer:
        print(message.value)   # already decoded from JSON
        await consumer.commit()

enable_auto_commit defaults to False, so commit after processing to get at-least-once delivery. The async for loop is yours to write: this library manages the client, not the message flow.

Documentation

Full documentation at bedrock-python.github.io/aiokafka-foundation-kit.

For AI agents is the whole library on one page — the API surface, the rules that break code when they are broken, the mistakes models make, and a map of which page to fetch for the rest — written to be handed to a coding assistant.

Development

# Install dependencies
uv sync --group dev --all-extras

# Lint, format check and mypy
make check

# Run tests
make test-unit  # unit tests, no Docker required
make test       # unit + integration tests, requires Docker

License

Apache 2.0 — see LICENSE for details.

Metadata

Release files for aiokafka-foundation-kit 0.2.0

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

Source distribution (sdist)

Source distribution for aiokafka-foundation-kit 0.2.0
File Size Uploaded
aiokafka_foundation_kit-0.2.0.tar.gz 26.3 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for aiokafka-foundation-kit 0.2.0
File Interpreter ABI Platform
aiokafka_foundation_kit-0.2.0-py3-none-any.whl Python 3 none any Details

Total release size: 69.5 kB

Release files / aiokafka_foundation_kit-0.2.0.tar.gz

Download URL aiokafka_foundation_kit-0.2.0.tar.gz
Size 26.3 kB
Tags Source
SHA-256 checksum
How to use checksums
575a87a9951941d1f06f7cbf5073ef0cd49ce980770eb73c018f8a9a968f4cd2
BLAKE2b-256 checksum
How to use checksums
fe9237a8d2f3591e26ce929f4c80b895823b22ecd2af29591294ed5b66705a6a
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via uv/0.13.1 {"installer":{"name":"uv","version":"0.13.1","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"Ubuntu","version":"24.04","id":"noble","libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":true}

Release files / aiokafka_foundation_kit-0.2.0-py3-none-any.whl

Download URL aiokafka_foundation_kit-0.2.0-py3-none-any.whl
Size 43.2 kB
Tags Python 3
SHA-256 checksum
How to use checksums
3e40bde23b9a552030509559cb77588334963ed3d7bd03d58d7f025096a5a092
BLAKE2b-256 checksum
How to use checksums
2ad9c5b52dee7b2c516c759c741af1571d260a82f5c4ee42b2c9cb0edbeb99ab
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via uv/0.13.1 {"installer":{"name":"uv","version":"0.13.1","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"Ubuntu","version":"24.04","id":"noble","libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":true}

Release history Release notifications | RSS feed

This release

0.2.0 This release

2 release files

0.1.3

2 release files

0.1.2

2 release files

0.1.1

2 release files

0.1.0

2 release files

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