Skip to content

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

Closed
@zjy282

Description

@zjy282

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't working

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions