Skip to content

images().edit() rejects image with 400 “expected file but got string” #452

Closed

Description

@danielfariati

Hi there!

I’m trying to call openAIClient.images().edit(...) in the Java SDK, but I keep getting: 400: Invalid type for 'image': expected one of an array of files or file, but got a string instead.

I’ve tried a few different ways to pass the image, but so far no luck. For example, here’s a minimal repro:

import com.openai.client.OpenAIClient;
import com.openai.client.okhttp.OpenAIOkHttpClient;
import com.openai.models.images.ImageEditParams;
import com.openai.models.images.ImagesResponse;

import java.io.InputStream;
import java.net.URL;

public class ImageEditTest {

    public static void main(String[] args) throws Exception {
        InputStream is = new URL("https://upload.wikimedia.org/wikipedia/en/thumb/7/7d/Lenna_%28test_image%29.png/320px-Lenna_%28test_image%29.png").openStream();

        OpenAIClient client = OpenAIOkHttpClient.fromEnv();
        ImagesResponse response = client.images().edit(
            ImageEditParams.builder()
                .model("gpt-image-1")
                .prompt("Make the image black and white")
                .image(is)
                .build()
        );

        System.out.println(response);
    }
}
  1. Am I using .image(InputStream) incorrectly?
  2. Is there a bug in the SDK?
  3. If I’m doing it wrong, could you share a working Java example?

Thanks in advance for any guidance!

Activity

  1. danielfariati commented on Apr 25, 2025

    @danielfariati
    Author

    Ok, it isn't a bug... If anyone encounter this also, you have to set a filename... So doing something like this works:

    public class Main {
        public static void main(String[] args) throws Exception {
            InputStream is = new URL("https://upload.wikimedia.org/wikipedia/commons/c/c1/Test_wiki_logo_notext.png").openStream();
    
            MultipartField<ImageEditParams.Image> mf = MultipartField.<ImageEditParams.Image>builder()
                .filename("test_image.png")
                .contentType("image/png")
                .value(ImageEditParams.Image.ofInputStream(is))
                .build();
    
            OpenAIClient client = OpenAIOkHttpClient.fromEnv();
    
            ImagesResponse response = client.images().edit(
                ImageEditParams.builder()
                    .model("gpt-image-1")
                    .prompt("Colorize the image")
                    .image(mf)
                    .build()
            );
    
            System.out.println(response);
        }
    }
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions