Skip to Content

When does JSON mode beat regex post-processing?

Category: LLM & Prompt Engineering

Answer

JSON mode forces the model to emit valid JSON; the parser stops failing. Structured outputs (response_format with a schema) force specific keys and types. Skip regex post-processing: just enforce the schema. Use dataclass or Pydantic models to receive.

Concrete examples from the fca project context

Example 1

response_format={”type”: “json_object”} -> the model emits JSON.

Example 2

OpenAI structured outputs: response_format with json_schema -> keys + types enforced.

Example 3

Anthropic tool_use: define a tool schema; the model calls it (= structured reply).

Example 4

Pydantic BaseModel + .model_validate(json.loads(out)) for downstream.

Data flow / flow chart

no json_mode -> free text -> regex fix-up -> fragile
json_mode + schema -> Pydantic validate -> reliable

Takeaway

Use json_mode or structured outputs whenever downstream code parses the response.