Generate structured data with the Cerebras Inference API
Initial Setup
Defining the Schema
required
array you define in your schema, you must set additionalProperties
to false
.Using Structured Outputs
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.$ref
with $defs
to define reusable schema components within your JSON schema. This is useful for avoiding repetition and creating more maintainable schemas.
anyOf
to 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.false
. For every required
array you define in your schema, you must set additionalProperties
to false
.enum
keyword to whitelist the exact literals a field may take. See rating
in the example below.model_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.
response_format
parameter to json_object
:
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" } |
strict
is set to true
in the JSON schema.Schema Limitations
additionalProperties
is not explicitly set to false. We quietly handle this case."type": ["string", "null"]
is not supported. Use anyOf
instead, and remember that all properties must be listed in the required
array. See the example below.Property Handling
properties
must also appear in the required
array. If a property may be null
, keep it in required
and add a union with anyOf
.Array Validation
items: true
is not supported for JSON schema array types.items: false
is supported when used with prefixItems
for tuple-like arrays with validation rules.Schema Structure
$anchor
keyword is not supported - use relative paths within definitions/references instead.$defs
instead of definitions
for reusable schema components.Supported Reference Patterns
"$ref": "#/$defs/cast_member"