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
When working with software, it's more challenging to parse free-form text outputs coming from language models. Structured outputs, like JSON, provide a clear format that software routines can read and process. This article explains how to use structured outputs to generate specific JSON schemas with the chat completions API with models deployed in Azure AI Foundry Models.
15
+
Free-form outputs of language models can be difficult to parse by software applications. Structured outputs, like JSON, provide a clear format that software applications can read and process. This article explains how to use structured outputs to generate specific JSON schemas with the chat completions API for models deployed in Azure AI Foundry Models.
13
16
14
-
Typical scenarios of structured outputs include:
17
+
The following list describes typical scenarios where structured outputs are useful:
15
18
16
-
> [!div class="checklist"]
17
-
> * You need to extract specific information from a prompt and such information can be described as an schema with specific keys and types.
18
-
> * You need to parse information contained in the prompts.
19
-
> * You are using the model to control a workflow in your application where you can benefit from more rigid structures.
20
-
> * You are using the model as a zero-shot or few-shot learner.
19
+
* You need to extract specific information from a prompt and such information can be described as a schema with specific keys and types.
20
+
* You need to parse information contained in the prompts.
21
+
* You're using the model to control a workflow in your application where you can benefit from more rigid structures.
22
+
* You're using the model as a zero-shot or few-shot learner.
21
23
22
24
## Prerequisites
23
25
24
26
To use structured outputs with chat completions models in your application, you need:
* A chat completions model deployment with JSON and structured outputs support. If you don't have one read [Add and configure Foundry Models](../../how-to/create-model-deployments.md).
30
+
* A chat completions model deployment with JSON and structured outputs support. If you don't have one, read [Add and configure Foundry Models](../../how-to/create-model-deployments.md).
29
31
30
32
* You can check which models support structured outputs by checking the column **Response format** in the [Models](../../concepts/models.md) article.
Structured outputs use JSON schemas to enforce output structure. JSON schemas describe the shape of the JSON object including expected values, types, and which ones are required. ThoseJSON objects are encoded as a string within the response of the model.
31
+
Structured outputs use JSON schemas to enforce output structure. JSON schemas describe the shape of the JSON object, including expected values, types, and which ones are required. ThoseJSON objects are encoded as a string within the response of the model.
32
32
33
33
### Example
34
34
35
-
To exemplify the scenario, let's try to parse the attributes of a GitHub Issue from its description.
35
+
To illustrate, let's try to parse the attributes of a GitHub Issue from its description.
Structured outputs use JSON schemas to enforce output structure. JSON schemas describe the shape of the JSONobject including expected values, types, and which ones are required. Those JSON objects are encoded as a string within the response of the model.
38
+
Structured outputs use JSON schemas to enforce output structure. JSON schemas describe the shape of the JSONobject, including expected values, types, and which ones are required. Those JSON objects are encoded as a string within the response of the model.
39
39
40
40
### Example
41
41
42
-
To exemplify the scenario, let's try to parse the attributes of a GitHub Issue from its description.
42
+
To illustrate, let's try to parse the attributes of a GitHub Issue from its description.
Structured Outputs can still contain mistakes. If you see mistakes, try adjusting your instructions, providing examples in the system instructions, or splitting tasks into simpler subtasks.
255
+
Structured outputs can still contain mistakes. If you see mistakes, try adjusting your instructions, providing examples in the system instructions, or splitting tasks into simpler subtasks.
257
256
258
257
It's a best practice to use validators to ensure you get valid structures. In Pydantic, you can verify the schema of a given object as follows:
259
258
@@ -268,13 +267,13 @@ except ValidationError as e:
268
267
269
268
### Specifying a schema
270
269
271
-
There are some limitations that models may place in schemas definitions. Such limitations may vary per-model. **We recommend reviewing the documentation from the model provider** to verify that your schemas are valid.
270
+
There are some limitations that models might place in schemas definitions. Such limitations might vary per-model. **We recommend reviewing the documentation from the model provider** to verify that your schemas are valid.
272
271
273
272
The following guidelines apply to most of the models:
274
273
275
274
#### Optional fields
276
275
277
-
Some models may require all the fields to be in the `required` section of the schema. If you need to use optional fields, use unions with null types to express that a given field can be optional.
276
+
Some models might require all the fields to be in the `required` section of the schema. If you need to use optional fields, use unions with null types to express that a given field can be optional.
278
277
279
278
```python
280
279
from pydantic import BaseModel
@@ -289,7 +288,7 @@ class Issue(BaseModel, extra="forbid"):
289
288
290
289
#### Nested types
291
290
292
-
Models may support indicating nesting types. You can compose complex structures as needed:
291
+
Models might support indicating nesting types. You can compose complex structures as needed:
293
292
294
293
```python
295
294
from pydantic import BaseModel
@@ -325,13 +324,13 @@ Verify the level of nesting supported by the model you're working with.
325
324
326
325
## Structured outputs in images
327
326
328
-
You can use structured outputs with multi-modal models to extract information from data like images.
327
+
You can use structured outputs with multi-modal models to extract information from data such as image data.
329
328
330
329
Let's consider the following chart:
331
330
332
331
:::image type="content" source="../../media/use-structured-outputs/example-graph-treecover.png" alt-text="An example image showing a chart with the annual loss in thousand square kilometers of global tree cover across different climate zones." lightbox="../../media/use-structured-outputs/example-graph-treecover.png":::
333
332
334
-
We can define a generic schema that can be used to encode the information contained in the chart and then use it for further analysis. We use [Pyndatic objects](#use-pydantic-objects) as described before.
333
+
We can define a generic schema that can be used to encode the information contained in the chart and then use it for further analysis. We use [Pydantic objects as described before](#use-pydantic-objects).
Structured outputs use JSON schemas to enforce output structure. JSON schemas describe the shape of the JSON object including expected values, types, and which ones are required. Those JSON objects are encoded as a string within the response of the model.
20
+
Structured outputs use JSON schemas to enforce output structure. JSON schemas describe the shape of the JSON object, including expected values, types, and which ones are required. Those JSON objects are encoded as a string within the response of the model.
21
21
22
22
### Example
23
23
24
-
To exemplify the scenario, let's try to parse the attributes of a GitHub Issue from its description. The following [example is extracted from a GitHub issue in Azure-Samples repository](https://api.github.com/repos/Azure-Samples/azure-search-openai-demo/issues/2231).
24
+
To illustrate, let's try to parse the attributes of a GitHub Issue from its description. The following [example is extracted from a GitHub issue in Azure-Samples repository](https://api.github.com/repos/Azure-Samples/azure-search-openai-demo/issues/2231).
25
25
26
26
```output
27
27
<!--
@@ -88,9 +88,9 @@ __github_issue_schema.json__
88
88
}
89
89
```
90
90
91
-
### Use structure outputs
91
+
### Use structured outputs
92
92
93
-
We can use structure outputs with the defined schema as follows:
93
+
We can use structured outputs with the defined schema as follows:
94
94
95
95
__Request__
96
96
@@ -186,7 +186,7 @@ __Response__
186
186
187
187
## Structured outputs in images
188
188
189
-
You can use structured outputs with multi-modal models to extract information from data like images.
189
+
You can use structured outputs with multi-modal models to extract information from data such as image data.
190
190
191
191
Let's consider the following chart:
192
192
@@ -275,9 +275,9 @@ __graph_schema.json__
275
275
```
276
276
277
277
278
-
## Use structure outputs
278
+
## Use structured outputs
279
279
280
-
We can use structure outputs with the defined schema as follows:
280
+
We can use structured outputs with the defined schema as follows:
0 commit comments