Structured Outputs
Structured outputs enables Grok models to generate responses in a predefined, organized format rather than free-form text. This is crucial when integrating LLMs into applications where predictable, parseable data is needed.
When you use structured outputs, the model completion message would be a JSON string parseable by the machine. The JSON object schema would match the response_format
you specified in the request body.
Defining JSON schema
This section is for illustration purposes on behind-the-scene (no pun intended) schema definition. It is preferred to use Pydantic or Zod with OpenAI SDK when sending the schema definition, to avoid mistakes in schema definition.
For example, if you want the model to send back a machine-parseable movie rating, such as:
You can specify the following json_schema
in response_format
:
Send the request with curl
:
The parsed object from the model response content:
Using Pydantic or Zod to define and send schema
It is quite error prone to compose and send the json_schema
yourself. Luckily we can define Pydantic or Zod objects, and parse those objects into the request.
Sending request through SDK
With the schema defined in Pydantic or Zod, you can now send the request through OpenAI SDK.
Refusal from model
The model may not be able to parse the answer into the provided schema. In which case, the model will return a non-null refusal
object in message with refusal reason.