I’m working with the OpenAI API and need help maintaining conversation history between multiple requests.
import openai
openai.api_key = my_secret_key
user_message = "create a limerick for me"
api_response = openai.Completion.create(model="text-davinci-002",
prompt=user_message,
max_tokens=75)
print(api_response)
This works fine and returns a limerick. But when I want to follow up with something like “make it funnier”, the API doesn’t remember what we were talking about before. In ChatGPT web interface, it keeps track of our conversation automatically. How can I achieve the same thing in my Python code? I see there’s an ID field in the response - does that help with conversation continuity?
You’ve got to manually track the conversation history and send it with each request. That ID in the response is just for that specific completion - it won’t track conversations.
Here’s how you build up the context with a messages array:
conversation = [
{"role": "user", "content": "create a limerick for me"},
{"role": "assistant", "content": "[previous AI response goes here]"},
{"role": "user", "content": "make it funnier"}
]
response = openai.ChatCompletion.create(
model="gpt-3.5-turbo",
messages=conversation
)
Just append each user message and AI response to this array before your next call.
But honestly, managing conversation state manually gets messy fast with multiple users or complex workflows. I’ve been using Latenode for this because it handles conversation context automatically.
Latenode sets up persistent conversation flows that remember context between API calls - no manual array management. It saves conversation state and handles OpenAI integration seamlessly.
Your problem’s with how you’re making API calls. OpenAI’s API doesn’t remember anything - each request starts fresh. Had this exact issue building a customer service bot. You need to keep a conversation array on your end and send the full chat history with every request. Switch to ChatCompletion too since it’s made for back-and-forth conversations. Here’s what others missed - add logic to cut old messages when you hit token limits. I keep the system message, last few exchanges, and any important stuff from earlier. Skip this and your costs will explode on long chats. If you need conversations to survive app restarts, throw the state in a database. Redis works great for temporary chat storage.
yeah, ur on the old completion endpoint - that won’t track convos. Switch to chat completions and keep all messages in an array. Just add each user and assistant msg, then send it all back. just a heads up - it can add up with those tokens quick!
You’re using the old Completion API - that’s your problem. It doesn’t track conversations at all. Switch to ChatCompletion API instead.
That ID you’re seeing? It’s just a random identifier for each API call, nothing more. You’ve got to handle conversation memory yourself by keeping all messages in a list. Every new request needs the full chat history sent along with it.
Warning though - tokens pile up fast this way since you’re resending everything each time. Found this out the hard way when my usage exploded overnight.
Once conversations get long, you’ll need to trim them or you’ll hit token limits. I stick with the system message plus the last 10-15 back-and-forth exchanges. Keeps enough context without breaking the bank.