How to implement conversation history
Quick answer
Implement conversation history by storing chat messages as a list of
messages with roles like user and assistant. Pass this list to the client.chat.completions.create method to maintain context across turns.PREREQUISITES
Python 3.8+OpenAI API key (free tier works)pip install openai>=1.0
Setup
Install the official OpenAI Python SDK and set your API key as an environment variable.
pip install openai>=1.0 output
Collecting openai Downloading openai-1.x.x-py3-none-any.whl (xx kB) Installing collected packages: openai Successfully installed openai-1.x.x
Step by step
Store conversation history as a list of messages and pass it to the chat completion call to maintain context.
import os
from openai import OpenAI
client = OpenAI(api_key=os.environ["OPENAI_API_KEY"])
# Initialize conversation history
conversation_history = [
{"role": "system", "content": "You are a helpful assistant."}
]
# User sends a message
user_message = "Hello, who won the world series in 2020?"
conversation_history.append({"role": "user", "content": user_message})
# Get assistant response
response = client.chat.completions.create(
model="gpt-4o-mini",
messages=conversation_history
)
assistant_message = response.choices[0].message.content
conversation_history.append({"role": "assistant", "content": assistant_message})
print("Assistant:", assistant_message)
# Next user message
next_user_message = "Where was it played?"
conversation_history.append({"role": "user", "content": next_user_message})
# Continue conversation with full history
response = client.chat.completions.create(
model="gpt-4o-mini",
messages=conversation_history
)
assistant_message = response.choices[0].message.content
conversation_history.append({"role": "assistant", "content": assistant_message})
print("Assistant:", assistant_message) output
Assistant: The Los Angeles Dodgers won the World Series in 2020. Assistant: The 2020 World Series was played at Globe Life Field in Arlington, Texas.
Common variations
You can implement conversation history with other models like gpt-4o-mini or use async calls. For streaming, pass stream=True and process chunks incrementally.
import asyncio
import os
from openai import OpenAI
async def async_chat():
client = OpenAI(api_key=os.environ["OPENAI_API_KEY"])
conversation_history = [
{"role": "system", "content": "You are a helpful assistant."},
{"role": "user", "content": "Hello!"}
]
# Async streaming response
stream = await client.chat.completions.create(
model="gpt-4o-mini",
messages=conversation_history,
stream=True
)
print("Assistant:", end=" ")
async for chunk in stream:
delta = chunk.choices[0].delta.content or ""
print(delta, end="", flush=True)
print()
asyncio.run(async_chat()) output
Assistant: Hello! How can I assist you today?
Troubleshooting
- If the assistant forgets context, ensure you pass the full
conversation_historylist each time. - Trim history if it grows too large to avoid token limits.
- Use roles correctly:
system,user, andassistant.
Key Takeaways
- Maintain conversation history as a list of messages with roles to preserve context.
- Always pass the full message history to
client.chat.completions.createfor coherent multi-turn dialogs. - Use streaming and async calls for responsive chat applications.
- Manage token limits by trimming or summarizing long histories.
- Roles
system,user, andassistantare essential for proper context handling.