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: docs/inference-providers/guides/structured-output.md
+9-9Lines changed: 9 additions & 9 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -2,7 +2,7 @@
2
2
3
3
In this guide, we'll show you how to use Inference Providers to generate structured outputs that follow a specific JSON schema. This is incredibly useful for building reliable AI applications that need predictable, parseable responses.
4
4
5
-
Instead of hoping the model returns properly formatted JSON, structured outputs guarantee that the response will match your exact schema every time. This eliminates the need for complex parsing logic and makes your applications more robust.
5
+
Structured outputs guarantee a model returns a response that matches your exact schema every time. This eliminates the need for complex parsing logic and makes your applications more robust.
6
6
7
7
<Tip>
8
8
@@ -12,7 +12,7 @@ This guide assumes you have a Hugging Face account. If you don't have one, you c
12
12
13
13
## What Are Structured Outputs?
14
14
15
-
Structured outputs make sure that AI model responses always follow a specific structure, typically a JSON Schema. This means you get predictable, type-safe data that integrates easily with your systems. Instead of hoping the model returns valid JSON, you give the AI a strict template to follow, so you always get data in the format you expect.
15
+
Structured outputs make sure model responses always follow a specific structure, typically a JSON Schema. This means you get predictable, type-safe data that integrates easily with your systems. The model follows a strict template so you always get the data in the format you expect.
16
16
17
17
Traditionally, getting structured data from LLMs required prompt engineering (asking the model to "respond in JSON format"), post-processing and parsing the response, and sometimes retrying when parsing failed. This approach is unreliable and can lead to brittle applications.
18
18
@@ -69,7 +69,7 @@ Install the Hugging Face Hub python package:
69
69
pip install huggingface_hub
70
70
```
71
71
72
-
Here's how to initialize the Hugging Face Hub client with the `InferenceClient` class.
72
+
Initialize the `InferenceClient` with an Inference Provider (check this [list](https://huggingface.co/docs/inference-providers/index#partners) for all available providers) and your Hugging Face token.
73
73
74
74
```python
75
75
import os
@@ -93,7 +93,7 @@ Install the OpenAI python package:
93
93
pip install openai
94
94
```
95
95
96
-
Here's how to initialize the OpenAI client with the `OpenAI` class.
96
+
Initialize an `OpenAI` client with the `base_url` and your Hugging Face token.
97
97
98
98
```python
99
99
import os
@@ -115,7 +115,7 @@ client = OpenAI(
115
115
116
116
<Tip>
117
117
118
-
Structured outputs are good use case for selecting a specific provider and model because you want to avoid incompatibility issues between the model, provider and the schema.
118
+
Structured outputs are a good use case for selecting a specific provider and model because you want to avoid incompatibility issues between the model, provider and the schema.
119
119
120
120
</Tip>
121
121
@@ -224,7 +224,7 @@ Both approaches guarantee that your response will match the specified schema. He
224
224
225
225
<hfoptionsid="structured-outputs-implementation">
226
226
227
-
The Hugging Face Hub client returns a `ChatCompletion` object, which contains the response from the model as a string. You can then parse the response to get the structured data using the `json.loads` function.
227
+
The Hugging Face Hub client returns a `ChatCompletion` object, which contains the response from the model as a string. Use the `json.loads` function to parse the response and get the structured data.
The OpenAI client returns a `ChatCompletion`object, which contains the response from the model as a Python object. You can then access the structured data using the `title`and`abstract_summary` attributes of the `PaperAnalysis`class.
246
+
The OpenAI client returns a `ChatCompletion`object, which contains the response from the model as a Python object. Access the structured data using the `title`and`abstract_summary` attributes of the `PaperAnalysis`class.
Now that you understand structured outputs, you probably want to build an application that uses them. Here are some ideas for fun things you can try out:
450
450
451
451
-**Different models**: Experiment with different models. The biggest models are not always the best for structured outputs!
452
-
-**Multi-turn conversations**: Maintaining structured format across conversation turns
453
-
-**Complex schemas**: Building domain-specific schemas for your use case
452
+
-**Multi-turn conversations**: Maintaining structured format across conversation turns.
453
+
-**Complex schemas**: Building domain-specific schemas for your use case.
454
454
-**Performance optimization**: Choosing the right provider for your structured output needs.
0 commit comments