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/pages/docs/guides/providers/openai.mdx
+41-7
Original file line number
Diff line number
Diff line change
@@ -152,13 +152,6 @@ export async function POST(req: Request) {
152
152
prompt
153
153
})
154
154
155
-
// Check for errors
156
-
if (!response.ok) {
157
-
returnnewResponse(awaitresponse.text(), {
158
-
status: response.status
159
-
})
160
-
}
161
-
162
155
// Convert the response into a friendly text-stream
163
156
const stream =OpenAIStream(response)
164
157
@@ -214,6 +207,47 @@ export default function Completion() {
214
207
215
208
</Steps>
216
209
210
+
## Guide: Handling Errors
211
+
212
+
The OpenAI's API throws an `OpenAI.APIError` when an error occurs during a request. It is recommended to wrap your API calls in a `try/catch` block to handle these errors. For more information about `OpenAI.APIError`, see [OpenAI SDK Handling Errors](https://github.com/openai/openai-node?tab=readme-ov-file#handling-errors).
returnNextResponse.json({ name, status, headers, message }, { status })
244
+
} else {
245
+
throwerror
246
+
}
247
+
}
248
+
}
249
+
```
250
+
217
251
## Guide: Save to Database After Completion
218
252
219
253
It’s common to want to save the result of a completion to a database after streaming it back to the user. The `OpenAIStream` adapter accepts a couple of optional callbacks that can be used to do this.
0 commit comments