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

Not as expected err: unexpected end of JSON input #280

Closed
zjy282 opened this issue Apr 25, 2023 · 1 comment
Closed

Not as expected err: unexpected end of JSON input #280

zjy282 opened this issue Apr 25, 2023 · 1 comment
Labels
bug Something isn't working

Comments

@zjy282
Copy link
Contributor

zjy282 commented Apr 25, 2023

When OpenAI or Azure returns an undefined error, since the JSON parsing meets the expectations of ErrorResponse, the "error, unexpected end of JSON input" error will be returned
client.go : line 134
func:handleErrorResp

err := json.NewDecoder(resp.Body).Decode(&errRes) 

The real error message cannot be captured , but errRes.Error can get the real error message.
Example Azure origin response body:

{
    "error":{
        "code":"AccessDenied",
        "message":"Access denied due to Virtual Network/Firewall rules."
    }
}

Calling “response, err := client.CreateChatCompletion()” gets “error, unexpected end of JSON input”,but unable to get correct error:Access denied due to Virtual Network/Firewall rules.

I think the correct error message can be obtained by modifying it in the following way:

func (c *Client) handleErrorResp(resp *http.Response) error {
	var errRes ErrorResponse
	err := json.NewDecoder(resp.Body).Decode(&errRes)
	if err != nil || errRes.Error == nil {
		reqErr := RequestError{
			HTTPStatusCode: resp.StatusCode,
			Err:            err,
		}
		if errRes.Error != nil{
			reqErr.Err = errRes.Error
		}
		return fmt.Errorf("error, %w", &reqErr)
	}
	errRes.Error.HTTPStatusCode = resp.StatusCode
	return fmt.Errorf("error, status code: %d, message: %w", resp.StatusCode, errRes.Error)
}

Go Version:1.19.7
go-openai Version:1.9.0

sashabaranov pushed a commit that referenced this issue Apr 30, 2023
* Fix OpenAI or Azure returns an undefined error message return

* Fix: OpenAI or Azure returns an undefined error message return
#280

* Fix: OpenAI or Azure returns an undefined error message return
#280

* Fix: OpenAI or Azure returns an undefined error message return
#280

Handle long line

* Fix: OpenAI or Azure returns an undefined error message return
#280

Add unit test ErrorResponse nil pointer check

* Fix: OpenAI or Azure returns an undefined error message return
#280

Add unit test ErrorResponse nil pointer check

---------

Co-authored-by: zhangxm <xuanming_zhang@qingsongchou.com>
@vvatanabe vvatanabe added the bug Something isn't working label Jun 30, 2023
@vvatanabe
Copy link
Collaborator

Fixed #283

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

No branches or pull requests

2 participants