Multi-Turn Conversations

Keep conversations going by sending previous messages along with each new request.

Multi-turn conversations enable you to build chat experiences where the model can refer to and respond within the context of previous messages, instead of treating every request as a new conversation.

The Chat Completions API is stateless by design: it does not automatically retain any memory or context of earlier requests. To continue a conversation, resend the full conversation history with each request. Your application is responsible for managing that history and including the relevant messages.


How conversations work

A conversation is represented as a list of messages. Each message has a role and content.

Common roles include:

  • user: Contains a message from the user.
  • assistant: Contains a previous response from the model.

To continue a conversation, include the previous messages along with the new user message in your next request.


Continue a conversation

Python

The following example sends two requests:

import os
from openai import OpenAI

client = OpenAI(
    api_key="DEPLOY_API_KEY",
    base_url="https://api-cdn.deploy.com/api/v1",
)

model = "DEPLOY_MODEL_ID"

# Start a conversation
messages = [
    {"role": "user", "content": "Suggest a vacation destination from SFO with a flight time shorter than 12 hours."},
]

response = client.chat.completions.create(
    model=model,
    messages=messages,
)

assistant_message = response.choices[0].message
print("Assistant:", assistant_message.content)

# Add the assistant's reply to the conversation history
messages.append({
    "role": "assistant",
    "content": assistant_message.content,
})

# Add a follow-up message
messages.append({
    "role": "user",
    "content": "Give me five more.",
})

# Resend the complete conversation history
response = client.chat.completions.create(
    model=model,
    messages=messages,
)

print("Assistant:", response.choices[0].message.content)

The second request includes all three messages:

  1. The original user request.
  2. The assistant's response.
  3. The user's follow-up request.

Because the model receives the conversation history containing the earlier exchange, it can answer the follow-up in context.

cURL

With cURL, you can achieve the same result by sending the conversation history in the messages array.

curl "https://api-cdn.deploy.com/api/v1/chat/completions" \
  -H "Authorization: Bearer $DEPLOY_API_KEY" \
  -H "Content-Type: application/json" \
  -d '{
    "model": "DEPLOY_MODEL_ID",
    "messages": [
      {"role": "user", "content": "Suggest a vacation destination from SFO with a flight time shorter than 12 hours."},
      {"role": "assistant", "content": "**Tokyo, Japan** is a great vacation destination from SFO. Nonstop flights from San Francisco to Tokyo (HND/NRT) typically take about **10.5–11.5 hours**, which is under your 12-hour limit."},
      {"role": "user", "content": "Give me five more."}
    ]
  }'

Streaming

To use multi-turn conversations with streaming, send the conversation history in messages and set stream: true in your request. As the response arrives, collect the assistant's text and append the completed response to the conversation history before sending the next turn.

See Streaming for details on handling streamed responses.


Tool calling

A tool-calling exchange adds an assistant message containing tool calls and one or more tool-result messages. See Tool Calling for a complete example. Don’t reduce a tool-call exchange to plain text if you expect the model to use the result correctly.