Skip to content

Conversation

@lzell
Copy link
Owner

@lzell lzell commented Nov 5, 2025

OpenAI expects a multipart upload with part image[] for gpt-image-1 and gpt-image-1-mini. However, they expect part image for dall-e-2. This patch makes that special case adjustment.

Working example to edit a png with dall-e-2 (note that OpenAI requires an alpha channel on your png):

    import AIProxy

    /* Uncomment for BYOK use cases */
    // let openAIService = AIProxy.openAIDirectService(
    //     unprotectedAPIKey: "your-openai-key"
    // )

    /* Uncomment for all other production use cases */
    // let openAIService = AIProxy.openAIService(
    //     partialKey: "partial-key-from-your-developer-dashboard",
    //     serviceURL: "service-url-from-your-developer-dashboard"
    // )

    guard let image = UIImage(named: "my-image") else {
        print("Could not find an image named 'my-image' in your app assets")
        return nil
    }

    guard let pngData = pngDataWithAlpha(image) else {
        print("Could not encode image as png")
        return nil
    }

    let requestBody = OpenAICreateImageEditRequestBody(
        images: [.png(pngData)],
        prompt: "Change the boat mast to red",
        model: .dallE2,
        responseFormat: .b64JSON
    )

    do {
        let response = try await openAIService.createImageEditRequest(
            body: requestBody,
            secondsToWait: 300
        )
        guard let base64Data = response.data.first?.b64JSON,
              let imageData = Data(base64Encoded: base64Data),
              let editedImage = UIImage(data: imageData) else {
            print("Could not create a UIImage out of the base64 returned by OpenAI")
            return nil
        }
        // Do something with 'editedImage'
    } catch AIProxyError.unsuccessfulRequest(let statusCode, let responseBody) {
        print("Received non-200 status code: \(statusCode) with response body: \(responseBody)")
    } catch {
        print("Could not create OpenAI edit image generation: \(error.localizedDescription)")
    }

@lzell
Copy link
Owner Author

lzell commented Nov 5, 2025

Closes: #211

@lzell lzell merged commit 971c337 into main Nov 5, 2025
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.

2 participants