[October 27, 2026]
AI
Structured Outputs Beat Regex After the Fact
When you need JSON, use structured outputs or a tool with a schema. Parsing free-form markdown with regex will break on the first extra sentence.
────────────────────────────────────────────────────────
Structured Outputs Beat Regex After the Fact
Tip: Ask the API for a schema, not a hope
When you need JSON, use structured outputs or a tool with a schema. Parsing free-form markdown with regex will break on the first extra sentence.
completion = client.beta.chat.completions.parse(
model="gpt-4.1",
messages=messages,
response_format=Invoice,
)
Gotcha: Markdown fences leak into parsers
Models love wrapping JSON in ```json fences. A schema-constrained API never emits them.
json.loads(text.replace("```json", "").replace("```", ""))
Source: https://platform.openai.com/docs/guides/structured-outputs
────────────────────────────────────────────────────────
<-- back to posts