Structured Outputs Guide
Structured outputs is currently a beta feature
Overview
We currently support two types of structured outputs: JSON Schema and Regex. LLM responses will work to match the specified format, except for the following cases:
- The output exceeds
max_tokens
Enabling the structured outputs can be done by adding a response_format
field in the request:
JSON Schema
-
response_format: { type: "json_schema", json_schema: {"schema": object} }
. -
The schema should be a valid JSON schema object.
Regex (only avilable for sonar
right now)
-
response_format: { type: "regex", regex: {"regex": str} }
. -
The regex is a regular expression string.
We recommend to give the LLM some hints about the output format in the prompts.
The first request with a new JSON Schema or Regex expects to incur delay on the first token. Typically, it takes 10 to 30 seconds to prepare the new schema. Once the schema has been prepared, the subsequent requests will not see such delay.
Examples
1. Get a response in JSON format
Request
Response
2. Use a regex to output the format
Request
Response
Best Practices
Generating responses in a JSON Format
For Python users, we recommend using the Pydantic library to generate JSON schema.
Unsupported JSON Schemas
Recursive JSON schema is not supported. As a result of that, unconstrained objects are not supported either. Here’s a few example of unsupported schemas:
Generating responses using a regex
Supported Regex
- Characters:
\d
,\w
,\s
,.
- Character classes:
[0-9A-Fa-f]
,[^x]
- Quantifiers:
*
,?
,+
,{3}
,{2,4}
,{3,}
- Alternation:
|
- Group:
( ... )
- Non-capturing group:
(?: ... )
- Positive lookahead:
(?= ... )
- Negative lookahead:
(?! ... )
Unsupported Regex
- Contents of group:
\1
- Anchors:
^
,$
,\b
- Positive look-behind:
(?<= ... )
- Negative look-behind:
(?<! ... )
- Recursion:
(?R)