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/public_cloud/ai_machine_learning/endpoints_guide_05_structured_output/guide.en-gb.md
+36-20Lines changed: 36 additions & 20 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -1,7 +1,7 @@
1
1
---
2
2
title: AI Endpoints - Structured Output
3
3
excerpt: Learn how to use Structured Output with OVHcloud AI Endpoints
4
-
updated: 2025-04-28
4
+
updated: 2025-08-05
5
5
---
6
6
7
7
> [!primary]
@@ -15,15 +15,17 @@ updated: 2025-04-28
15
15
16
16
**Structured Output** is a powerful feature that allows you to enforce specific formats for the responses from AI models. By using the `response_format` parameter in your API calls, you can define how you want the output to be structured, ensuring consistency and ease of integration with your applications.
17
17
This is particularly useful when you need the AI model to return data in a specific JSON format.
18
-
The [JSON schema](https://json-schema.org/) specification can be used to describe what data structure should the output adhere to, and the AI model will generate responses that match it.
18
+
The [JSON schema](https://json-schema.org/) specification can be used to describe what data structure should the output adhere to, and the AI model will generate responses that match it.
19
19
This feature allows for seamless integration of AI-generated data into your applications, enabling you to build robust and consistent workflows.
20
20
21
21
## Objective
22
22
23
-
This documentation provides an overview on how to use structured outputs with the various AI models offered on [AI Endpoints](https://endpoints.ai.cloud.ovh.net/).
24
-
The examples provided in this guide will be using the [Llama 3.3 70b model](https://endpoints.ai.cloud.ovh.net/models/c968b503-27fa-451d-b59d-1b0ff91d304d)
23
+
This documentation provides an overview on how to use structured outputs with the various AI models offered on [AI Endpoints](https://endpoints.ai.cloud.ovh.net/).
24
+
25
+
The examples provided in this guide will be using the [Llama 3.3 70b model](https://endpoints.ai.cloud.ovh.net/models/c968b503-27fa-451d-b59d-1b0ff91d304d).
25
26
26
27
Visit our [Catalog](https://endpoints.ai.cloud.ovh.net/catalog) to find out which models are compatible with Structured Output.
28
+
27
29
The output formats managed by each model are defined in the Response Format section:
28
30
29
31

@@ -36,6 +38,7 @@ The examples provided during this guide can be used with one of the following en
36
38
> **Python**
37
39
>>
38
40
>> A [Python](https://www.python.org/) environment with the [openai client](https://pypi.org/project/openai/) and the pydantic library installed.
41
+
>>
39
42
>> ```sh
40
43
>> pip install openai pydantic
41
44
>>```
@@ -44,6 +47,7 @@ The examples provided during this guide can be used with one of the following en
44
47
>>
45
48
>> A [Node.js](https://nodejs.org/en) environment with the [request](https://www.npmjs.com/package/request) library.
46
49
>> Request can be installed using [NPM](https://www.npmjs.com/):
50
+
>>
47
51
>>```sh
48
52
>> npm install request
49
53
>>```
@@ -55,25 +59,26 @@ The examples provided during this guide can be used with one of the following en
55
59
56
60
### Authentication & rate limiting
57
61
58
-
Most of the examples provided in this guide are using the anonymous authentication which makes it simpler to use but may cause rate limiting issues.
62
+
Most of the examples provided in this guide use anonymous authentication, which makes it simpler to use but may cause rate limiting issues.
59
63
If you wish to enable authentication using your own token, simply specify your API key within the requests.
60
-
Follow the following instructions in the [AI Endpoints - Getting Started](/pages/public_cloud/ai_machine_learning/endpoints_guide_01_getting_started) for more information on authentication.
64
+
65
+
Follow the instructions in the [AI Endpoints - Getting Started](/pages/public_cloud/ai_machine_learning/endpoints_guide_01_getting_started) guide for more information on authentication.
61
66
62
67
## Instructions
63
68
64
69
The `response_format` parameter of the Chat Completion API allows us to enable and configure the Structured Output features.
70
+
65
71
Models that support structured output can manage the three following modes:
66
72
67
73
- `{"type": "text"}`
68
74
The default textual format. This is the same as specifying no `response_format`.
69
75
70
76
- `{"type": "json_object"}`
71
-
The JSON object format is a legacy format that was introduced with the first iteration of Structured Outputs.
72
-
This mode is non-deterministic and allows the model to output a JSON object without strict validation.
77
+
The JSON object format is a legacy format that was introduced with the first iteration of Structured Outputs. This mode is non-deterministic and allows the model to output a JSON object without strict validation.
73
78
74
79
- `{"type": "json_schema", "json_schema": .. }`
75
-
[JSON schema](https://json-schema.org/) is a very powerful tool used to specify and validate a JSON data structure.
76
-
This latest kind of response_format allows us to enforce custom output formats in LLM outputs using this specification and ensure consistency and interoperability with a variety of platforms and applications.
80
+
[JSON schema](https://json-schema.org/) is a very powerful tool used to specify and validate a JSON data structure. This latest kind of `response_format` allows us to enforce custom output formats in LLM outputs using this specification and ensure consistency and interoperability with a variety of platforms and applications.
81
+
77
82
When using the JSON schema mode, outputs are deterministic and will always adhere to the schema specified.
78
83
79
84
We recommend using JSON schema over JSON object whenever possible.
@@ -131,6 +136,7 @@ The following code samples provide a simple example on how to specify a JSON sch
@@ -295,11 +304,11 @@ The following code samples provide a simple example on how to specify a JSON sch
295
304
>>```
296
305
>>
297
306
>> This example shows us how to use the JSON schema response format with Javascript.
307
+
>>
298
308
299
309
### JSON object
300
310
301
-
The following code samples provide a simple example on how to use the legacy JSON object mode, using the `response_format` parameter.
302
-
Note that when using the JSON object mode, we cannot explicitly specify the schema of the output.
311
+
The following code samples provide a simple example on how to use the legacy JSON object mode, using the `response_format` parameter. Note that when using the JSON object mode, we cannot explicitly specify the schema of the output.
303
312
304
313
> [!tabs]
305
314
>**Python**
@@ -338,6 +347,7 @@ Note that when using the JSON object mode, we cannot explicitly specify the sche
338
347
>>```
339
348
>>
340
349
>> Output:
350
+
>>
341
351
>>```sh
342
352
>> {
343
353
>>"rank": [
@@ -363,6 +373,7 @@ Note that when using the JSON object mode, we cannot explicitly specify the sche
363
373
>**Curl**
364
374
>>
365
375
>> Input query:
376
+
>>
366
377
>>```sh
367
378
>> curl -X POST "https://llama-3-3-70b-instruct.endpoints.kepler.ai.cloud.ovh.net/api/openai_compat/v1/chat/completions" \
368
379
>> -H 'accept: application/json' \
@@ -382,6 +393,7 @@ Note that when using the JSON object mode, we cannot explicitly specify the sche
@@ -430,6 +442,7 @@ Note that when using the JSON object mode, we cannot explicitly specify the sche
430
442
>>```
431
443
>>
432
444
>> Output:
445
+
>>
433
446
>>```sh
434
447
>> {
435
448
>> rank: [
@@ -439,16 +452,18 @@ Note that when using the JSON object mode, we cannot explicitly specify the sche
439
452
>> ]
440
453
>> }
441
454
>>```
455
+
>>
442
456
443
457
### Tips and best practices
444
458
445
459
This section contains additional tips that may improve the performance of Structured Output queries.
446
460
447
461
#### Streaming
448
462
449
-
All kinds of response_format are compatible with streaming. To enable streaming, simply use `"streaming": true`in your request's body and process the stream accordingly.
463
+
All kinds of `response_format` are compatible with streaming. To enable streaming, simply use `"streaming": true`in your request's body and process the stream accordingly.
450
464
451
465
Example with python:
466
+
452
467
```python
453
468
from pydantic import BaseModel
454
469
import openai
@@ -505,6 +520,7 @@ for language in language_rankings.languages:
@@ -519,23 +535,24 @@ Java is the n°3 language (https://www.java.com/)
519
535
#### Schema definition
520
536
521
537
Some considerations about the JSON schema definition:
538
+
522
539
- Structured output currently supports a subset of the [JSON schema specification](https://json-schema.org/specification). Some features may not be compatible.
523
540
- The models will generate the output following alphabetical order of the JSON schema keys. It may be useful to rename your fields to enforce a specific order during generation.
524
-
- To avoid divergence, we recommend setting [additional properties](https://json-schema.org/understanding-json-schema/reference/object#additionalproperties) to `false` and explicity setting the [required fields](https://json-schema.org/learn/getting-started-step-by-step#define-required-properties)
541
+
- To avoid divergence, we recommend setting [additional properties](https://json-schema.org/understanding-json-schema/reference/object#additionalproperties) to `false` and explicity setting the [required fields](https://json-schema.org/learn/getting-started-step-by-step#define-required-properties).
525
542
526
543
Don't hesitate to experiment with different variations of your JSON schemas to reach the best performance!
527
544
528
545
#### Prompting & additional parameters
529
546
530
547
Some additional considerations regarding prompts and model parameters:
531
-
- Even though the response_format can be used to enable structured outputs, models can generally perform better when asked to produce json outputs within the prompt (`messages` field).
548
+
549
+
- Even though the `response_format` can be used to enable structured outputs, models can generally perform better when asked to produce json outputs within the prompt (`messages` field).
532
550
- Most models tend to perform better when using lower temperature for structured outputs.
533
-
- Some model providers may recommend specific system prompts and parameters to use for structured outputs and functioncalling. Don't hesitate to visit the model pages to dive deeper into model specifics ([example for Llama 3.3 on HuggingFace](https://huggingface.co/meta-llama/Llama-3.3-70B-Instruct)).
551
+
- Some model providers may recommend specific system prompts and parameters to use for structured outputs and functioncalling. Don't hesitate to visit the model pages to dive deeper into model specifics ([An example for Llama 3.3 on HuggingFace](https://huggingface.co/meta-llama/Llama-3.3-70B-Instruct)).
534
552
535
553
## Conclusion
536
554
537
-
In this guide, we have explained how to use Structured Output with the [AI Endpoints](https://endpoints.ai.cloud.ovh.net/) models.
538
-
We have provided a comprehensive overview of the feature which can help you perfect your integration of LLM for your own application.
555
+
In this guide, we have explained how to use Structured Output with the [AI Endpoints](https://endpoints.ai.cloud.ovh.net/) models. We have provided a comprehensive overview of the feature which can help you perfect your integration of LLM for your own application.
539
556
540
557
## Go further
541
558
@@ -549,5 +566,4 @@ If you need training or technical assistance to implement our solutions, contact
549
566
550
567
Please send us your questions, feedback and suggestions to improve the service:
551
568
552
-
- On the OVHcloud [Discord server](https://discord.gg/ovhcloud)
553
-
569
+
- On the OVHcloud [Discord server](https://discord.gg/ovhcloud).
0 commit comments