Skip to content

Commit f39a811

Browse files
authored
Update use-structured-outputs.mdx
Updating structured output documentation to identify JSON Mode as a Legacy method that is not recommended anymore.
1 parent f460d77 commit f39a811

File tree

1 file changed

+65
-64
lines changed

1 file changed

+65
-64
lines changed

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

Lines changed: 65 additions & 64 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 lack 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 on the Scaleway library support **Structured outputs** and **JSON mode**. However, schemaless **JSON mode** will produce lower quality result and is not recommended.
5047
</Message>
5148

5249
## Code examples
@@ -58,7 +55,7 @@ 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 both **Structured outputs** and to generate structured responses.
6259

6360
We will send to our LLM a voice note transcript in order to structure it.
6461
Below is our base code:
@@ -94,52 +91,6 @@ TRANSCRIPT = (
9491
)
9592
```
9693

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-
14394
### Using structured outputs with JSON schema (Pydantic)
14495

14596
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 +100,7 @@ extract = client.chat.completions.create(
149100
messages=[
150101
{
151102
"role": "system",
152-
"content": "The following is a voice message transcript. Only answer in JSON.",
103+
"content": "The following is a voice message transcript. Only answer in JSON using '{' as the first character.",
153104
},
154105
{
155106
"role": "user",
@@ -191,7 +142,7 @@ extract = client.chat.completions.create(
191142
messages=[
192143
{
193144
"role": "system",
194-
"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.",
195146
},
196147
{
197148
"role": "user",
@@ -240,12 +191,62 @@ Output example:
240191
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.
241192
</Message>
242193

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

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

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

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

Comments
 (0)