Kafka-python get number of partitions for topic
System Design practice on Codemia
Work through 120+ system design problems with detailed solutions, from rate limiters to multi-region storage.
Introduction
If you need the number of partitions for a Kafka topic in Python, the answer comes from topic metadata rather than from consuming messages. A Kafka client can ask the cluster for metadata about the topic and then count the partition ids returned for that topic.
Using KafkaConsumer.partitions_for_topic
With kafka-python, one of the simplest APIs is partitions_for_topic.
If the topic exists and metadata is available, partitions is typically a set of partition numbers such as {0, 1, 2}.
Why The Return Value Can Be None
A return value of None usually means one of these things:
- the topic does not exist
- metadata has not been fetched successfully yet
- the broker connection is unavailable
That is why len(partitions) should be guarded instead of assumed.
Refreshing Metadata Matters
Kafka clients cache metadata, and sometimes the answer appears stale if the topic was created or expanded very recently. Polling or explicitly waiting for metadata refresh can help in startup code.
Calling topics() is a simple way to ensure the client has performed a metadata request before you inspect the topic.
Using KafkaAdminClient For Administrative Code
If the code is administrative rather than consumer-oriented, you may prefer the admin client.
Depending on library version and broker compatibility, the exact response shape varies, but the principle is the same: count the partitions described in the metadata.
Why Partition Count Matters
Applications query partition count for several common reasons:
- validating deployment assumptions
- assigning work across processes
- calculating expected parallelism
- checking whether a topic resize took effect
This is especially common in management scripts and monitoring code.
Be Careful With Producer Partition Logic
Knowing the number of partitions is useful, but an application should still not hardcode partition ids casually unless it owns the partitioning strategy. Topic partition counts can change over time, and brittle assumptions about fixed ids can create subtle production bugs.
Use metadata to learn about the topic, not to cement static assumptions unless the design requires it.
Handle Broker Errors Gracefully
Metadata calls can fail because Kafka is unavailable, the bootstrap server list is wrong, or authentication settings are missing.
That means partition discovery code should surface errors clearly instead of quietly treating every failure as "topic has zero partitions."
Common Pitfalls
The most common mistake is assuming partitions_for_topic can never return None. Another is forgetting to close the client after a short admin-style script finishes. Developers also sometimes query metadata immediately after topic creation and assume the result is authoritative before the client has refreshed metadata. Finally, partition count is metadata, not a guarantee about current consumer lag, leader health, or application throughput.
Summary
- Use Kafka metadata APIs to discover partition count, not consumer message flow.
- '
partitions_for_topicis a simple way to get the partition ids for a topic.' - Guard against
Nonein case metadata is missing or the topic does not exist. - Refresh or trigger metadata fetch when topic state may have changed recently.
- Treat partition count as useful cluster metadata, not as a substitute for full topic health checks.
Related reading
- Kafka-python How to consume json message
- kafka-python producer is not able to connect
- kafka-python raise UnrecognizedBrokerVersion Error
- kafka-python read from last produced message after a consumer restart
- Kafka-python retrieve the list of topics
- kafka-server-stop.sh not working when Kafka started from Python script
- Kafka->Spark->Cassandra forcing data locality
- kafka-streams alert on kafka connection faliure

System Design Fundamentals
Build a strong foundation in designing scalable, reliable distributed systems.
View the courseTrack what you have practised
A free account saves your progress, solutions and study plan across every problem on Codemia.
System Design practice on Codemia
Work through 120+ system design problems with detailed solutions, from rate limiters to multi-region storage.