Kafka
Python
Programming
Kafka Admin Client
Topic Creation

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.

Practice system design

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:

  1. create an admin client
  2. build one or more NewTopic objects
  3. call create_topics
  4. close the client

Here is a runnable example:

python
1from kafka.admin import KafkaAdminClient, NewTopic
2from kafka.errors import TopicAlreadyExistsError
3
4
5def create_topic():
6    admin = KafkaAdminClient(
7        bootstrap_servers="localhost:9092",
8        client_id="topic-creator",
9    )
10
11    topic = NewTopic(
12        name="orders.events",
13        num_partitions=3,
14        replication_factor=1,
15    )
16
17    try:
18        admin.create_topics(new_topics=[topic], validate_only=False)
19        print("topic created")
20    except TopicAlreadyExistsError:
21        print("topic already exists")
22    finally:
23        admin.close()
24
25
26if __name__ == "__main__":
27    create_topic()

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.

python
1from kafka.admin import KafkaAdminClient, NewTopic
2
3admin = KafkaAdminClient(bootstrap_servers="localhost:9092", client_id="validator")
4
5candidate = NewTopic(name="payments.events", num_partitions=6, replication_factor=1)
6admin.create_topics(new_topics=[candidate], validate_only=True)
7admin.close()

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.

python
1from kafka.admin import KafkaAdminClient, NewTopic
2from kafka.errors import KafkaError, TopicAlreadyExistsError
3
4admin = KafkaAdminClient(bootstrap_servers="localhost:9092", client_id="safe-creator")
5
6try:
7    admin.create_topics([
8        NewTopic(name="inventory.events", num_partitions=3, replication_factor=1)
9    ])
10except TopicAlreadyExistsError:
11    print("inventory.events already exists")
12except KafkaError as exc:
13    print(f"topic creation failed: {exc}")
14finally:
15    admin.close()

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

  • 'KafkaAdminClient plus NewTopic is 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=True is 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
Course
Beginner
27 lessons
10 hours
System Design Fundamentals

Build a strong foundation in designing scalable, reliable distributed systems.

View the course
Track 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.

Practice system design

All Rights Reserved.