Skip to content

Commit b6cce8b

Browse files
committed
fix
1 parent 1b49ea4 commit b6cce8b

File tree

7 files changed

+151
-58
lines changed

7 files changed

+151
-58
lines changed

articles/ai-foundry/model-inference/includes/use-structured-outputs/csharp.md

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@ ms.topic: how-to
1010
ms.date: 1/21/2025
1111
ms.author: mopeakande
1212
ms.reviewer: fasantia
13-
ms.custom: references_regions
1413
zone_pivot_groups: azure-ai-inference-samples
1514
---
1615

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
---
2+
manager: nitinme
3+
ms.service: azure-ai-model-inference
4+
ms.topic: include
5+
ms.date: 1/31/2025
6+
ms.author: fasantia
7+
author: santiagxf
8+
---
9+
10+
[!INCLUDE [Feature preview](~/reusable-content/ce-skilling/azure/includes/ai-studio/includes/feature-preview.md)]
11+
12+
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 to Azure AI model inference in Azure AI services.
13+
14+
Typical scenarios of structured outputs include:
15+
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.
21+
22+
## Prerequisites
23+
24+
To use structured outputs with chat completions models in your application, you need:
25+
26+
[!INCLUDE [how-to-prerequisites](../how-to-prerequisites.md)]
27+
28+
* A chat completions model deployment with JSON and structured outputs support. If you don't have one read [Add and configure models to Azure AI services](../../how-to/create-model-deployments.md).
29+
30+
* You can check which models support structured outputs by checking the column **Response format** in the [Models](../../concepts/models.md) article.
31+
32+
* This article uses `Cohere-command-r-plus-08-2024`.

articles/ai-foundry/model-inference/includes/use-structured-outputs/java.md

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@ ms.topic: how-to
1010
ms.date: 1/21/2025
1111
ms.author: mopeakande
1212
ms.reviewer: fasantia
13-
ms.custom: references_regions
1413
zone_pivot_groups: azure-ai-inference-samples
1514
---
1615

articles/ai-foundry/model-inference/includes/use-structured-outputs/javascript.md

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@ ms.topic: how-to
1010
ms.date: 1/21/2025
1111
ms.author: mopeakande
1212
ms.reviewer: fasantia
13-
ms.custom: references_regions
1413
zone_pivot_groups: azure-ai-inference-samples
1514
---
1615

articles/ai-foundry/model-inference/includes/use-structured-outputs/python.md

Lines changed: 68 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -10,28 +10,13 @@ ms.topic: how-to
1010
ms.date: 1/21/2025
1111
ms.author: mopeakande
1212
ms.reviewer: fasantia
13-
ms.custom: references_regions
1413
zone_pivot_groups: azure-ai-inference-samples
1514
---
1615

17-
[!INCLUDE [Feature preview](~/reusable-content/ce-skilling/azure/includes/ai-studio/includes/feature-preview.md)]
18-
19-
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 to Azure AI model inference in Azure AI services.
20-
21-
## Prerequisites
22-
23-
To use structured outputs with chat completions models in your application, you need:
24-
25-
[!INCLUDE [how-to-prerequisites](../how-to-prerequisites.md)]
16+
[!INCLUDE [intro](intro.md)]
2617

2718
[!INCLUDE [how-to-prerequisites-python](../how-to-prerequisites-python.md)]
2819

29-
* A chat completions model deployment with JSON and structured outputs support. If you don't have one read [Add and configure models to Azure AI services](../../how-to/create-model-deployments.md).
30-
31-
* You can check which models support structured outputs by checking the column **Response format** in the [Models](../../concepts/models.md) article.
32-
33-
* This article uses `Cohere-command-r-plus-08-2024`.
34-
3520
* Initialize a client to consume the model:
3621

3722
```python
@@ -104,20 +89,21 @@ __github_issue_schema.json__
10489
}
10590
```
10691

