Keep multi-turn messages for the DeepSeek API
Understand that /chat/completions is stateless: each turn, send the prior history, the model's last reply and the new question together in messages.
Before you start · 1. Create the messages list · 2. Send the first request
At a glance
DeepSeek's /chat/completions API is stateless, so the server does not record the context of your earlier requests. To have the model continue from a previous answer, your program must store the conversation and send the whole history again with every request. This guide uses a small two-round dialogue to show where to keep the data, what to append each round, what to check and when to stop. The sample questions are invented and contain no real user information.
Workflow
What does round one contain?
messages holds only the first user message, which produces the first reply.
Does round two carry the history?
Round two must contain, in order: the first user message, the model's assistant reply, and the new user question.
Does your program keep storing it?
Because the server keeps no context, the list must grow inside your program each round and be passed in full on every request.
Before you start
- A Python environment that can call the DeepSeek API using the openai library, with base_url https://api.deepseek.com as in the official sample and the key kept in local configuration.
- A list variable called messages that survives between calls, and two or three related questions you plan to ask in this conversation.
1. Create the messages list
In your code, create messages with the first item in the form {"role": "user", "content": "..."}. This list is your conversation record, so keep it alive across rounds.
2. Send the first request
Call client.chat.completions.create with model and messages. The official sample uses the model name deepseek-flash; follow your current documentation.
3. Append the model reply to the end
The official sample uses messages.append(response.choices[0].message) to add the first-round output. Append the reply first and the new question second; do not swap them.
4. Append the new user question
Then append {"role": "user", "content": "new question"}. The question can depend on earlier text, such as asking about the second one, because that text is already in the list.
5. Send the next round with the full list
Call create again, still passing the entire messages list. When the reply arrives, append it to the end as well, ready for another round.
6. Print, verify, then stop
Before each send, print or log the role order and confirm it alternates user, assistant, user. When the conversation is done, stop calling and discard or clear the list; do not mix it into an unrelated session.
Original example
Invented task: use two rounds of questions to prepare a weekend hiking checklist. Round one sends: [{"role": "user", "content": "List two items for a day hike."}] After the reply, append (placeholder here, not recorded output): {"role": "assistant", "content": "<first-round model reply>"} Round two then appends: {"role": "user", "content": "Which of those is more important if I can carry only one?"} The messages actually sent in round two run in this order: user (first question), assistant (first reply), user (second question). If the middle assistant item is missing, the word those in the second question has nothing to refer to, so check whether the append was skipped.
Completion checks
- Before round two, the list holds the first question, the first reply and the second question in order.
- Each reply is appended to the end before the next question is sent.
- Every request passes the full list, not only the latest question.
- The messages variable is not recreated between rounds, which would lose history.
- Logs contain no key or real personal information.
Limits
- The official material says the server does not record context, but it does not discuss truncating or compressing a long history; this guide gives no such strategy or length limit.
- It covers only this concatenation approach for /chat/completions, not other endpoints or database storage.
- Answer accuracy depends on the model's replies, and this guide makes no guarantee about quality.
Verification scope
Only the official DeepSeek multi-round conversation documentation was read. Login, installation, product API execution, file upload and generated output were not tested; the dialogue and placeholder reply are original illustrative samples, not real run output.
Article sources
Original editorial content; official descriptions and checking suggestions are distinguished.