You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: pages/generative-apis/how-to/use-structured-outputs.mdx
+65-64Lines changed: 65 additions & 64 deletions
Original file line number
Diff line number
Diff line change
@@ -31,22 +31,19 @@ There are several ways to interact with language models:
31
31
32
32
## Types of structured outputs
33
33
34
-
-**JSON mode** (schemaless):
34
+
-**Structured outputs (schema mode)**:
35
+
- Type `{"type": "json_schema"}`
36
+
- This mode enforces a strict schema format, where the output adheres to the predefined structure.
37
+
- Supports complex types and validation mechanisms as per the [JSON schema specification](https://json-schema.org/specification/), including nested schemas composition (`anyOf`, `allOf`, `oneOf` etc), `$ref`, `all` types, and regular expressions.
38
+
39
+
-**JSON mode** (Legacy method):
35
40
- Type: `{"type": "json_object"}`
36
41
- This mode is non-deterministic and allows the model to output a JSON object without strict validation.
37
42
- Useful for flexible outputs when you expect the model to infer a reasonable structure based on your prompt.
38
-
- JSON mode is older and has been used by developers since early API implementations.
- This mode enforces a strict schema format, where the output adheres to the predefined structure.
43
-
- Supports complex types and validation mechanisms as per the [JSON schema specification](https://json-schema.org/specification/).
44
-
- Structured outputs is a newer feature implemented by OpenAI in 2024 to enable stricter, schema-based response formatting.
43
+
- JSON mode is older and has been used by developers since early API implementations but lack reliability in response formats.
45
44
46
45
<Messagetype="note">
47
-
- All LLMs on the Scaleway library support **JSON mode** and **Structured outputs**, however, the quality of results will vary in the schemaless JSON mode.
48
-
- JSON mode: It is important to explicitly ask the model to generate a JSON output either in system prompt or user prompt. To prevent infinite generations, model providers most often encourage users to ask the model for short JSON objects.
49
-
- Structured outputs: Scaleway supports the [JSON schema specification](https://json-schema.org/specification/) including nested schemas composition (`anyOf`, `allOf`, `oneOf` etc), `$ref`, `all` types, and regular expressions.
46
+
- All LLMs on the Scaleway library support **Structured outputs** and **JSON mode**. However, schemaless **JSON mode** will produce lower quality result and is not recommended.
50
47
</Message>
51
48
52
49
## Code examples
@@ -58,7 +55,7 @@ There are several ways to interact with language models:
58
55
```
59
56
</Message>
60
57
61
-
The following Python examples demonstrate how to use both **JSON mode** and **Structured outputs** to generate structured responses.
58
+
The following Python examples demonstrate how to use both **Structured outputs** and to generate structured responses.
62
59
63
60
We will send to our LLM a voice note transcript in order to structure it.
64
61
Below is our base code:
@@ -94,52 +91,6 @@ TRANSCRIPT = (
94
91
)
95
92
```
96
93
97
-
### Using JSON mode (schemaless)
98
-
99
-
In JSON mode, you can prompt the model to output a JSON object without enforcing a strict schema.
100
-
101
-
```python
102
-
extract = client.chat.completions.create(
103
-
messages=[
104
-
{
105
-
"role": "system",
106
-
"content": "The following is a voice message transcript. Only answer in JSON.",
"task": "prepare dinner (pasta with garlic bread)",
133
-
"priority": "high"
134
-
},
135
-
{
136
-
"task": "catch up on phone calls",
137
-
"priority": "medium"
138
-
}
139
-
]
140
-
}
141
-
```
142
-
143
94
### Using structured outputs with JSON schema (Pydantic)
144
95
145
96
Using [Pydantic](https://docs.pydantic.dev/latest/concepts/models/), users can define the schema as a Python class and enforce the model to return results adhering to this schema.
"content": "The following is a voice message transcript. Only answer in JSON.",
145
+
"content": "The following is a voice message transcript. Only answer in JSON using '{' as the first character.",
195
146
},
196
147
{
197
148
"role": "user",
@@ -240,12 +191,62 @@ Output example:
240
191
When using the OpenAI SDKs like in the examples above, you are expected to set `additionalProperties` to false, and to specify all your properties as required.
241
192
</Message>
242
193
194
+
### Using JSON mode (schemaless, Legacy method)
195
+
196
+
<Messagetype="warning">
197
+
- When using the OpenAI SDKs like in the examples above, you are expected to set `additionalProperties` to false, and to specify all your properties as required.
198
+
- JSON mode: It is important to explicitly ask the model to generate a JSON output either in system prompt or user prompt. To prevent infinite generations, model providers most often encourage users to ask the model for short JSON objects. Prompt example: `Only answer in JSON using '{' as the first character.`.
199
+
</Message>
200
+
201
+
In JSON mode, you can prompt the model to output a JSON object without enforcing a strict schema.
202
+
203
+
```python
204
+
extract = client.chat.completions.create(
205
+
messages=[
206
+
{
207
+
"role": "system",
208
+
"content": "The following is a voice message transcript. Only answer in JSON using '{' as the first character.",
"task": "prepare dinner (pasta with garlic bread)",
235
+
"priority": "high"
236
+
},
237
+
{
238
+
"task": "catch up on phone calls",
239
+
"priority": "medium"
240
+
}
241
+
]
242
+
}
243
+
```
244
+
243
245
## Conclusion
244
246
245
-
Using structured outputs with LLMs can significantly enhance data handling in your applications.
246
-
By choosing between JSON mode and Structured outputs with JSON schema, you control the consistency and structure of the model's responses to suit your specific needs.
247
+
Using structured outputs with LLMs can significantly improve their reliability, especially to implement agentic use cases.
247
248
248
-
-**JSON mode** is flexible but less predictable.
249
249
-**Structured outputs** provide strict adherence to a predefined schema, ensuring consistency.
250
+
-**JSON mode** (Legacy Method) is flexible but less predictable.
250
251
251
-
Experiment with both methods to determine which best fits your application's requirements.
252
+
We recommend using Structured outputs (`json_schema`) for most use cases.
0 commit comments