C.W.K.
Stream
Lesson 03 of 05 · published

POST /api/generate — Raw Completion

~16 min · api, completion, fim

Level 0Downloader
0 XP0/41 lessons0/11 achievements
0/120 XP to next level120 XP to go0% complete

When to use /api/generate

/api/generate takes a single prompt string instead of a messages array. It's the right shape when:

  • You're doing fill-in-the-middle (FIM) code completion. The suffix field holds the code after the cursor; the model fills the gap.
  • You want a one-shot completion without conversation framing.
  • You need to send raw mode (raw: true) and bypass the model's chat template entirely — useful for fine-tuned models with custom prompt formats.

FIM is the killer use case

Coding-tuned models (Qwen3-Coder, Code Llama variants, DeepSeek-Coder) understand FIM tokens and can complete the middle of a function given a prefix and suffix. This is what powers local GitHub-Copilot-style autocomplete in editors like Continue.dev, Cursor (with local models), and Aider.

Other useful fields

  • images — array of base64 image strings (multimodal models).
  • options — same as /api/chat.
  • format — same as /api/chat.
  • raw — boolean; if true, no template is applied to the prompt.

Code

FIM completion with suffix·python
import httpx

# Code with a hole — the model fills the middle
prefix = '''def parse_csv(path: str) -> list[dict]:
    """Parse a CSV file and return rows as dicts."""
    with open(path, encoding="utf-8") as f:
        reader = csv.DictReader(f)
'''
suffix = '''
        return rows
'''

resp = httpx.post(
    "http://localhost:11434/api/generate",
    json={
        "model": "qwen3-coder:7b",
        "prompt": prefix,
        "suffix": suffix,
        "stream": False,
        "options": {"temperature": 0.2, "num_predict": 200},
    },
    timeout=60.0,
)
print(resp.json()["response"])
# expected: rows = [r for r in reader]
Raw mode for custom templates·python
# When you have a fine-tuned model with its own format, bypass the chat template
custom_prompt = "<|im_start|>user\nHello<|im_end|>\n<|im_start|>assistant\n"

resp = httpx.post(
    "http://localhost:11434/api/generate",
    json={
        "model": "my-custom-model",
        "prompt": custom_prompt,
        "raw": True,           # skip Ollama's template
        "stream": False,
    },
)
print(resp.json()["response"])

External links

Exercise

Implement a tiny complete(prefix, suffix, model) function using /api/generate with the suffix field. Use it to fill in the middle of three real Python snippets (a list comprehension, a try/except, a function body). Note when the suffix helps and when it confuses smaller models.

Progress

Progress is local-only — sign in to sync across devices.
Spotted a bug or have feedback on this page?Report an Issue

Comments 0

🔔 Reply notifications (sign in)
Sign inPlease sign in to comment.

No comments yet — be the first.