Skip to content

Commit a48e768

Browse files
fpagnybene2k1nerda-codesRoRoJ
authored andcommitted
Update use-structured-outputs.mdx (scaleway#4375)
* Update use-structured-outputs.mdx Updating structured output documentation to identify JSON Mode as a Legacy method that is not recommended anymore. * Apply suggestions from code review Co-authored-by: Néda <[email protected]> * Apply suggestions from code review Co-authored-by: Rowena Jones <[email protected]> Co-authored-by: Néda <[email protected]> * Apply suggestions from code review --------- Co-authored-by: Benedikt Rollik <[email protected]> Co-authored-by: Néda <[email protected]> Co-authored-by: Rowena Jones <[email protected]>
1 parent f59ed5a commit a48e768

File tree

1 file changed

+66
-66
lines changed

1 file changed

+66
-66
lines changed

pages/generative-apis/how-to/use-structured-outputs.mdx

Lines changed: 66 additions & 66 deletions
Original file line numberDiff line numberDiff line change
@@ -31,22 +31,19 @@ There are several ways to interact with language models:
3131

3232
## Types of structured outputs
3333

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):
3540
- Type: `{"type": "json_object"}`
3641
- This mode is non-deterministic and allows the model to output a JSON object without strict validation.
3742
- 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.
39-
40-
- **Structured outputs (schema mode)** (deterministic/structured):
41-
- Type `{"type": "json_schema"}`
42-
- 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.
4544

4645
<Message type="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.
5047
</Message>
5148

5249
## Code examples
@@ -58,10 +55,9 @@ There are several ways to interact with language models:
5855
```
5956
</Message>
6057

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.
6259

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:
6561

6662
```python
6763
import json
@@ -94,52 +90,6 @@ TRANSCRIPT = (
9490
)
9591
```
9692

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.",
107-
},
108-
{
109-
"role": "user",
110-
"content": TRANSCRIPT,
111-
},
112-
],
113-
model=MODEL,
114-
response_format={
115-
"type": "json_object",
116-
},
117-
)
118-
output = json.loads(extract.choices[0].message.content)
119-
print(json.dumps(output, indent=2))
120-
```
121-
122-
Output example:
123-
```json
124-
{
125-
"current_time": "6:30 PM",
126-
"tasks": [
127-
{
128-
"task": "water the plants in the garden",
129-
"priority": "high"
130-
},
131-
{
132-
"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-
14393
### Using structured outputs with JSON schema (Pydantic)
14494

14595
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.
@@ -149,7 +99,7 @@ extract = client.chat.completions.create(
14999
messages=[
150100
{
151101
"role": "system",
152-
"content": "The following is a voice message transcript. Only answer in JSON.",
102+
"content": "The following is a voice message transcript. Only answer in JSON using '{' as the first character.",
153103
},
154104
{
155105
"role": "user",
@@ -191,7 +141,7 @@ extract = client.chat.completions.create(
191141
messages=[
192142
{
193143
"role": "system",
194-
"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.",
195145
},
196146
{
197147
"role": "user",
@@ -240,12 +190,62 @@ Output example:
240190
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.
241191
</Message>
242192

193+
### Using JSON mode (schemaless, Legacy method)
194+
195+
<Message type="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.",
208+
},
209+
{
210+
"role": "user",
211+
"content": TRANSCRIPT,
212+
},
213+
],
214+
model=MODEL,
215+
response_format={
216+
"type": "json_object",
217+
},
218+
)
219+
output = json.loads(extract.choices[0].message.content)
220+
print(json.dumps(output, indent=2))
221+
```
222+
223+
Output example:
224+
```json
225+
{
226+
"current_time": "6:30 PM",
227+
"tasks": [
228+
{
229+
"task": "water the plants in the garden",
230+
"priority": "high"
231+
},
232+
{
233+
"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+
243244
## Conclusion
244245

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.
247247

248-
- **JSON mode** is flexible but less predictable.
249248
- **Structured outputs** provide strict adherence to a predefined schema, ensuring consistency.
249+
- **JSON mode** (Legacy Method) is flexible but less predictable.
250250

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

Comments
 (0)