Skip to content

Commit 7224c93

Browse files
committed
fix
1 parent 56e8dc3 commit 7224c93

File tree

2 files changed

+38
-10
lines changed
  • articles/ai-foundry/model-inference/includes/use-structured-outputs

2 files changed

+38
-10
lines changed

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

Lines changed: 34 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -20,14 +20,16 @@ When working with software, it's more challenging to parse free-form text output
2020

2121
## Prerequisites
2222

23-
To use chat completion models in your application, you need:
23+
To use structured outputs with chat completions models in your application, you need:
2424

2525
[!INCLUDE [how-to-prerequisites](../how-to-prerequisites.md)]
2626

2727
[!INCLUDE [how-to-prerequisites-python](../how-to-prerequisites-python.md)]
2828

2929
* 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).
3030

31+
* You can check which models support structured outputs by checking the column **Response format** in the [Models](../../concepts/models.md) article.
32+
3133
* This article uses `Cohere-command-r-plus-08-2024`.
3234

3335
* Initialize a client to consume the model:
@@ -109,6 +111,14 @@ with open("github_issue_schema.json", "r") as f:
109111
github_issue_schema = json.load(f)
110112
```
111113

114+
When defining schemas, follow these recommendations:
115+
116+
> [!div class="checklist"]
117+
> * Use clear and expressive keys.
118+
> * Use `_` if you need to separate words to convey meaning.
119+
> * Create clear titles and descriptions for important keys in your structure.
120+
> * Evaluate multiple structures until finding the one that works best for your use case.
121+
112122
### Use structure outputs
113123

114124
We can use structure outputs with the defined schema as follows:
@@ -158,6 +168,8 @@ print(json.dumps(json_response_message, indent=4))
158168

159169
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.
160170

171+
### Define the schema
172+
161173
The following example shows how you can use Pydantic to define an schema for a GitHub issue.
162174

163175
```python
@@ -175,15 +187,17 @@ class Issue(BaseModel, extra="forbid"):
175187
Some things to notice:
176188

177189
> [!div class="checklist"]
178-
> We represent schemas using a class that inherits from `BaseModel`.
179-
> We set `extra="forbid"` to instruct Pyndantic to do not accept additional properties from what we have specified.
180-
> We use type annotations to indicate the expected types.
181-
> `Literal` indicates we expect specific fixed values.
190+
> * We represent schemas using a class that inherits from `BaseModel`.
191+
> * We set `extra="forbid"` to instruct Pyndantic to do not accept additional properties from what we have specified.
192+
> * We use type annotations to indicate the expected types.
193+
> * `Literal` indicates we expect specific fixed values.
182194
183195
```python
184196
github_issue_schema = Issue.model_json_schema()
185197
```
186198

199+
### Use structure outputs
200+
187201
Let's see how we can use the schema in the same way:
188202

189203
```python
@@ -225,3 +239,18 @@ print(json.dumps(json_response_message, indent=4))
225239
"operating_system": "Windows 10"
226240
}
227241
```
242+
243+
### Validate
244+
245+
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.
246+
247+
The following example uses validators in Pydantic to verify the schema:
248+
249+
```python
250+
from pydantic import ValidationError
251+
252+
try:
253+
Issue.model_validate(json.loads(response.choices[0].message.content), strict=True)
254+
except ValidationError as e:
255+
print(f"Validation error: {e}")
256+
```

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

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -18,16 +18,15 @@ zone_pivot_groups: azure-ai-inference-samples
1818

1919
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.
2020

21-
## Prerequisites
22-
23-
To use chat completion models in your application, you need:
21+
To use structured outputs with chat completions models in your application, you need:
2422

2523
[!INCLUDE [how-to-prerequisites](../how-to-prerequisites.md)]
2624

27-
* A chat completions model deployment with JSON and structure outputs support. If you don't have one read [Add and configure models to Azure AI services](../../how-to/create-model-deployments.md).
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).
2826

29-
* This article uses `Cohere-command-r-plus-08-2024`.
27+
* You can check which models support structured outputs by checking the column **Response format** in the [Models](../../concepts/models.md) article.
3028

29+
* This article uses `Cohere-command-r-plus-08-2024`.
3130

3231
## How to use structured outputs
3332

0 commit comments

Comments
 (0)