Structured Outputs
Generate structured data with the Cerebras Inference API
Structured outputs with strict adherence is currently in beta, released in version 1.23 of the Cerebras Inference Cloud SDK. Improvements for speed and expanded support will be released in coming weeks, with documentation updated accordingly.
Structured outputs is a feature that can enforce consistent JSON outputs for models in the Cerebras Inference API. This is particularly useful when building applications that need to process AI-generated data programmatically. Some of the key benefits of using structured outputs are:
- 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.
In this tutorial, we’ll explore how to use structured outputs with the Cerebras SDK, from basic JSON responses to strictly enforced schemas.
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.
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:
Using Structured Outputs
Next, use the schema in your API call by setting the 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.
Sample output:
Now you have a structured JSON response from the model, which can be used in your application.
Advanced Schema Features
Your schema can include various JSON Schema features:
-
Types: String, Number, Boolean, Integer, Object, Array, Enum, anyOf (max of 5), null
-
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 example, a more complex schema might look like:
When used with the API, you might get a response like:
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. Note: we recommend using structured outputs (with strict set to true) whenever possible, as it provides more predictable and reliable results.
To use JSON mode, simply set the 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) |
Compatible models | llama-3.1-8b and llama-3.1-70b | llama-3.1-8b and llama-3.1-70b |
Enabling | response_format: { type: "json_schema", json_schema: {"strict": true, "schema": ...} } | response_format: { type: "json_object" } |
Variation 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.
Structured outputs do not work with function calling yet. This feature will be supported in a future release.
Conclusion
Structured outputs with JSON schema enforcement ensures your AI-generated responses follow a consistent, predictable format. This makes it easier to build reliable applications that can process AI outputs programmatically without worrying about unexpected data structures or missing fields.
Was this page helpful?