Improve error handling in Invoke-OAIBeta to display OpenAI API error responses#97
Conversation
…Invoke-RestMethod. Now reads the HTTP response stream when ErrorDetails is null, enabling display of OpenAI error messages like 'insufficient_quota' for billing.
There was a problem hiding this comment.
Pull Request Overview
This PR improves error handling in the Invoke-OAIBeta function to provide clearer feedback when OpenAI API calls fail due to billing quota issues.
- Enhances parsing of error messages from JSON responses.
- Adds a fallback mechanism for reading HTTP error responses for both OpenAI and AzureOpenAI.
- Bumps the module version in PSAI.psd1.
Reviewed Changes
Copilot reviewed 2 out of 2 changed files in this pull request and generated no comments.
| File | Description |
|---|---|
| Private/Invoke-OAIBeta.ps1 | Improved error handling logic to extract and display detailed error messages for OpenAI API failures. |
| PSAI.psd1 | Module version increment to reflect the minor update. |
Comments suppressed due to low confidence (2)
Private/Invoke-OAIBeta.ps1:134
- [nitpick] Consider including details of the caught exception (for example, by appending $_.Exception or similar) in the error message to aid troubleshooting without exposing sensitive data.
catch { $targetError = "Failed to parse OpenAI error JSON: $message" }
Private/Invoke-OAIBeta.ps1:168
- [nitpick] Throwing a plain error message may lose valuable debugging context. Consider wrapping the error message in a custom exception that preserves the original exception data, including the stack trace.
throw $targetError
|
Thank you for the PR @whiteken and thanks for trying out PSAI. Folks have hit this, I know I thought about handling it and must've convinced myself I did. Also, if you haven't already, check out my other PowerShell AI module. Let's you work with 13 providers and their models https://github.com/dfinke/psaisuite Once I shake things out and continue to monitor where the AI space is going, the plan is to blend these two modules together. |
|
Thanks again @whiteken! Published. |
I installed this module today but did not have a billing quota configured at OpenAI. So I had a few failed attempts trying to understand what was failing as prompts were being echoed back rather than an actual reply from OpenAI. Had to drop down to irm to see the actual error in the JSON:
To hopefully help anyone else who (inadvertently!) might end up in the same situation, this PR improves error handling for OpenAI billing quotas. Previously, if Invoke-RestMethod failed, the original prompt was returned since ErrorDetails.Message was empty.
With these changes, the function falls back to reading the raw HTTP response stream and pulling out the error message from the JSON. Now a user will see more helpful output like:
You exceeded your current quota, please check your plan and billing details.
Also bumped the module version due to the minor change.