How to create topic in Kafka with Python-kafka admin client?
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 to create Kafka topics from Python, the KafkaAdminClient class in kafka-python is the standard administrative API. It lets you declare a topic name, partition count, and replication factor without shelling out to Kafka CLI tools.
The main thing to understand is that topic creation is a cluster operation, not just a local client call. The request succeeds only if the broker is reachable, the cluster has enough brokers for the requested replication factor, and your user has permission to create topics.
Basic Topic Creation with KafkaAdminClient
The core workflow is short:
- create an admin client
- build one or more
NewTopicobjects - call
create_topics - close the client
Here is a runnable example:
This is enough for a local broker or a simple development cluster.
Understanding the Important Parameters
A NewTopic instance needs three main values:
- '
name: the Kafka topic name' - '
num_partitions: how many partitions the topic should have' - '
replication_factor: how many broker copies each partition should keep'
Choose these deliberately. More partitions can increase parallelism, but they also increase metadata and coordination overhead. The replication factor must not exceed the number of brokers that are eligible to host replicas.
If your cluster has only one broker, replication_factor=3 will fail no matter how correct the Python code is.
Validating Before Creating
Kafka supports a validation mode. When validate_only=True, the broker checks whether the request is valid without actually creating the topic.
This is useful in deployment tooling where you want a preflight check before applying changes.
Handling Errors Cleanly
Real systems need better error handling than a single happy-path call. The most common failures are:
- topic already exists
- broker unavailable
- authorization failure
- invalid replication factor or partition count
A practical pattern is to catch the specific “already exists” case and let other Kafka errors surface with enough detail for logs and alerts.
When to Create Topics in Code
Creating topics in application code is convenient for local development, tests, and temporary environments. In production, many teams prefer infrastructure-managed topic creation so that naming, retention, replication, and access policies are reviewed centrally.
That is a deployment decision, not a Python limitation. The KafkaAdminClient API is still useful for internal tools, smoke tests, and admin jobs.
Common Pitfalls
- Requesting a replication factor larger than the number of available brokers.
- Forgetting to close the admin client, which leaves open network resources longer than necessary.
- Treating topic creation as idempotent without handling
TopicAlreadyExistsError. - Using application code to create production topics without coordinating partition count, retention, and ACL policy.
- Assuming a client-side call is enough when the connected principal lacks permission to create topics.
Summary
- '
KafkaAdminClientplusNewTopicis the normal Python approach for creating Kafka topics.' - The minimum required settings are the bootstrap servers, topic name, partition count, and replication factor.
- '
validate_only=Trueis useful for preflight checks.' - Production failures are usually cluster or permission issues rather than Python syntax issues.
- Handle “already exists” separately and always close the admin client.
Related reading
- How to create topics in apache kafka?
- How to create unit test with kafka embedded in the spring cloud stream
- How to create ZeroMQ socket suitable both for sending and consuming?
- How to deal with Kafka warning Error while loading kafka-streams-version.properties java.lang.NullPointerException inStream parameter is null
- How to crop an image in OpenCV using Python
- How to crop an image using PIL?
- How to decide Kafka Cluster size
- How to decide the concurrency to be set in spring kafka?

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.