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
+66-66Lines changed: 66 additions & 66 deletions
Display the source diff
Display the rich diff
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 lacks 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 in the Scaleway library support **Structured outputs** and **JSON mode**. However, a schemaless **JSON mode** will produce lower quality results and is not recommended.
50
47
</Message>
51
48
52
49
## Code examples
@@ -58,10 +55,9 @@ 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 **Structured outputs** to generate structured responses.
62
59
63
-
We will send to our LLM a voice note transcript in order to structure it.
64
-
Below is our base code:
60
+
We are using the base code below to send our LLM a voice note transcript to structure:
65
61
66
62
```python
67
63
import json
@@ -94,52 +90,6 @@ TRANSCRIPT = (
94
90
)
95
91
```
96
92
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
93
### Using structured outputs with JSON schema (Pydantic)
144
94
145
95
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.",
144
+
"content": "The following is a voice message transcript. Only answer in JSON using '{' as the first character.",
195
145
},
196
146
{
197
147
"role": "user",
@@ -240,12 +190,62 @@ Output example:
240
190
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
191
</Message>
242
192
193
+
### Using JSON mode (schemaless, Legacy method)
194
+
195
+
<Messagetype="important">
196
+
- When using the OpenAI SDKs as in the examples above, you are expected to set `additionalProperties` to false, and to specify all your properties as required.
197
+
- JSON mode: It is important to explicitly ask the model to generate a JSON output either in the 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.`.
198
+
</Message>
199
+
200
+
In JSON mode, you can prompt the model to output a JSON object without enforcing a strict schema.
201
+
202
+
```python
203
+
extract = client.chat.completions.create(
204
+
messages=[
205
+
{
206
+
"role": "system",
207
+
"content": "The following is a voice message transcript. Only answer in JSON using '{' as the first character.",
"task": "prepare dinner (pasta with garlic bread)",
234
+
"priority": "high"
235
+
},
236
+
{
237
+
"task": "catch up on phone calls",
238
+
"priority": "medium"
239
+
}
240
+
]
241
+
}
242
+
```
243
+
243
244
## Conclusion
244
245
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.
246
+
Using structured outputs with LLMs can significantly improve their reliability, especially to implement agentic use cases.
247
247
248
-
-**JSON mode** is flexible but less predictable.
249
248
-**Structured outputs** provide strict adherence to a predefined schema, ensuring consistency.
249
+
-**JSON mode** (Legacy Method) is flexible but less predictable.
250
250
251
-
Experiment with both methods to determine which best fits your application's requirements.
251
+
We recommend using structured outputs (`json_schema`) for most use cases.
0 commit comments