Skip to content

JSON Mode

Some models support forcing the output format to a JSON object, like the JSON Object mode of OpenAI.

The generated file name will be [spec].[template].json.

script({
responseType: "json_object",
})

Schema validation

You can specify a schema through responseSchema which will automatically turn on the JSON mode. The output will be validated against the schema, and GenAIScript will attempt to repair the output is not valid. The script will fail if the output does not match the schema.

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

You can also provide an example of object and GenAIScript will generate the schema for you.

script({
responseType: "json_object",
responseSchema: { characters: [{ name: "neo", age: 30 }] },
})

Inline schemas

You can also specify the schema inline in the script and use a mixed markdown/data that GenAIScript will parse.

Structured Output

Recent models have added a structured output mode that is more strict than JSON mode. This mode is enabled by setting responseType to json_schema.

script({
responseType: "json_schema",
responseSchema: {...},
})