107-
Let's load this schema:
108-
109-
```python
110-
with open("github_issue_schema.json", "r") as f:
111-
github_issue_schema = json.load(f)
112-
```
113-
11492
When defining schemas, follow these recommendations:
11593

11694
> [!div class="checklist"]
11795
> * Use clear and expressive keys.
11896
> * Use `_` if you need to separate words to convey meaning.
11997
> * Create clear titles and descriptions for important keys in your structure.
12098
> * Evaluate multiple structures until finding the one that works best for your use case.
99+
> * Take into account [limitations when indicating schemas](#supported-schemas). Limitations may vary per model.
100+
101+
Let's load this schema:
102+
103+
```python
104+
with open("github_issue_schema.json", "r") as f:
105+
github_issue_schema = json.load(f)
106+
```
121107

122108
### Use structure outputs
123109

@@ -163,7 +149,6 @@ print(json.dumps(json_response_message, indent=4))
163149
}
164150
```
165151

166-
167152
## Use Pydantic objects
168153

169154
Maintaining JSON schemas by hand is difficult and prone to errors. AI developers usually use [Pydantic](https://docs.pydantic.dev/) objects to describe the shapes of a given object. Pydantic is an open-source data validation library where you can flexibly define data structures for your applications.
@@ -175,7 +160,6 @@ The following example shows how you can use Pydantic to define an schema for a G
175160
```python
176161
from pydantic import BaseModel
177162
from typing import Literal
178-
from enum import Enum
179163

180164
class Issue(BaseModel, extra="forbid"):
181165
title: str
@@ -244,13 +228,69 @@ print(json.dumps(json_response_message, indent=4))
244228

245229
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.
246230

247-
The following example uses validators in Pydantic to verify the schema:
231+
It's a best practice to uses validators to ensure you get valid structures. In Pydantic, you can verify the schema of a given object as follows:
248232

