Error: 400 Bad Request: template error: template: :1: unclosed action means the
TEMPLATE in your Modelfile has a {{ without its closing
}}. ollama create parses the template before it saves the model,
refuses it, and names the line. Close the action, or remove the call it does not know,
and run the same command again.
The same parse step printed function "upper" not defined for a function the
template language does not have. Both are fixed in the Modelfile; nothing on the server
needs to change.
The error
FROM qwen2.5:0.5b
TEMPLATE """{{ .Prompt """
PS> ollama create fix-tpl -f Modelfile.tpl
gathering model components
Error: 400 Bad Request: template error: template: :1: unclosed action
FROM qwen2.5:0.5b
TEMPLATE """{{ upper .Prompt }}"""
PS> ollama create fix-fn -f Modelfile.fn
gathering model components
Error: 400 Bad Request: template error: template: :1: function "upper" not defined
Why it happens
The 400 Bad Request at the start shows the refusal came back from the server
during ollama create, after gathering model components, and the
command ended with exit code 1. In the first file the closing }} is missing,
so the template ends while an action is still open. In the second, upper is a
function name the template language does not define; the message quotes the name.
The number after template: is the line inside the template text: with the
same mistakes on later lines of the template, the messages read
template: :3: unclosed action and template: :2: function "upper" not
defined.
A misspelled field is a different story. A Modelfile with
{{ .Promptt }} was accepted: ollama create printed
success, and both ollama run and a request to
/api/generate answered without an error. So the create step catches broken
syntax and unknown functions, but a typo in a field name gets through; check the field
names against the base model's template by eye.
The fix
FROM qwen2.5:0.5b
TEMPLATE """{{ .Prompt }}"""
PS> ollama create fix-tplok -f Modelfile.tplok
success
PS> ollama run fix-tplok "Say hi in three words"
Hello, how are you?
Every {{ needs a matching }} on the same action. For a long
template, start from the base model's own one (ollama show <model> --template
prints it) and change one part at a time, running ollama create after each
change, so an error belongs to the edit you just made.
How it was reproduced
On Windows 11 in Windows PowerShell 5.1 with Ollama 0.40.16, against a separate test
server on 127.0.0.1:15190 with its own models folder and qwen2.5:0.5b as the
base model. Three two-line Modelfiles were created: one with an unclosed action, one
calling upper, and one with the misspelled field .Promptt. The
first two failed at create; the third was created and answered a prompt. A Modelfile with
{{ .Prompt }} was then created and run.
Frequently asked
- What does 'template: :1: unclosed action' mean in Ollama?
- The TEMPLATE text in the Modelfile opens an action with two braces and never closes it. The number is the line inside the template. Add the closing braces and run ollama create again.
- Why does ollama create say function not defined?
- The template calls a function name that the template language does not define, such as upper. ollama create refuses it with a 400 Bad Request. Remove the call or use a function that exists.
- Does Ollama check field names in a Modelfile TEMPLATE?
- Not in the test with Ollama 0.40.16. A template using .Promptt instead of .Prompt was created and answered requests without any error, so a misspelled field has to be found by reading the template.
More decoded errors in the Fixes category. When
ollama create rejects the instruction name itself, see
Ollama
Modelfile: 'command must be one of'.