A follow-up question often makes sense only alongside the conversation that came before it. Ollama's api/chat endpoint accepts role-based messages and returns an assistant reply that the caller can retain for the next turn.
Each request supplies its own conversation history in messages. Keeping the returned assistant message between the earlier user turn and the new question preserves what the model actually said, rather than substituting an assumed answer.
A local Ollama server and an installed chat model are required. A POSIX shell, curl, and Python 3 provide a client without an SDK or an API key; disabling streaming makes each response a single JSON object.
Steps to run a chat request with the Ollama API:
- Confirm that qwen2.5:0.5b is installed on the local Ollama server.
$ ollama list NAME ID SIZE MODIFIED granite3.2-vision:2b 3be41a661804 2.4 GB 2 hours ago all-minilm:latest 1b226e2802db 45 MB 3 hours ago qwen3:0.6b 7df6b6e09427 522 MB 3 hours ago qwen2.5:0.5b a8b0c5157701 397 MB 3 hours ago
The model name must appear in the list before sending a request. A connection error means the server is unavailable.
Related: How to start the Ollama server
Related: How to pull an Ollama model - Save the opening user turn as chat-request.json.
- chat-request.json
{ "model": "qwen2.5:0.5b", "messages": [ {"role": "user", "content": "What is 17 plus 25? Answer in one short sentence."} ], "stream": false, "options": {"temperature": 0} }
stream: false returns one complete JSON object instead of newline-delimited chunks. A temperature of zero reduces variation in the arithmetic replies.
- Send the opening request into chat-response.json.
$ curl --fail-with-body --silent --show-error http://127.0.0.1:11434/api/chat \ --output chat-response.json \ -H 'Content-Type: application/json' \ --data-binary @chat-request.json
The response file contains the API result. An HTTP error makes curl exit unsuccessfully; its error body remains available in the file.
- Read the completed assistant reply from chat-response.json.
$ python3 - <<'PY' import json from pathlib import Path response = json.loads(Path("chat-response.json").read_text()) assert response["done"] is True and response["done_reason"] == "stop", response message = response["message"] assert message["role"] == "assistant" and message["content"].strip(), response print(message["content"]) PY 17 plus 25 equals 42.
message.content holds the answer. An empty answer, missing field, or incomplete response stops the check instead of being treated as a successful reply.
- Build chat-followup.json with the saved assistant message and a context-dependent question.
$ python3 - <<'PY' import json from pathlib import Path request = json.loads(Path("chat-request.json").read_text()) response = json.loads(Path("chat-response.json").read_text()) request["messages"].append(response["message"]) request["messages"].append({"role": "user", "content": "Repeat your previous answer exactly."}) Path("chat-followup.json").write_text(json.dumps(request, indent=2) + "\n") PYThe message order is user, returned assistant, then user. The new user message omits the original question and answer, so its reply depends on the earlier messages.
- Submit the conversation in chat-followup.json.
$ curl --fail-with-body --silent --show-error http://127.0.0.1:11434/api/chat \ --output chat-followup-response.json \ -H 'Content-Type: application/json' \ --data-binary @chat-followup.json
- Confirm that the follow-up repeats the saved assistant reply.
$ python3 - <<'PY' import json from pathlib import Path response = json.loads(Path("chat-followup-response.json").read_text()) assert response["done"] is True and response["done_reason"] == "stop", response message = response["message"] assert message["role"] == "assistant", response previous = json.loads(Path("chat-response.json").read_text())["message"] assert message["content"] == previous["content"], response print(message["content"]) PY 17 plus 25 equals 42.
The saved request and both responses remain available for the next turn. The residency setting controls how long a model stays loaded.
Related: How to keep an Ollama model loaded between requests
Related: How to list running Ollama models