{"error":"invalid format: \"\\\"xml\\\"\"; expected \"json\" or a valid JSON Schema object"}
— the format field of an Ollama request takes only two things: the string
"json" or a JSON Schema object, as the message says. The other values
tested, "xml" and true, were refused. For structured output, send a schema object.
The fix is to put a real schema in format, for example
{"type":"object","properties":{...},"required":[...]}. A schema with a typo in
it fails differently, with a 400 and JSON schema conversion failed, and the
message names the bad keyword.
The error
PS> curl.exe -s -i http://127.0.0.1:15190/api/generate -d '@fmt-xml.json' (fmt-xml.json: {"model":"qwen2.5:0.5b","prompt":"Give the name and age of a fictional patient.","stream":false,"format":"xml"})
HTTP/1.1 500 Internal Server Error
{"error":"invalid format: \"\\\"xml\\\"\"; expected \"json\" or a valid JSON Schema object"}
The runner printed the status line and the last line of each reply; the other headers are
left out. /api/chat answered the same body shape with the same 500 and
message, and "format":true gave
invalid format: \"true\". A schema object with a wrong type name:
PS> curl.exe -s -i http://127.0.0.1:15190/api/generate -d '@fmt-badschema.json' (fmt-badschema.json: {"model":"qwen2.5:0.5b","prompt":"Give the name and age of a fictional patient.","stream":false,"format":{"type":"objekt","properties":{"name":{"type":"string"}}}})
HTTP/1.1 400 Bad Request
{"error":"Field 'json_schema': \"json_schema\": JSON schema conversion failed:\nJSON schema error at #: unrecognized type objekt"}
Why it happens
format is not a list of output formats. The message itself names the two
accepted forms: "json", which asks for any valid JSON, or a JSON Schema
object, which asks for JSON of that exact shape. The other values tested,
"xml" and true, were refused. Two
details from the test are worth knowing: the invalid value came back as HTTP 500, not 400,
so code that treats 5xx as "server down, retry" will retry a request that can never work;
and the doubled escaping in \"\\\"xml\\\"\" is your value
"xml", quotes included, escaped inside the error string.
A schema object with an unknown type was refused with a 400 instead of a 500,
and the message gave the path # (the root of the schema) and the word it did
not recognise.
The fix
PS> curl.exe -s -i http://127.0.0.1:15190/api/generate -d '@fmt-schema.json' (fmt-schema.json: {"model":"qwen2.5:0.5b","prompt":"Give the name and age of a fictional patient.","stream":false,"options":{"temperature":0},"format":{"type":"object","properties":{"name":{"type":"string"},"age":{"type":"integer"}},"required":["name","age"]}})
HTTP/1.1 200 OK
...
PS> (curl.exe -s http://127.0.0.1:15190/api/generate -d '@fmt-schema.json' | ConvertFrom-Json).response
{"name":"李华","age":28}
With a valid schema the reply's response is a JSON string of that shape, ready
to deserialize. The schema fixes the shape, not the content: asked for a fictional patient,
the small model chose a Chinese name, so say what you want in the prompt as well. The other accepted value, "format":"json", asks
only for valid JSON of any shape; a schema is the better choice when your code expects
particular fields.
How it was reproduced
Ollama 0.40.14 on Windows 11, against a separate test server started with
ollama serve on 127.0.0.1:15190 with its own models folder and
qwen2.5:0.5b. Five bodies (the file names are the test's own) were written to
files by a PowerShell script and sent with curl.exe -i:
"xml" and true to /api/generate,
"xml" to /api/chat, a schema with "objekt", and a
valid schema with temperature 0.
Frequently asked
- What values does the Ollama format field accept?
- The string "json" or a JSON Schema object, as the error message says. The other values tested, "xml" and true, were refused with HTTP 500 and 'invalid format' in the test.
- How do I get structured output from Ollama?
- Put a JSON Schema object in format, for example type object with properties and required fields. The response field then holds a JSON string of that shape, which you can deserialize.
- Why does Ollama say JSON schema conversion failed?
- The format object is not a valid schema. In the test a misspelled type, objekt, gave HTTP 400 with 'unrecognized type objekt' and the path of the bad part, # for the root.
More decoded errors in the Fixes category. JSON mode from curl and .NET is part of the Ollama API post.