Add Conversation History in AI Chatbot (Lesson 14)

What You Will Learn

In this lesson, you will learn:

  • Add conversation history in an AI Chatbot using Python.
  • What conversation history is
  • Why your chatbot forgets previous questions
  • How to store previous messages
  • How to make your chatbot remember the conversation

Quick Answer

Conversation History is a list of previous messages exchanged between the user and the AI.

By sending the previous conversation along with the new question, the AI can understand the context and give better answers.


The Problem

Run your chatbot and try this conversation.

You: My name is Rahul.

AI: Nice to meet you, Rahul.

You: What is my name?

AI: I don't know your name.

Why?

Because every API request is independent.

The AI only receives your latest question.

It does not automatically remember what happened earlier.


How Can We Fix This?

The latest OpenAI Responses API allows us to continue a conversation by using the previous response ID. Instead of sending the entire conversation again, we tell the API:

“Continue from the last response.”

This makes the code simpler and easier to understand.


Step 1: Create a Variable

Open app.py.

After creating the OpenAI client, add this line.

previous_response_id = None

What does this variable do?

It stores the ID of the previous AI response.

When the chatbot starts, there is no previous conversation.

That’s why the value is None.


Step 2: Find This Code

From the previous lesson, your API request looks similar to this.

response = client.responses.create(
    model="gpt-5",
    instructions="""
You are a friendly Python teacher.

Explain every topic in simple English.

Keep your answers short.

Always include an example whenever possible.
""",
    input=user_message
)

We’re going to improve this.


Step 3: Add the Previous Response ID

Update the code like this.

response = client.responses.create(
    model="gpt-5",
    instructions="""
You are a friendly Python teacher.

Explain every topic in simple English.

Keep your answers short.

Always include an example whenever possible.
""",
    input=user_message,
    previous_response_id=previous_response_id
)

What changed?

We added one new line.

previous_response_id=previous_response_id

If there is a previous conversation, the AI will continue from it.

If the value is None, the AI starts a new conversation.


Step 4: Save the Latest Response ID

After printing the AI response, add this line.

previous_response_id = response.id

Every AI response has its own unique ID.

We save that ID so the next request can continue the same conversation.


Complete Code

from openai import OpenAI

client = OpenAI(
    api_key="YOUR_API_KEY"
)

previous_response_id = None

while True:

    user_message = input("You: ")

    if user_message.lower() == "exit":
        print("Goodbye!")
        break

    response = client.responses.create(
        model="gpt-5",
        instructions="""
You are a friendly Python teacher.

Explain every topic in simple English.

Keep your answers short.

Always include an example whenever possible.
""",
        input=user_message,
        previous_response_id=previous_response_id
    )

    print("\nAI:", response.output_text)

    previous_response_id = response.id

Try It Yourself

Run the chatbot.

Now try this conversation.

You: My name is Rahul.

AI: Nice to meet you, Rahul.

You: I am learning Python.

AI: That's great!

You: What is my name?

AI: Your name is Rahul.

The chatbot now understands the earlier conversation.


Why Is This Better?

In the previous version, we manually stored every message.

In this version:

  • The code is shorter.
  • It is easier to understand.
  • It follows the latest Responses API conversation flow.

As a beginner, you only need to remember one thing:

Save the previous response ID and use it in the next request.


Common Mistakes

The chatbot still forgets previous messages

Check whether you added:

previous_response_id=response.id

after printing the response.


Every conversation starts from the beginning

Make sure you pass:

previous_response_id=previous_response_id

inside client.responses.create().


previous_response_id is created inside the loop

It should be created before the while loop.

Otherwise, it will become None every time the loop runs.


Key Points

  • Conversation History helps the AI understand context.
  • The latest Responses API supports multi-turn conversations.
  • Save the previous response ID after every request.
  • Use the previous response ID in the next request.

Mini Quiz

1. What is Conversation History?

A. A Python package

B. Remembering previous messages

C. A database

D. A model

Answer: B


2. Which variable stores the previous response?

A. response_text

B. previous_response_id

C. user_message

D. history

Answer: B


3. What should you pass in the next API request?

Answer: previous_response_id


4. Where should you save the latest response ID?

After printing the AI response.


5. Why do we use Conversation History?

A. To make the chatbot understand previous messages.

Answer: A


Practice Exercise

Run your chatbot and try this conversation.

My name is Aman.

I live in Delhi.

What is my name?

Where do I live?

If the chatbot answers both questions correctly, your Conversation History is working.


Lesson Summary

Great job!

Your chatbot can now remember previous messages.

Instead of treating every question as a new conversation, it now continues the same conversation using the latest Responses API.

This makes your chatbot smarter and much more natural.



Written by Shubhranshu Shekhar, who has trained 20000+ students in coding.

Scroll to Top