Sub topic 15
Importing your own GGUF weights
Ollama's library covers popular models, but many others exist only as files on Hugging Face or on your disk. This chapter shows how to bring a GGUF file into Ollama, supply the prompt template it needs, and check that the result behaves correctly.
This chapter assumes you can already run ollama create with a Modelfile, know what quantization levels such as Q4_K_M mean, and are comfortable with a terminal and basic Python.
What GGUF is and why Ollama uses it
GGUF is a single-file model format from the llama.cpp project. One file holds the tensors (the weights), the tokenizer, and metadata such as the architecture name and the context length. The weights are usually already quantized, so the file name often ends in something like Q4_K_M.gguf.
Ollama stores every model it runs as GGUF blobs. Importing a GGUF file therefore means registering a file you already have under a model name, not converting it.
The import workflow
The import has four steps: obtain the file, write a Modelfile that points at it, run ollama create, and test the result.
Step 1: Get the file
Download a GGUF file from a model repository. With the Hugging Face command-line tool, this looks like:
pip install -U "huggingface_hub[cli]"
huggingface-cli download Qwen/Qwen2.5-0.5B-Instruct-GGUF qwen2.5-0.5b-instruct-q4_k_m.gguf --local-dir .Pick the quantization the same way you would for a library model. Check that the file fits in your RAM or VRAM with room left for the context window.
Some large repositories split a model into parts named like model-00001-of-00003.gguf. Ollama needs one file, so merge the parts first with llama.cpp's llama-gguf-split --merge.
Step 2: Write the Modelfile
The minimal Modelfile has one line:
FROM ./qwen2.5-0.5b-instruct-q4_k_m.ggufA relative path is resolved against the Modelfile's location, so keep the two together or use an absolute path. This minimal form often runs but behaves badly, for the reason in the next section.
Step 3: Create and run
ollama create qwen-local -f Modelfile
ollama run qwen-localDuring creation Ollama hashes the file and copies it into its model store as a blob. This temporarily doubles your disk use. Once ollama list shows the new model, you can delete the original GGUF file if you do not need it elsewhere.
The template problem
Chat models are trained on a specific text layout. That layout wraps each turn in special tokens that mark who is speaking. If the prompt does not follow the layout, the model may ramble, repeat itself, or never stop.
Ollama builds the final prompt from the TEMPLATE in the Modelfile. When you import a GGUF file without one, the model gets a bare template that passes your prompt through with no role markers. A base model works fine with that. An instruct model usually does not.
You have two ways to find the right template:
- Read the model card for the format name, such as ChatML, Llama 3, or Gemma.
- Copy it from a library model of the same family:
ollama show llama3.2 --modelfileprints theTEMPLATEandPARAMETER stoplines that model uses.
The next Modelfile imports a Qwen model, which uses the ChatML format:
FROM ./qwen2.5-0.5b-instruct-q4_k_m.gguf
TEMPLATE """{{ if .System }}<|im_start|>system
{{ .System }}<|im_end|>
{{ end }}{{ if .Prompt }}<|im_start|>user
{{ .Prompt }}<|im_end|>
{{ end }}<|im_start|>assistant
{{ .Response }}<|im_end|>
"""
PARAMETER stop "<|im_start|>"
PARAMETER stop "<|im_end|>"
PARAMETER temperature 0.4
PARAMETER num_ctx 4096
SYSTEM You are a concise assistant.The template uses Go template syntax. .System, .Prompt, and .Response are filled in by Ollama for each turn. The stop parameters tell the runner to halt when the model emits a turn marker, which prevents the model from writing the next user turn itself.
Importing straight from Hugging Face
For repositories that publish GGUF files, Ollama can pull directly without a manual download:
ollama run hf.co/Qwen/Qwen2.5-0.5B-Instruct-GGUF:Q4_K_MThe part after the colon selects the quantization. If you leave it out, Ollama picks a default, preferring Q4_K_M when the repository has it. Ollama uses the chat template stored in the GGUF metadata when one is present, so this route is often easier. You can still build a customized model on top of it by using that hf.co/... name in a FROM line.
Other sources: safetensors
Many repositories ship safetensors weights (the Hugging Face native format) instead of GGUF. For supported architectures, FROM can point at the directory that contains the weights, and Ollama converts them during create. If the weights are full precision, ollama create --quantize q4_K_M can quantize them on the way in. For unsupported architectures, convert with llama.cpp's convert_hf_to_gguf.py and import the result as GGUF.
Verifying the import
Run these checks after creating the model:
ollama show qwen-local
ollama show qwen-local --modelfile
ollama run qwen-local "Reply with the single word: ready"ollama show reports the architecture, parameter count, context length, and quantization. This should match what the model card says. The --modelfile form prints the template and parameters as Ollama stored them. The test prompt should return a short answer that stops by itself.
You can also test through the API, which is the path your scripts will take:
import requests
resp = requests.post(
"http://localhost:11434/api/chat",
json={
"model": "qwen-local",
"messages": [{"role": "user", "content": "Name two prime numbers."}],
"stream": False,
},
timeout=120,
)
resp.raise_for_status()
print(resp.json()["message"]["content"])Common errors
| Symptom | Likely cause | Fix |
|---|---|---|
| `ollama create` fails with a file-not-found error | `FROM` path is wrong relative to the Modelfile | Use an absolute path or move the file |
| Model fails to load with an unsupported architecture message | The GGUF uses an architecture your Ollama version does not know | Update Ollama, or choose a different model |
| Output never stops or includes special tokens | Missing template or stop parameters | Add `TEMPLATE` and `PARAMETER stop` lines |
| Out-of-memory on load | The quantization is too large for your hardware | Download a smaller quantization or lower `num_ctx` |
| Repetitive, low-quality replies | Wrong template, or temperature far from the model card's recommendation | Match the model card's settings |
Licensing and trust
A GGUF file is a third-party artifact. Check the license on the model card before you use it in a product. Prefer repositories from the original publisher or well-known quantizers. Quantized files are derived by someone else, so a different quantizer may produce slightly different quality at the same nominal level.
Exercise
You import an instruct-tuned GGUF file with a one-line Modelfile (FROM ./model.gguf). The model answers questions, but then keeps generating fake "User:" turns and never stops. What two things should you add to the Modelfile, and how can you find the correct values?
Show answer
Add a TEMPLATE that matches the model's chat format, and PARAMETER stop lines for its end-of-turn tokens. Find the values on the model card, or copy them from a library model of the same family with ollama show <model> --modelfile. Then run ollama create again with the same name and test.
Exercise
After ollama create succeeds, you still have the original 4 GB GGUF file in your downloads folder. Why does the model store use extra disk space, and can you remove the original?
Show answer
ollama create copies the weights into Ollama's model store as a blob, so the data exists twice. Once ollama list shows the model and it runs correctly, you can delete the original file unless you want it for other tools.