Images API doesn't automatically detect content type
Nobody has claimed this yet.
- Dominant language
- Kotlin
- Stars
- 1.5k
- Forks
- 264
- Avg merge
- 9h 46m
- Merged PRs (30d)
- 96
Description
TLDR: HttpRequestBodies.kt sets inappropriate ContentType which ends in 400 response when working with ImageEditParams object
While working on https://github.com/openai/openai-java/issues/453 I've noticed a problem with uploading files to /images/edits endpoint
Problem:
Any attempt to upload an image with client.images().edit ends in 400: Invalid type for 'image': expected a file, but got a string instead. response
Stacktrace:
Exception in thread "main" com.openai.errors.BadRequestException: 400: Invalid type for 'image': expected a file, but got a string instead.
at com.openai.errors.BadRequestException$Builder.build(BadRequestException.kt:88)
at com.openai.core.handlers.ErrorHandler$withErrorHandler$1.handle(ErrorHandler.kt:48)
at com.openai.services.blocking.ImageServiceImpl$WithRawResponseImpl$edit$1.invoke(ImageServiceImpl.kt:109)
at com.openai.services.blocking.ImageServiceImpl$WithRawResponseImpl$edit$1.invoke(ImageServiceImpl.kt:107)
at com.openai.core.http.HttpResponseForKt$parseable$1$parsed$2.invoke(HttpResponseFor.kt:14)
at kotlin.SynchronizedLazyImpl.getValue(LazyJVM.kt:74)
at com.openai.core.http.HttpResponseForKt$parseable$1.getParsed(HttpResponseFor.kt:14)
at com.openai.core.http.HttpResponseForKt$parseable$1.parse(HttpResponseFor.kt:16)
at com.openai.services.blocking.ImageServiceImpl.edit(ImageServiceImpl.kt:42)
at com.openai.services.blocking.ImageService.edit(ImageService.kt:34)
at com.openai.example.ImageEditExample.main(ImageEditExample.java:44)
Reason:
Multipart form data Content-Type set to text/plain instead of image/jpeg which end API doesn't accept
ImageServiceImpl.kt L106 brekapoint values:
Reproducible example: https://github.com/nuclear-bean/openai-java/blob/invalid-content-type-example/openai-java-example/src/main/java/com/openai/example/ImageEditExample.java
I've also prepared a (VERY dirty) fix - let's call it a demonstration on what what solves the issue. You can find it here - https://github.com/nuclear-bean/openai-java/commit/91f4f29231903a76f8147523cb440a123327478e - this branch runs and completes the task with no problems.
However, I don't have the time now to dive deeper into the SDK strucutre and find an appropriate approach. My guess is that ImageEditParams object should accept a filename param and derive Content-Type from it (?). If you could direct me towards an accaptable solution I can work from there or I will get back to it later
There's a workaround mentioned here https://github.com/openai/openai-java/issues/452#issue-3018731017. If this is the only intended way of setting the Image maybe fun image(image: Image) should be removed from ImageEditParams.Builder since using it always ends in 400 error?
Contributor guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
Research direction
Start with HttpRequestBodies.kt and the multipart values observed around ImageServiceImpl.kt line 106, then compare the reproducible ImageEditExample.java and the linked demonstration fix. Check the ImageEditParams image path and the workaround in issue 452. Done means client.images().edit accepts an image upload without the 400 response and sends an appropriate image content type.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- java, kotlin
- Domain
- api
- Issue type
- Bug
- Difficulty
- 4/5
- Estimated time
- 3-5 days
- Activity status
- Stale
- Clarity
- Mostly clear
- Newbie friendliness
- 35/100