Good question — I’ve worked through this with Ollama + n8n quite a bit. Here’s what actually works reliably in 2026:
**1. Use Ollama’s native `format: json` parameter (most reliable layer)**
When you configure the Ollama Chat Model node, add `format: json` in the additional fields / model parameters. This forces Ollama to constrain its token sampling to valid JSON tokens only — it’s not prompt engineering, it’s enforced at the model level. Models like `llama3`, `mistral`, and `qwen2.5` support this well. This alone eliminates ~80% of malformed output.
**2. Pair it with n8n’s Structured Output Parser**
Stack Ollama format:json + the Structured Output Parser (with a JSON Schema). The schema tells the model what fields you expect; the parser validates and extracts them. If you’re doing routing, your schema might be as simple as `{ “route”: { “type”: “string”, “enum”: [“billing”, “support”, “sales”] } }`.
**3. Code node as your safety net**
After the parser, add a Code node that does:
```js
const out = $input.first().json;
if (!out.route || ![‘billing’,‘support’,‘sales’].includes(out.route)) {
throw new Error('Invalid route: ’ + JSON.stringify(out));
}
return [{ json: out }];
```
This gates downstream nodes so you never silently route wrong.
**4. For SQL intent specifically**
Don’t ask the model to output SQL. Ask it to output structured intent: `{ “table”: “orders”, “filters”: [{“field”: “status”, “op”: “eq”, “value”: “pending”}], “limit”: 10 }`. Then your Code node maps that to a parameterized query. Much safer and more reliable than free-form SQL.
**5. One-shot repair loop if needed**
For complex schemas where the model still occasionally fails, wire an IF node checking for parse errors → re-send to Ollama with the error message appended to the prompt. One retry catches most stragglers. If it fails twice, route to a fallback/human review rather than guessing.
The combination of format:json + schema + Code validation is what I use in production-style workflows. Works without any paid nodes.