249233
```python
250234
from pydantic import ValidationError
251235

252236
try:
253-
Issue.model_validate(json.loads(response.choices[0].message.content), strict=True)
237+
Issue.model_validate(json_response_message, strict=True)
254238
except ValidationError as e:
255239
print(f"Validation error: {e}")
256-
```
240+
```
241+
242+
### Supported schemas
243+
244+
There are some limitations that models may place in how schemas are defined. When using structure outputs, consider the following limitations. Notice that limitations may vary per model.
245+
246+
#### Optional fields
247+
248+
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.
249+
250+
```python
251+
from pydantic import BaseModel
252+
from typing import Literal, Union
253+
254+
class Issue(BaseModel, extra="forbid"):
255+
title: str
256+
description: str
257+
type: Literal["Bug", "Feature", "Documentation", "Regression"]
258+
operating_system: Union[str, None]
259+
```
260+
261+
#### Nested types
262+
263+
Models may support indicating nesting types. You can compose complex structures as needed:
264+
265+
```python
266+
from pydantic import BaseModel
267+
from typing import Literal
268+
269+
class Project(BaseModel, extra="forbid"):
270+
name: str
271+
owner: str
272+
273+
class Issue(BaseModel, extra="forbid"):
274+
title: str
275+
description: str
276+
type: Literal["Bug", "Feature", "Documentation", "Regression"]
277+
operating_system: str
278+
project: Project
279+
```
280+
281+
Nested types also include recursive definition of types:
282+
283+
```python
284+
from pydantic import BaseModel
285+
from typing import Literal, List
286+
287+
class Issue(BaseModel, extra="forbid"):
288+
title: str
289+
description: str
290+
type: Literal["Bug", "Feature", "Documentation", "Regression"]
291+
operating_system: str
292+
related_issues: List[Issue]
293+
```
294+
295+
296+
[!INCLUDE [supported-schemas](supported-schemas.md)]

articles/ai-foundry/model-inference/includes/use-structured-outputs/rest.md

Lines changed: 42 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -10,23 +10,10 @@ ms.topic: how-to
1010
ms.date: 1/21/2025
1111
ms.author: mopeakande
1212
ms.reviewer: fasantia
13-
ms.custom: references_regions
1413
zone_pivot_groups: azure-ai-inference-samples
1514
---
1615

17-
[!INCLUDE [Feature preview](~/reusable-content/ce-skilling/azure/includes/ai-studio/includes/feature-preview.md)]
18-
19-
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 to Azure AI model inference in Azure AI services.
20-
21-
To use structured outputs with chat completions models in your application, you need:
22-
23-
[!INCLUDE [how-to-prerequisites](../how-to-prerequisites.md)]
24-
25-
* A chat completions model deployment with JSON and structured outputs support. If you don't have one read [Add and configure models to Azure AI services](../../how-to/create-model-deployments.md).
26-
27-
* You can check which models support structured outputs by checking the column **Response format** in the [Models](../../concepts/models.md) article.
28-
29-
* This article uses `Cohere-command-r-plus-08-2024`.
16+
[!INCLUDE [intro](intro.md)]
3017

3118
## How to use structured outputs
3219

@@ -78,12 +65,16 @@ __github_issue_schema.json__
7865

7966
We can use structure outputs with the defined schema as follows:
8067

68+
__Request__
69+
8170
```http
8271
POST https://<resource>.services.ai.azure.com/models/chat/completions?api-version=2024-05-01-preview
8372
Content-Type: application/json
8473
api-key: <key>
8574
```
8675

76+
__Body__
77+
8778
```json
8879
{
8980
"messages": [
@@ -92,13 +83,11 @@ api-key: <key>
9283
"content": "Extract structured information from GitHub issues opened in our project.
9384

9485
Provide
95-
- The title of the issue
96-
- A 1-2 sentence description of the project
97-
- The type of issue (Bug, Feature, Documentation, Regression)
98-
- The operating system the issue was reported on
99-
- Whether the issue is a duplicate of another issue
100-
101-
Do not guess."
86+
- The title of the issue.
87+
- A 1-2 sentence description of the project.
88+
- The type of issue (Bug, Feature, Documentation, Regression).
89+
- The operating system the issue was reported on.
90+
- Whether the issue is a duplicate of another issue."
10291
},
10392
{
10493
"role": "user",
@@ -142,11 +131,37 @@ api-key: <key>
142131

143132
Let's see how this works:
144133

145-
```output
134+
__Response__
135+
136+
```json
146137
{
147-
"title": "Metadata tags issue on access control lists - ADLSgen2 setup",
148-
"description": "Our project seems to have an issue with the metadata tag for groups when deploying the application with access control lists and necessary settings.",
149-
"type": "Bug",
150-
"operating_system": "Windows 10"
138+
"id": "0a1234b5de6789f01gh2i345j6789klm",
139+
"object": "chat.completion",
140+
"created": 1718726686,
141+
"model": "mistral-large-2407",
142+
"choices": [
143+
{
144+
"index": 0,
145+
"message": {
146+
"role": "assistant",
147+
"content": "{
148+
\"title\": "Metadata tags issue on access control lists - ADLSgen2 setup\",
149+
\"description\": "Our project seems to have an issue with the metadata tag for groups when deploying the application with access control lists and necessary settings.\",
150+
\"type\": \"Bug\",
151+
\"operating_system\": \"Windows 10\"
152+
}",
153+
"tool_calls": null
154+
},
155+
"finish_reason": "stop",
156+
"logprobs": null
157+
}
158+
],
159+
"usage": {
160+
"prompt_tokens": 150,
161+
"total_tokens": 246,
162+
"completion_tokens": 96
163+
}
151164
}
152-
```
165+
```
166+
167+
[!INCLUDE [supported-schemas](supported-schemas.md)]
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
---
2+
manager: nitinme
3+
ms.service: azure-ai-model-inference
4+
ms.topic: include
5+
ms.date: 1/31/2025
6+
ms.author: fasantia
7+
author: santiagxf
8+
---
9+

0 commit comments

Comments
 (0)