Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

APIError is suggested to be returned from stream.Recv() #302

Closed
hophacker opened this issue May 5, 2023 · 3 comments
Closed

APIError is suggested to be returned from stream.Recv() #302

hophacker opened this issue May 5, 2023 · 3 comments
Labels
enhancement New feature or request

Comments

@hophacker
Copy link

hophacker commented May 5, 2023

In the example below, it is very helpful to get err type other than err message at some cases. For example, when your quota exceeded, the raw response is

{
    "error": {
        "message": "You exceeded your current quota, please check your plan and billing details.",
        "type": "insufficient_quota",
        "param": null,
        "code": null
    }
}

This error will be returned by stream.Recv() and the error message is generated as error, You exceeded your current quota, please check your plan and billing details.". Error message is changeable due to many reasons, thus it is imprecise to determine that the account has no quota through it. Instead, error type is needed here since it is supposed to be hard to change.

So, an error object to return is needed here, a.k.a, APIError is suggested to be returned from stream.Recv()

		stream, err := tunnel.OpenaiClient.CreateChatCompletionStream(cc, req)
		if err != nil {
			return err
		}
		defer stream.Close()

		for {
			response, err := stream.Recv()
			if errors.Is(err, io.EOF) {
				fmt.Println("\nStream finished")
				return nil
			}

			if err != nil {
				return err
			}
			w.Write([]byte(response.Choices[0].Delta.Content))
			w.(http.Flusher).Flush()
		}
	}
@hophacker
Copy link
Author

never mind, I found a way to to that

func getErrType(err error) string {
	unwrappedError := errors.Unwrap(err)
	if unwrappedError != nil {
		if apiErr, ok := unwrappedError.(*openai.APIError); ok {
			return apiErr.Type
		}
	}
	return ""
}

@dannysievers
Copy link

This can be solved by using errors.As() as seen in the example README. Check out PR comments #293 for implementation details.

e := &openai.APIError{}
if errors.As(err, &e) {
  switch e.HTTPStatusCode {
    case 401:
      // invalid auth or key (do not retry)
    case 429:
      // rate limiting or engine overload (wait and retry) 
    case 500:
      // openai server error (retry)
    default:
      // unhandled
  }
}

@vvatanabe vvatanabe added the enhancement New feature or request label Jun 30, 2023
@vvatanabe
Copy link
Collaborator

I'm closing this because it appears to have already been resolved.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

3 participants