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

omit empty values with Image API clients #615

Closed

Conversation

kashitaka
Copy link

Issue

HI, I came across this project and I tried to use Image APIs. Then, I found that some optional parameters must be specify to call APIs, or it returns 400 error.

For example, according to the API specification of Create image variation API, the parameters n, response_format, and size are optional, and if those parameters aren't specified, the API applies default values.
However, in go-openai client, if those parameter aren't specified explicitly, it sends zero values and gets 400 response.

if I make a request like this:

	resp, err := client.CreateVariImage(
		context.Background(), openai.ImageVariRequest{
			Image: file,
		})

then, I got a response like this:

error, status code: 400, message: 0 is less than the minimum of 1 - 'n'

because client actually send a request like this:

curl https://api.openai.com/v1/images/variations \
  -H "Authorization: Bearer $OPENAI_API_KEY" \
  -F image="foo.png" \
  -F n=0 \  # <- zero value for int
  -F size="" # <- zero value for string

Although the request structs have fields with json struct tag and seemingly intend to omitempty, actually this omitempty doesn't work because those API accept multipart/form-data so request aren't marshaled to json.

Describe the change

Then, I made image API client work even if optional parameters aren't set in request structs.

Provide OpenAI documentation link

Describe your solution

I just wrote if to check zero values. This change will work same as omitempty. As discussed #9 , it cannot express the difference between zero value and intended zero or empty string though. Another alternative might be using a null structs like NullInt or NullString but it would make breaking changes though.

If my changes would be acceptable, I'd make changes in other image API clients that request with multipart/form-data format and write tests. If it was already argued or you don't need this changes, please close it. Thanks😃

@kashitaka kashitaka closed this Jan 9, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant