How to generate text with the Ollama API

The api/generate endpoint is the direct text-generation path for a single prompt. It is a good fit for scripts that do not need chat history but still need model options, streaming control, and a JSON response.

By default, Ollama streams newline-delimited JSON chunks. Set stream to false when the caller wants one complete JSON object that is easier to parse in shell scripts and tests.

Use a small prompt and a low num_predict value for smoke tests. Longer prompts and larger limits belong in application code after the endpoint, model name, and response shape are known to work.

Steps to generate text with the Ollama API:

  1. Confirm the target server is reachable.
    $ curl -s http://localhost:11434/api/version
    {"version":"0.31.1"}
  2. Send a non-streaming generation request.
    $ curl -s http://localhost:11434/api/generate -d '{
      "model": "gpt-oss:20b",
      "prompt": "Return the word OK and nothing else.",
      "think": "low",
      "stream": false,
      "options": {"num_predict": 24}
    }'
    {"model":"gpt-oss:20b","response":"OK","thinking":"Just output \"OK\".","done":true}
  3. Read the final answer from response.
    $ python3 - <<'PY'
    import json
    payload = {"response": "OK", "done": True}
    print(payload["response"])
    PY
    OK
  4. Use options for generation behavior that belongs to the request.
    $ curl -s http://localhost:11434/api/generate -d '{
      "model": "gpt-oss:20b",
      "prompt": "Return OK.",
      "stream": false,
      "options": {"temperature": 0, "num_predict": 24}
    }'
    {"response":"OK","done":true}
  5. Switch to api/chat when the application needs message history.
    $ curl -s http://localhost:11434/api/chat -d '{"model":"gpt-oss:20b","messages":[{"role":"user","content":"Reply OK."}],"stream":false}'
    {"message":{"role":"assistant","content":"OK"},"done":true}