> ## Documentation Index
> Fetch the complete documentation index at: https://docs.runanywhere.ai/llms.txt
> Use this file to discover all available pages before exploring further.

# Structured Output

> Get JSON back instead of prose

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.

```dart theme={null}
final result = await RunAnywhere.llm.generateStructured(
  'Extract the name and age: Dana is 34.',
  schemaJson,
  mode: StructuredOutputMode.validationOnly,
  options: LlmOptions(temperature: 0.1),
);
```

## Enforcement modes

| Mode             | Behaviour                                            |
| ---------------- | ---------------------------------------------------- |
| `validationOnly` | Generate freely, then validate. The default.         |
| `repair`         | Validate, then retry once with a repair instruction. |
| `constrained`    | **Throws `featureNotAvailable` today.**              |

<Warning>
  Constrained mode **throws** on every SDK. It needs engine-level constrained decoding, which is not
  wired in yet. Use validation-only or repair.
</Warning>

Validation-only is the default and 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 and a
failure means showing an error.

## The schema

```dart theme={null}
// Flutter takes the schema as a String, not an object
const schemaJson = '{"type":"object","properties":{"name":{"type":"string"}}}';
```

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 rather than
one large object.

Naming fields the way a person would helps: `totalPrice` gets better results than `tp`.

## Reading the result

```dart theme={null}
// result carries the parsed value and the raw text
```

## When to reach for this

| Task                           | Approach                                |
| ------------------------------ | --------------------------------------- |
| Extracting fields from text    | Structured output                       |
| Classifying into fixed labels  | Structured output, or a one-word prompt |
| Anything you will `JSON.parse` | Structured output                       |
| Prose for a human to read      | Plain generation                        |

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.
