Tips > AI & LLM Integration

Use Structured Output (JSON Mode) for Parseable Responses

LLMs return free-form text by default, which is fragile to parse with regex or string splitting.

TipIntermediate2 min read

LLMs return free-form text by default, which is fragile to parse with regex or string splitting. Both OpenAI and Anthropic support JSON mode (or structured outputs) that constrains the model to return valid JSON matching a schema you define. Always include the expected schema directly in your prompt -- the model needs to see the field names and types to comply.

Real-world example: You receive customer support emails and need the LLM to extract the ticket category, urgency level, and a one-line summary as structured data for routing.

OpenAI Chat Model node configuration:

Setting Value
Model gpt-4o-mini
Response Format JSON Object

System prompt:

You are a support ticket classifier. Analyze the customer message and return
a JSON object with exactly these fields:

{
  "category": "billing" | "technical" | "account" | "general",
  "urgency": "low" | "medium" | "high" | "critical",
  "summary": "One sentence summary of the issue",
  "requires_human": true | false
}

Return ONLY the JSON object. No additional text.
```text
User message expression:

```text
{{ $json.email_body }}
```text
The model returns:

```json
{
  "category": "billing",
  "urgency": "high",
  "summary": "Customer was double-charged for their March subscription renewal.",
  "requires_human": true
}
```text
Follow with an Edit Fields node to extract each field, or pass `$json.message.content` directly into a downstream IF node:

```text
{{ JSON.parse($json.message.content).urgency === "critical" }}
```text
> **Warning: Always Validate**
>
> Even in JSON mode, models can occasionally return malformed output. Wrap your `JSON.parse()` in a try-catch inside a Code node for production workflows, and route parse failures to an error handler.

Structured output eliminates brittle text parsing and makes LLM responses directly consumable by the rest of your workflow.

**Related:** [Use Manual Trigger During Development Instead of Webhook or Schedule](../api-cost-optimization/01-use-manual-trigger-during-development-instead-of-webhook-or-schedule.md) | [Flatten Deeply Nested API Responses](../code-node-mastery/01-flatten-deeply-nested-api-responses.md)

Want this running in your stack?

I build production n8n and Cloudflare automation for teams — the same engineering behind HarperFlow. Fixed-price, escrow-protected, US-based.