# Structured Outputs

> Utilize structured output in GenAIScript to generate JSON data with schema validation for precise and reliable data structuring.

GenAIScript supports the generation of structured outputs with automatic data repairs. It can leverage built-in schema validation from LLM providers or executes it own validation as needed.

[Play](https://youtube.com/watch?v=U6mWnZOCalo)

The structured output are configured through two flags: `responseType`, which controls the data format, and `responseSchema` which controls the data structure.

## Response Type

[Section titled “Response Type”](#response-type)

The response type is controlled by the `responseType` optional argument and has the following options:

* `json`: tell the LLM to produce valid JSON output.
* `yaml`: tell the LLM to produce valid YAML output.
* `json_object`: use built-in OpenAI JSON output
* `json_schema`: use built-in OpenAI JSON output with JSON schema validation

Note that `text` and `markdown` are also supported to configure the LLM output.

### `json`

[Section titled “json”](#json)

In this mode, GenAIScript prompts the LLM to produce valid JSON output. It also validate the output and attempt to repair it if it is not valid. This mode is implemented by GenAIScript and does not rely on LLM providers support.

```js
script({
    responseType: "json",
})
```

The schema validation is applied if the `responseSchema` is provided.

### `yaml`

[Section titled “yaml”](#yaml)

In this mode, GenAIScript prompts the LLM to produce valid JSON output. It also validate the output and attempt to repair it if it is not valid. This mode is implemented by GenAIScript and does not rely on LLM providers support.

```js
script({
    responseType: "yaml",
})
```

The schema validation is applied if the `responseSchema` is provided.

### `json_object`

[Section titled “json\_object”](#json_object)

In this mode, GenAIScript prompts the LLM to produce valid JSON output. It also validate the output and attempt to repair it if it is not valid. This mode relies on built-in support from LLMs, like OpenAI.

```js
script({
    responseType: "json_object",
})
```

### `json_schema`

[Section titled “json\_schema”](#json_schema)

Structured output is a feature that allows you to generate structured data in data format like with a [JSON schema](/genaiscript/reference/scripts/schemas). This is more strict than `json_object`.

To enable this mode, set `responseType` to `json_schema` and provide a `responseSchema` object.

```js
script({
    responseType: "json_schema",
    responseSchema: {
        type: "object",
        properties: {
            name: { type: "string" },
            age: { type: "number" },
        },
        required: ["name", "age"],
    },
})
```

Note that there are [several restrictions](https://platform.openai.com/docs/guides/structured-outputs/how-to-use) on the schema features supported by this mode.

* `additionalProperties: true` is not supported.
* all optional fields (e.g. not in `required`) will be returned and might be `null`

## Response Schema

[Section titled “Response Schema”](#response-schema)

You can specify a [schema](/genaiscript/reference/scripts/schemas) through `responseSchema` which will automatically turn on the structured output mode. The output will be validated against the schema, and GenAIScript will attempt to repair the output if it is not valid. The script will fail if the output does not match the schema.

```js
script({
    responseType: "json",
    responseSchema: {
        type: "object",
        properties: {
            name: { type: "string" },
            age: { type: "number" },
        },
        required: ["name", "age"],
    },
})
```

### Inlined schemas

[Section titled “Inlined schemas”](#inlined-schemas)

Note that this section applies to the entire output of a chat. You can also use [inlined schemas](/genaiscript/reference/scripts/schemas) and use a mixed markdown/data that GenAIScript will parse.

### Choices

[Section titled “Choices”](#choices)

If you are looking to build a LLM-as-a-Judge and only looking for outputs in a set of words, you can also consider using [choices](/genaiscript/reference/scripts/choices) to increase the probability of the model generating the specified words.

## `cast`

[Section titled “cast”](#cast)

The [cast](/genaiscript/reference/runtime/cast) function is a [GenAIScript runtime helper](/genaiscript/reference/runtime) to convert unstructured text/images into structured data.

```js
import { cast } from "@genaiscript/runtime"


const { data } = await cast((_) => _.defImages(images), {
    type: "object",
    properties: {
        keywords: {
            type: "array",
            items: {
                type: "string",
                description: "Keywords describing the objects on the image",
            },
        },
    },
    required: ["keywords"],
})
```