- Reduced Variability: Ensures consistent outputs by adhering to predefined fields.
- Type Safety: Enforces correct data types, preventing mismatches.
- Easier Parsing & Integration: Enables direct use in applications without extra processing.
Tutorial: Structured Outputs using Cerebras Inference
In this tutorial, we’ll explore how to use structured outputs with the Cerebras Cloud SDK. We’ll build a simple application that generates movie recommendations and uses structured outputs to ensure the response is in a consistent JSON format.1
Initial Setup
First, ensure that you have completed steps 1 and 2 of our Quickstart Guide to set up your API key and install the Cerebras Cloud SDK.Then, initialize the Cerebras client and import the necessary modules we will use in this tutorial.
2
Defining the Schema
To ensure structured responses from the model, we’ll use a JSON schema to define our output structure. Start by defining your schema, which specifies the fields, their types, and which ones are required. For our example, we’ll define a schema for a movie recommendation that includes the title, director, and year:
3
Using Structured Outputs
Next, use the schema in your API call by setting the Sample output:Now you have a structured JSON response from the model, which can be used in your application.
response_format parameter to include both the type and your schema. Setting strict to true will enforce the schema. Setting strict to false will allow the model to return additional fields that are not specified in the schema, similar to JSON mode.Schema References and Definitions
You can use$ref with $defs to define reusable schema components within your JSON schema. This is useful for avoiding repetition and creating more maintainable schemas.
Advanced Schema Features
Your schema can include various JSON Schema features:- Fundamental Data Types: String, Number, Boolean, Integer, Object, Array, Enum, null.
- Union Types: Use
anyOfto allow the model to return one of multiple possible types (max of 5). Note: The shorthand"type": [ … ]is not supported. See schema limitations for an example. - Nested structures: Define complex objects with nested properties, with support for up to 5 layers of nesting. You can also use definitions to reference reusable schema components.
- Required fields: Specify which fields must be present.
- Additional properties: Control whether extra fields are allowed. Note: the only accepted value is
false. For everyrequiredarray you define in your schema, you must setadditionalPropertiestofalse. - Enums (value constraints): Use the
enumkeyword to whitelist the exact literals a field may take. Seeratingin the example below.
Working with Pydantic and Zod
Besides defining a JSON schema manually, you can use Pydantic (Python) or Zod (JavaScript) to create your schema and convert it to JSON. Pydantic’smodel_json_schema and Zod’s zodToJsonSchema methods generate the JSON schema, which can then be used in the API call, as demonstrated in the workflow above.
JSON Mode
In addition to structured outputs, you can also use JSON mode to generate JSON responses from the model. This approach tells the model to return data in JSON format but doesn’t enforce a specific structure. The model decides what fields to include based on the context of your prompt.We recommend using structured outputs (with strict set to true) whenever possible, as it provides more predictable and reliable results.
response_format parameter to json_object:
Structured Outputs vs JSON Mode
The table below summarizes the key differences between Structured Outputs and JSON Mode:| Feature | Structured Output | JSON Mode |
|---|---|---|
| Outputs valid JSON | Yes | Yes |
| Adheres to schema | Yes (enforced) | No (flexible) |
| Enabling | response_format: { type: "json_schema", json_schema: {"strict": true, "schema": ...} } | response_format: { type: "json_object" } |
Variations from OpenAI’s Structured Output Capabilities
While our structured output capabilities closely match OpenAI’s implementation, there are a few key differences to note.These limitations only apply if
strict is set to true in the JSON schema.Schema Limitations
Schema Limitations
- Recursive JSON schemas are not currently supported.
- Maximum schema length is limited to 5,000 characters.
- No error is thrown when
additionalPropertiesis not explicitly set to false. We quietly handle this case. - Union shorthand
"type": ["string", "null"]is not supported. UseanyOfinstead, and remember that all properties must be listed in therequiredarray. See the example below.
Property Handling
Property Handling
- Every property defined in
propertiesmust also appear in therequiredarray. If a property may benull, keep it inrequiredand add a union withanyOf. - Omitted optional properties are not treated as errors.
Array Validation
Array Validation
items: trueis not supported for JSON schema array types.items: falseis supported when used withprefixItemsfor tuple-like arrays with validation rules.
Schema Structure
Schema Structure
$anchorkeyword is not supported - use relative paths within definitions/references instead.- Use
$defsinstead ofdefinitionsfor reusable schema components. - Additional informational fields meant as guidelines (not used in validation) are not supported.
Supported Reference Patterns
Supported Reference Patterns
For security reasons, external schema references are not supported.
- Internal definitions are supported:
"$ref": "#/$defs/cast_member" - Other reference access patterns are not recommended, and will be deprecated in future releases. See Schema References and Definitions for more info.

