All guides

Keep a multi-turn conversation going with the Ollama chat API

Maintain the messages array yourself in the local /api/chat: send a first question, append the reply and a follow-up, resend, and check the model picked up the context.

AINavWritten / Updated Sources checked
On this page

At a glance

Ollama's /api/chat describes messages as the chat history, an array of objects that each carry a role and content. For the model to continue from the first turn in the second, you maintain that array yourself: each round, include the earlier question and the model's reply, then append the new question. This guide walks through a synthetic task: ask for a name for a weekly plant-watering reminder, then ask to shorten it to two words. It also shows how to put the reply safely into JSON. It is based only on the official API documentation and was not run.

Workflow

  1. First request

    Put a single user message in messages and set stream to false to get one complete reply.

  2. Escape and append

    Escape the reply as a JSON string, then write the first user message, the assistant reply and the new user message in order.

  3. Check context

    Inspect the messages actually sent, then see whether the second reply builds on turn one and done is true.

Each stage cites official sources.

Before you start

  • Ollama installed and running locally; per the docs the endpoint is http://localhost:11434/api/chat.
  • A model you can chat with. The docs example uses gemma4, but whether it exists on your machine was not checked, so substitute a model you have downloaded.
  • A terminal with curl, and a text editor to save each round's request body as a file (called round1.json and round2.json below).

1. Confirm endpoint and model name

Confirm Ollama is running locally and use the documented address http://localhost:11434/api/chat. Note the model name; the docs example is gemma4, so swap in one you have downloaded. model and messages are the required fields for this endpoint.

2. Write and send the first body

Write the first JSON in an editor: {"model":"<model name>","messages":[{"role":"user","content":"Suggest a name for a weekly plant-watering reminder"}],"stream":false}. Send it with the documented inline form: curl http://localhost:11434/api/chat -d '{"model":"<model name>","messages":[{"role":"user","content":"Suggest a name for a weekly plant-watering reminder"}],"stream":false}'. Substitute a downloaded model; this exercise uses synthetic text without single quotes.

3. Read the reply

In the response find message.role (expected assistant), message.content, done and done_reason. Copy message.content; it is the assistant content you will send back. It may contain double quotes, backslashes or line breaks.

4. Turn the reply into a valid JSON string

Before placing the reply in the content field of round2.json, apply JSON rules: write a double quote as \", a backslash as \\, and a line break as \n. Unescaped double quotes, backslashes or control characters can invalidate JSON; ordinary text may already be safe. The parsed content must equal the original message.content from round one. It is safer to open round2.json in an editor that validates JSON and confirm there are no syntax errors before sending.

5. Build the second messages array

Put three entries in order: the first user message unchanged; role assistant with the escaped reply as content; role user with "Shorten the name to two words". Do not reorder or rewrite earlier entries, and keep model and stream the same as in round one.

6. Send, check and stop

Use the documented curl http://localhost:11434/api/chat -d inline form again, placing the complete JSON below inside single quotes. If the actual reply contains a single quote, stop this terminal exercise and use a JSON request tool you have independently verified; do not paste it directly into a single-quoted argument. Read message.content and see whether it reworks the first name. If not, check the messages actually sent for a missing assistant entry or truncated content before switching models, since a model can answer poorly even when it received the history. Stopping means sending no more requests; deleting the two JSON files removes your local copy. To unload the model at once, the docs list keep_alive, where 0 unloads immediately; add "keep_alive": 0.

Original example

Synthetic sample: round2.json is below. Angle-bracket parts are placeholders for the escaped reply you actually received in round one; no simulated output is given.

json
{
  "model": "<your model name>",
  "messages": [
    {"role": "user", "content": "Suggest a name for a weekly plant-watering reminder"},
    {"role": "assistant", "content": "<escaped round-one message.content>"},
    {"role": "user", "content": "Shorten the name to two words"}
  ],
  "stream": false
}

To send, use curl http://localhost:11434/api/chat -d with the complete JSON above as a single-quoted inline argument; follow the stop condition if it contains a single quote.

Completion checks

  • The parsed assistant content equals the original message.content from round one. round2.json is valid JSON, messages run user, assistant, user, and the assistant content matches the received message.content (after escaping).
  • The response shows done as true; check done_reason too. done only means the response finished, not that it is correct.
  • Does the second reply build on the first name? If unrelated, inspect the messages actually sent before deciding whether it is a model issue or missing history.
  • The files hold only synthetic questions, with no real names or addresses.

Limits

  • The docs only say messages is a chat history array; they do not say whether the server stores or retains a session, so this guide sends the history with every request. Deleting files removes only your local copy.
  • Longer history means a larger body; this page gives no context-length limit or overflow behavior, so look elsewhere for that.
  • tools, format, think and logprobs are out of scope here.
  • The API does not guarantee reply quality; judge it yourself.
  • The inline terminal exercise is limited to synthetic text without single quotes; arbitrary model replies must not be pasted directly into shell arguments.

Verification scope

Only the official Ollama /api/chat documentation was read. Login, installation, product API execution, file upload and generated output were not tested; the request body is an original sample built from documented fields and was not run.

Article sources

Original editorial content; official descriptions and checking suggestions are distinguished.

  1. Ollama · Official sources
    View details