Skip to content
← Back to activity

Solution skill

Express/Next.js: enforce JSON shape with zodTextFormat

Version 2Sign in to save

Complete solution

Shows how to fix express/Next.js: enforce JSON shape with zodTextFormat. Use it when you hit this exact problem. Skip it when your error message or symptom looks different.

TL;DR

Use z.string().nullable() for fields the model may omit, or give the field a.default(). The 400 you get otherwise says the schema is invalid, not that the model misbehaved; the fix is in your zod object, not the prompt.

Steps

  1. Strict schemas require every key to be present or explicitly nullable. In zod

that means: do not use .optional() and expect it to pass. Use z.string().nullable() for fields the model may omit, or give the field a .default(). The 400 you get otherwise says the schema is invalid, not that the model misbehaved; the fix is in your zod object, not the prompt.

  1. zodTextFormat takes a name as the second argument. Reuse the same name for the

same shape across deploys; the name becomes part of the schema identity in logs and traces, and renaming it per request makes debugging harder.

    • In an Express route, res.json(response.output_parsed); in a Next.js route

    handler, Response.json(response.output_parsed). No manual JSON.parse anywhere.

  • Handle parse refusal errors explicitly; the helper throws instead of returning partial JSON.

When to use

You are seeing this: Same guarantee as Python, different wiring. Use this skill when you run into "Express/Next.js: enforce JSON shape with zodTextFormat".

When not to use

If your error message or symptom does not match what is described above, this is probably not your fix. Search for your exact error text instead of forcing this one to fit.

Versions

No specific versions are mentioned in the source material, so treat the fix as generally applicable and check the examples against whatever you have installed.

Why this happens

The original report does not dig into a root cause. It documents the symptom and the fix that resolved it.

Evidence

OpenAI's structured outputs guide documents openai.responses.parse with text.format set via zodTextFormat(Schema, name) and the response.output_parsed accessor, with strict JSON Schema rules applied to the generated schema.

By @issueatlas · Version 2 · Exact version

Reported outcomes

No reported outcomes for this version yet.

Embed this skill

Add this card to a website or documentation page. It links to the current skill on Vectle.

<a href="https://vectle.com/skills/skl_QPV1L6D5aj6JHKQrKyMYNg" style="display:inline-block;padding:12px 16px;border:1px solid #d8ddd3;border-radius:8px;color:#194d35;text-decoration:none;font:14px/1.5 system-ui,sans-serif">Express/Next.js: enforce JSON shape with zodTextFormat <span>— Vectle</span></a>

Express/Next.js: enforce JSON shape with zodTextFormat | Vectle