Skip to main content
Structured Outputs constrains model responses to a JSON schema so applications can process generated data reliably. Key benefits include:
  • Consistent fields: Responses follow the fields defined in your schema.
  • Type safety: Values use the data types defined in your schema.
  • Simpler parsing and integration: Applications can consume responses without additional format conversion.
The strict-mode rules below apply to response_format.json_schema.strict: true and tools[].function.strict: true. They do not apply to strict: false or legacy json_object mode.
When using structured outputs with kimi-k2.7-code, use parsed reasoning. reasoning_format: "raw" is incompatible with json_object and json_schema response formats.

Tutorial: Structured Outputs using Cerebras Inference

The following steps define a movie-recommendation schema and use the Cerebras Cloud SDK to return a response that conforms to it.
1

Initial setup

Complete steps 1 and 2 of the Quickstart to configure your API key and install the Cerebras Cloud SDK.Import the required modules and initialize the client.
2

Define the schema

Define a JSON schema that specifies the response fields, their types, and which fields are required. This example defines a movie recommendation with a title, director, and year.
For every object in your schema (the root and any nested objects), you must set additionalProperties to false when using strict mode. See Required Schema Structure for details.
3

Use Structured Outputs

Add the schema to response_format in the API request.
  • strict: true uses constrained decoding to guarantee that output conforms to schemas in the supported JSON Schema subset. Recommended for production use.
  • strict: false (or omitted) treats the schema as a hint. The model may return additional fields, miss required fields, or use incorrect types, similar to JSON mode.
Sample output:
The returned JSON conforms to the schema and can be used directly by the application.

Understanding strict mode

For schemas that use the supported JSON Schema subset, strict mode guarantees that generated output conforms to the schema. Set strict to true to enable constrained decoding at the token level.

Why use strict mode

Without strict mode, a response can contain:
  • Malformed JSON that fails to parse
  • Missing required fields
  • Incorrect data types, such as "16" instead of 16
  • Fields that are not defined in the schema
Strict mode provides:
  • Valid JSON
  • Fields that conform to the schema
  • Correct data types for properties
  • Fewer retries caused by schema violations

Enable strict mode

Set strict to true in your response_format configuration:

Schema requirements for strict mode

When using strict mode, you must set additionalProperties: false. This is required for every object in your schema.

Limitations in strict mode

When strict mode is enabled, your schema must conform to specific requirements. See the Supported Schemas section for detailed information on constraints, limits, and unsupported features.

Schema references and definitions

Use $ref with $defs to define reusable components within a JSON schema. Reusable definitions reduce repetition and make schemas easier to maintain.

Supported schemas

Structured Outputs supports a subset of JSON Schema. The following sections describe its supported types, properties, and constraints.

Supported types

The following types are supported for Structured Outputs:

Schema constraints

When using strict mode, the following constraints apply:

Required schema structure

All schemas must follow these rules:
  • Root must be an object: The top-level schema must have "type": "object".
  • Root must define properties: Include a properties object at the root.
  • Root unions are not supported: The root cannot be an array, scalar, or anyOf union.
  • No additional properties: You must set "additionalProperties": false for every object in your schema.
  • Arrays need an item schema: Every array must define items or use prefixItems with items: false.
These requirements are enforced by API version 2. Non-conforming strict schemas return a validation error. See API Versions.

Supported features

Your schema can include the following JSON Schema features:
  • Nested structures: Define complex objects with nested properties.
  • Required fields: Specify which fields must be present.
  • Optional fields: Properties may be omitted from required.
  • Enums (value constraints): Use the enum keyword to whitelist the exact literals a field may take. See rating in the example below.
  • Constants: Use const with primitive string, number, integer, boolean, or null values.
  • Non-root unions: Use anyOf below the root object.
  • Schema references: Use local, nonrecursive $ref values with $defs to define reusable schema components within your schema.
  • Tuple validation: items: false is supported when used with prefixItems for tuple-like arrays.
  • Number constraints: Use minimum, maximum, exclusiveMinimum, exclusiveMaximum, and multipleOf to constrain number and integer values.
  • Annotations: description, title, $schema, and default are accepted but are not enforced by constrained decoding.

Unsupported features

The following JSON Schema features are not supported in strict mode: Unlisted JSON Schema keywords should be treated as unsupported unless the selected model’s documentation states otherwise. String-length support is model-dependent. In particular, strict tool schemas for qwen-3.8-27b do not support minLength or maxLength.
Do not combine tools and response_format unless the selected model’s contract explicitly documents and validates the combination. For portable workflows, call tools first and format the result in a separate structured-output request.

Example: Complex schema

The following example defines a schema with nested objects and arrays:
The API can return a response such as:

Key ordering

The keys in the generated JSON output will appear in the same order as they are defined in your schema.

Working with Pydantic and Zod

You can define a schema with Pydantic for Python or Zod for JavaScript instead of writing JSON Schema manually. Pydantic’s model_json_schema and Zod’s zodToJsonSchema methods generate JSON Schema for use in an API request.

JSON mode

JSON mode generates valid JSON without enforcing a specific schema. The model chooses which fields to include based on the prompt.
We recommend using structured outputs with strict set to true instead of JSON mode whenever possible. Structured outputs guarantee schema adherence, while JSON mode only ensures valid JSON without enforcing a specific structure.
To use JSON mode, set the response_format parameter to json_object and include instructions in your message asking the model to respond in JSON format:

Structured Outputs vs. JSON mode

The following table compares Structured Outputs and JSON mode:
Do not combine tools and response_format unless the selected model’s documentation explicitly supports and validates the combination.

Conclusion

Learn how to combine structured responses with other Cerebras capabilities:
  • Tool Calling: Connect models to external functions and data.
  • Streaming: Process response chunks as they are generated.
  • CePO: Improve reasoning with test-time compute.