Skip to content

Commit 679f5d8

Browse files
committed
Merge branch 'guide-structured-output' of https://github.com/huggingface/hub-docs into guide-structured-output
2 parents db1d5eb + 1d3584a commit 679f5d8

File tree

1 file changed

+9
-9
lines changed

1 file changed

+9
-9
lines changed

docs/inference-providers/guides/structured-output.md

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
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.
44

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.
66

77
<Tip>
88

@@ -12,7 +12,7 @@ This guide assumes you have a Hugging Face account. If you don't have one, you c
1212

1313
## What Are Structured Outputs?
1414

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.
1616

1717
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.
1818

@@ -69,7 +69,7 @@ Install the Hugging Face Hub python package:
6969
pip install huggingface_hub
7070
```
7171

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.
7373

7474
```python
7575
import os
@@ -93,7 +93,7 @@ Install the OpenAI python package:
9393
pip install openai
9494
```
9595

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.
9797

9898
```python
9999
import os
@@ -115,7 +115,7 @@ client = OpenAI(
115115

116116
<Tip>
117117

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.
119119

120120
</Tip>
121121

@@ -224,7 +224,7 @@ Both approaches guarantee that your response will match the specified schema. He
224224

225225
<hfoptions id="structured-outputs-implementation">
226226

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.
228228

229229
<hfoption id="huggingface_hub">
230230

@@ -243,7 +243,7 @@ print(f"Abstract Summary: {analysis['abstract_summary']}")
243243

244244
</hfoption>
245245

246-
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.
247247

248248
<hfoption id="openai">
249249

@@ -449,7 +449,7 @@ def analyze_paper_structured():
449449
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:
450450

451451
- **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.
454454
- **Performance optimization**: Choosing the right provider for your structured output needs.
455455

0 commit comments

Comments
 (0)