API Reference

kafka-python

A Python client for Apache Kafka supporting producers, consumers, and administrative operations.

Official Documentation ↗
CompatibilityThis documentation covers version v3.0.12. Check the official docs for the latest API surface.

Installation

pippip install kafka-python

Methods

kafka-python implements the Kafka protocol in pure Python for publishing and consuming records.

Key Features

  • Producer with batching and acks
  • Consumer groups and offset control
  • Administrative client for topics

Usage Notes

Pin a version and check the official docs for the API surface you depend on before upgrading.

Errors

The official API reference declares the following error types:

  • KafkaConfigurationError (显式 enable_idempotence=True 或提供 transactional_id 且配置冲突时抛出) — official text: “When the user explicitly sets enable_idempotence=True (or supplies transactional_id), any such conflict raises KafkaConfigurationError instead.” (source: https://kafka-python.readthedocs.io/en/master/apidoc/KafkaProducer.html, fetched 2026-11-08T20:07:00+08:00)
  • KafkaTimeoutError (动态版本探测在 init 中无法建立连接时抛出) — official text: “Note: Dynamic version checking is performed eagerly during init and can raise KafkaTimeoutError if no connection can be made before timeout (see bootstrap_timeout_ms below).” (source: https://kafka-python.readthedocs.io/en/master/apidoc/KafkaConsumer.html, fetched 2026-11-08T20:07:00+08:00)

API reference evidence

Fetched from official sources on 2026-11-08T20:07:00+08:00 via WebFetch https://kafka-python.readthedocs.io/en/master/apidoc/KafkaProducer.html;WebFetch https://kafka-python.readthedocs.io/en/master/apidoc/KafkaConsumer.html;WebFetch https://kafka-python.readthedocs.io/en/master/_modules/kafka/producer/kafka.html.

FAQ

Which library should I use?
kafka-python is pure Python and easy to deploy; confluent-kafka wraps librdkafka and is faster but needs the native library.
How do I guarantee delivery?
Set acks="all" and retries, and wait on the send future so failures surface instead of being lost in the background.
How are offsets committed?
Enable auto commit or call commit manually after processing; manual commits give you control over duplicates.