Batch scripts often need a finished piece of text before their next action can run. Ollama's api/generate endpoint accepts a single prompt and returns generated text in the response field.

By default, Ollama streams newline-delimited JSON chunks. A non-streaming response keeps the complete answer and its completion metadata in one JSON object, ready to save or pass to another program.

The local Ollama server must already be running with a text model installed. A POSIX shell, curl with support for –fail-with-body, and Python 3 are required to save and read the response.

Steps to generate text with the Ollama API:

  1. 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    3 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 generation. A connection error means the server is unavailable.
    Related: How to start the Ollama server
    Related: How to pull an Ollama model

  2. Save a non-streaming generation response as generate-response.json.
    $ curl --fail-with-body --silent --show-error http://127.0.0.1:11434/api/generate \
      -H 'Content-Type: application/json' \
      --output generate-response.json \
      --data-binary '{
        "model": "qwen2.5:0.5b",
        "prompt": "What is 17 plus 25? Answer in one short sentence.",
        "stream": false,
        "options": {"temperature": 0}
      }'

    stream: false requests one JSON object; a temperature of zero reduces variation in the reply. An HTTP error makes curl exit unsuccessfully while retaining the error body in the file. Do not continue with a failed request.

  3. Extract the completed answer from generate-response.json.
    $ python3 - <<'PYTHON'
    import json
    from pathlib import Path
     
    payload = json.loads(Path("generate-response.json").read_text())
    if "error" in payload:
        raise SystemExit(payload["error"])
    assert payload["done"] is True and payload["done_reason"] == "stop", payload
    answer = payload["response"]
    assert isinstance(answer, str) and answer.strip(), payload
    print(answer)
    PYTHON
    17 plus 25 equals 42.

    The parser reads the saved API result, rejects an error or incomplete answer, and prints the generated text. A done_reason of length means generation reached its output limit. The response file remains available for the next program; the exact wording can vary between models and releases.

Share this guide