Skip to content

Commit 5f1ee42

Browse files
committed
add some docs about errors in the README
1 parent ed20d68 commit 5f1ee42

File tree

1 file changed

+102
-0
lines changed

1 file changed

+102
-0
lines changed

packages/inference/README.md

Lines changed: 102 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -120,6 +120,108 @@ await textGeneration({
120120

121121
This will enable tree-shaking by your bundler.
122122

123+
### Error handling
124+
125+
The inference package provides specific error types to help you handle different error scenarios effectively.
126+
127+
#### Error Types
128+
129+
The package defines several error types that extend the base `Error` class:
130+
131+
- `HfInferenceError`: Base error class for all Hugging Face Inference errors
132+
- `HfInferenceInputError`: Thrown when there are issues with input parameters
133+
- `HfInferenceProviderApiError`: Thrown when there are API-level errors from providers
134+
- `HfInferenceHubApiError`: Thrown when there are API-levels errors from the Hugging Face Hub
135+
- `HfInferenceProviderOutputError`: Thrown when there are issues with providers' API responses format
136+
137+
### Example Usage
138+
139+
```typescript
140+
import { InferenceClient } from "@huggingface/inference";
141+
import {
142+
HfInferenceError,
143+
HfInferenceProviderApiError,
144+
HfInferenceProviderOutputError,
145+
HfInferenceHubApiError,
146+
} from "@huggingface/inference";
147+
148+
const hf = new HfInference();
149+
150+
try {
151+
const result = await hf.textGeneration({
152+
model: "gpt2",
153+
inputs: "Hello, I'm a language model",
154+
});
155+
} catch (error) {
156+
if (error instanceof HfInferenceProviderApiError) {
157+
// Handle API errors (e.g., rate limits, authentication issues)
158+
console.error("Provider API Error:", error.message);
159+
console.error("HTTP Request details:", error.request);
160+
console.error("HTTP Response details:", error.response);
161+
if (error instanceof HfInferenceHubApiError) {
162+
// Handle API errors (e.g., rate limits, authentication issues)
163+
console.error("Hub API Error:", error.message);
164+
console.error("HTTP Request details:", error.request);
165+
console.error("HTTP Response details:", error.response);
166+
} else if (error instanceof HfInferenceProviderOutputError) {
167+
// Handle malformed responses from providers
168+
console.error("Provider Output Error:", error.message);
169+
} else if (error instanceof HfInferenceInputError) {
170+
// Handle invalid input parameters
171+
console.error("Input Error:", error.message);
172+
} else {
173+
// Handle unexpected errors
174+
console.error("Unexpected error:", error);
175+
}
176+
}
177+
178+
/// Catch all errors from @huggingface/inference
179+
try {
180+
const result = await hf.textGeneration({
181+
model: "gpt2",
182+
inputs: "Hello, I'm a language model",
183+
});
184+
} catch (error) {
185+
if (error instanceof HfInferenceError) {
186+
// Handle errors from @huggingface/inference
187+
console.error("Error from InferenceClient:", error);
188+
} else {
189+
// Handle unexpected errors
190+
console.error("Unexpected error:", error);
191+
}
192+
}
193+
```
194+
195+
### Error Details
196+
197+
#### HfInferenceProviderApiError
198+
199+
This error occurs when there are issues with the API request when performing inference at the selected provider.
200+
201+
It has several properties:
202+
- `message`: A descriptive error message
203+
- `request`: Details about the failed request (URL, method, headers)
204+
- `response`: Response details including status code and body
205+
206+
#### HfInferenceHubApiError
207+
208+
This error occurs when there are issues with the API request when requesting the Hugging Face Hub API.
209+
210+
It has several properties:
211+
- `message`: A descriptive error message
212+
- `request`: Details about the failed request (URL, method, headers)
213+
- `response`: Response details including status code and body
214+
215+
216+
#### HfInferenceProviderOutputError
217+
218+
This error occurs when a provider returns a response in an unexpected format.
219+
220+
#### HfInferenceInputError
221+
222+
This error occurs when input parameters are invalid or missing. The error message describes what's wrong with the input.
223+
224+
123225
### Natural Language Processing
124226
125227
#### Text Generation

0 commit comments

Comments
 (0)