Text of realpython.com › build-a-chatbot-python-chatterbot as Styx crawled it on Nov 20, 2026. The page may have changed since. Only its words are kept, without pictures or layout, and only the first part of a long page.

Open the live page

ChatterBot: Build a Chatbot With Python – Real Python

ChatterBot: Build a Chatbot With Python by Martin Breuss Updated May 06, 2026 Reading time estimate 36m intermediate data-science projects Table of Contents Remove ads The Python ChatterBot library lets you build a self-learning command-line chatbot with just a few lines of code. You’ll set up a basic bot, clean real WhatsApp conversation data with regular expressions, and train your chatbot on that custom corpus. You’ll also plug in a local LLM through Ollama to augment its replies with contextual knowledge.

By the end of this tutorial, you’ll understand that: ChatterBot is a Python library that combines text processing, machine learning, and a local database to generate chatbot replies. A minimal ChatterBot script instantiates ChatBot , collects user input in a loop, and returns matching responses through .get_response() . Training with ListTrainer and default settings stores conversation pairs in a SQLite database that ChatterBot queries with Levenshtein distance to pick each reply.

ChatterBot can call a local LLM through OllamaLogicAdapter , voting against other logic adapters with a confidence score. ChatterBot was revived in 2025 with spaCy-based NLP , CSV and JSON trainers, and experimental LLM support. Along the way, you’ll move from a potted plant that can only echo hello to a chatbot that chats knowledgeably about houseplants. You can follow along with your own WhatsApp export or grab the provided sample data below.

Get Your Code: Click here to download the free sample code that you’ll use to build a chatbot with Python’s Chatterbot. Take the Quiz: Test your knowledge with our interactive “ChatterBot: Build a Chatbot With Python” quiz. You’ll receive a score upon completion to help you track your learning progress: Interactive Quiz ChatterBot: Build a Chatbot With Python Test your understanding of the ChatterBot Python library, from training a basic bot with ListTrainer to wiring in a local LLM through Ollama.

Preview the Chatbot At the end of this tutorial, you’ll have a command-line chatbot that can respond to your inputs with semi-meaningful replies: You’ll achieve that by preparing WhatsApp chat data and using it to train the chatbot. Beyond learning from your automated training, the chatbot will improve over time as it gets more exposure to questions and replies from user interactions. Remove ads Project Overview The ChatterBot library combines text processing, machine learning algorithms, and data storage and retrieval to allow you to build flexible chatbots.

You can build an industry-specific chatbot by training it with relevant data. Additionally, the chatbot will remember user responses and continue building its internal graph structure to improve the responses that it can give. Note: After a long hiatus, ChatterBot was revived in early 2025 with support for modern Python, new training formats for CSV and JSON data, and even experimental LLM integration. Under the hood, ChatterBot now uses spaCy for language processing, which gives it a more robust NLP pipeline than before.

If you want to develop an LLM-first chatbot, Real Python’s LLM Application Development With Python learning path takes you through the concepts and libraries step by step: Learning Path LLM Application Development With Python 14 Resources ⋅ Skills: OpenAI, Ollama, OpenRouter, Prompt Engineering, LangChain, LlamaIndex, ChromaDB, MarkItDown, RAG, Embeddings, AI Agents, Pydantic AI, LangGraph, MCP In this tutorial, you’ll start with an untrained chatbot that’ll showcase how quickly you can create an interactive chatbot using Python’s ChatterBot.

You’ll also notice how small the vocabulary of an untrained chatbot is. Next, you’ll learn how you can train such a chatbot and check on the slightly improved results. The more plentiful and high-quality your training data is, the better your chatbot’s responses will be. Therefore, you’ll either fetch the conversation history of one of your WhatsApp chats or use the provided chat.txt file that you can download here: Get Your Code: Click here to download the free sample code that you’ll use to build a chatbot with Python’s Chatterbot.

It’s rare that input data comes exactly in the form you need, so you’ll clean the chat export data to get it into a useful input format. This process will show you some tools you can use for data cleaning, which may help you prepare other input data to feed to your chatbot. After data cleaning, you’ll retrain your chatbot and give it another spin to experience the improved performance. Finally, you’ll hook a local LLM into your chatbot to augment the variety and contextual relevance of its responses.

When you work through this process from start to finish, you’ll get a good idea of how you can build and train a Python chatbot with the ChatterBot library so that it can provide an interactive experience with relevant replies. Prerequisites Before you get started, make sure that you have Python 3.10 or later installed, which is the minimum Python version that ChatterBot supports. If you need help setting up Python, check out Python 3 Installation & Setup Guide .

Once you have a suitable Python version, you’re ready to get started. You’ll touch on a handful of Python concepts while working through the tutorial: Conditional statements while loops for iteration Lists and tuples Python functions Substring checks and substring replacement File input/output Python comprehensions and generator expressions Regular expressions (regex) using re If you’re comfortable with these concepts, then you’ll probably be comfortable writing the code for this tutorial.

If you don’t have all of the prerequisite knowledge before starting this tutorial, that’s okay! In fact, you might learn more by going ahead and getting started. You can always stop and review the resources linked here if you get stuck. Step 1: Create a Chatbot Using Python ChatterBot In this step, you’ll set up a virtual environment and install the necessary dependencies. You’ll also create a working command-line chatbot that can reply to you—but it won’t have interesting replies for you yet.

To get started with your chatbot project, create and activate a virtual environment , then install chatterbot : Language: Shell $ python -m venv venv $ source venv/bin/activate (venv) $ python -m pip install chatterbot Running this command in your terminal application installs ChatterBot and its dependencies into a new Python virtual environment. After the installation is complete, running python -m pip freeze should bring up a list of installed dependencies that’s similar to what you can find in the provided sample code’s requirements.txt file: Get Your Code: Click here to download the free sample code that you’ll use to build a chatbot with Python’s Chatterbot.

With the installation out of the way, you’re ready to get started! Create a new Python file, call it bot.py , and add the code that you need to get a basic chatbot up and running: Language: Python Filename: bot.py 1 from chatterbot import ChatBot 2 3 chatbot = ChatBot ( "Chatpot" ) 4 5 exit_conditions = ( ":q" , "quit" , "exit" ) 6 while True : 7 query = input ( "> " ) 8 if query in exit_conditions : 9 break 10 else : 11 print ( f "🪴 { chatbot . get_response ( query ) } " ) After importing ChatBot in line 1, you create an instance of ChatBot in line 3.

The only required argument is a name, and you call this one "Chatpot" . No, that’s not a typo—you’ll actually build a chatty flowerpot chatbot in this tutorial! You’ll soon notice that pots may not be the best conversation partners after all. In line 6, you create a while loop that’ll keep looping until you enter one of the exit conditions defined in line 5. Finally, in line 11, you call .get_response() on the ChatBot instance that you created earlier and pass it the user input that you collected in line 7 and assigned to query .

The call to .get_response() in

The rest of this page is on the live site.