Skip to main content
Asking a model for JSON in a system prompt works most of the time, which is the problem. generateStructured validates the result against a schema and can repair it.
Electron’s generateStructured is generic over the parsed type, so the result is typed without a cast.

Enforcement modes

Constrained mode throws on every SDK. It needs engine-level constrained decoding, which is not wired in yet. Use validation-only or repair.
Validation-only is right when a retry is cheap. Repair costs a second generation when the first is invalid, which is worth it when the call is user-facing.

The schema

Keep schemas small. A small model handles four flat fields reliably and a deeply nested structure badly. When you need something complex, ask for the pieces separately. Naming fields the way a person would helps: totalPrice gets better results than tp.

When to reach for this

If you are about to write a regex over a model’s reply, you want this instead.

Temperature

Lower it. Structured extraction is not a creative task, and 0.1 to 0.3 produces far more consistent field values than the default.