Skip to content

Improve error handling in Invoke-OAIBeta to display OpenAI API error responses#97

Merged
dfinke merged 2 commits intodfinke:mainfrom
whiteken:CatchForNoBilling
May 14, 2025
Merged

Improve error handling in Invoke-OAIBeta to display OpenAI API error responses#97
dfinke merged 2 commits intodfinke:mainfrom
whiteken:CatchForNoBilling

Conversation

@whiteken
Copy link

@whiteken whiteken commented May 13, 2025

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:

Invoke-RestMethod:                                                                                                      
{
  "error": {
    "message": "You exceeded your current quota, please check your plan and billing details. For more information on this error, read the docs: https://platform.openai.com/docs/guides/error-codes/api-errors.",
    "type": "insufficient_quota",
    "param": null,
    "code": "insufficient_quota"
  }
}

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.

…Invoke-RestMethod. Now reads the HTTP response stream when ErrorDetails is null, enabling display of OpenAI error messages like 'insufficient_quota' for billing.
@dfinke dfinke self-assigned this May 14, 2025
@dfinke dfinke added the bug Something isn't working label May 14, 2025
@dfinke dfinke requested a review from Copilot May 14, 2025 16:24
Copy link

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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

@dfinke
Copy link
Owner

dfinke commented May 14, 2025

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.

@dfinke dfinke merged commit 309d6a3 into dfinke:main May 14, 2025
3 checks passed
@dfinke
Copy link
Owner

dfinke commented May 14, 2025

Thanks again @whiteken!

Published.

@whiteken whiteken deleted the CatchForNoBilling branch May 20, 2025 10:40
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

bug Something isn't working

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants