Skip to content
This repository was archived by the owner on May 29, 2026. It is now read-only.
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions packages/core/src/chat.ts
Original file line number Diff line number Diff line change
Expand Up @@ -168,6 +168,8 @@ export type CreateImageRequest = {
size?: string
style?: string
outputFormat?: "png" | "jpeg" | "webp"
image?: string | Uint8Array | Buffer // Base image for editing (base64 or binary)
mask?: string | Uint8Array | Buffer // Optional mask for editing (base64 or binary)
}

export interface ImageGenerationUsage {
Expand Down
34 changes: 26 additions & 8 deletions packages/core/src/openai.ts
Original file line number Diff line number Diff line change
Expand Up @@ -787,25 +787,38 @@ export async function OpenAIImageGeneration(
quality,
style,
outputFormat,
image,
mask,
...rest
} = req
const { trace } = options || {}
let url = `${cfg.base}/images/generations`

// Detect edit mode based on presence of image parameter
const isEditMode = !!image
let url = `${cfg.base}/images/${isEditMode ? 'edits' : 'generations'}`

const isDallE = /^dall-e/i.test(model)
const isDallE2 = /^dall-e-2/i.test(model)
const isDallE3 = /^dall-e-3/i.test(model)
const isGpt = /^gpt-image/i.test(model)

const body: any = {
model,
prompt,
size,
quality,
style,
...rest,
}

// Add edit mode parameters if present
if (isEditMode) {
body.image = image
if (mask) body.mask = mask
// Model parameter is typically not used in edit mode for OpenAI

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

is it really the case?
I think we should always set the model

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You're absolutely right! I've fixed this - the model parameter is now always set in the request body for both edit and generation modes. Commit: b8eb307

} else {
body.model = model
}

// auto is the default quality, so always delete it
if (body.quality === "auto" || isDallE2) delete body.quality
if (isDallE3) {
Expand Down Expand Up @@ -846,12 +859,17 @@ export async function OpenAIImageGeneration(
if (cfg.type === "azure") {
const version = cfg.version || AZURE_OPENAI_API_VERSION
trace?.itemValue(`version`, version)
url =
trimTrailingSlash(cfg.base) +
"/" +
body.model +
`/images/generations?api-version=${version}`
delete body.model
if (isEditMode) {
// Azure doesn't use model in the URL for edit mode

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

YES, model is needed in edits just like for generations!

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed! The model is now included in the Azure URL construction for edit mode, just like it is for generations. Commit: b8eb307

url = trimTrailingSlash(cfg.base) + `/images/edits?api-version=${version}`
} else {
url =
trimTrailingSlash(cfg.base) +
"/" +
body.model +
`/images/generations?api-version=${version}`
delete body.model
}
}

const fetch = await createFetch(options)
Expand Down
2 changes: 1 addition & 1 deletion packages/core/src/runpromptcontext.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1184,12 +1184,12 @@ export function createChatGenerationContext(
quality,
style,
outputFormat,
...rest,
}) satisfies CreateImageRequest
const m = measure("img.generate", `${req.model} -> image`)
const res = await imageGenerator(req, configuration, {
trace: imgTrace,
cancellationToken,
...rest,
})
const duration = m()
if (res.error) {
Expand Down
12 changes: 12 additions & 0 deletions packages/core/src/types/prompt_template.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4631,6 +4631,18 @@ interface ImageGenerationOptions extends ImageTransformOptions, RetryOptions {
* For gpt-image-1 only, the type of image format to generate.
*/
outputFormat?: "png" | "jpeg" | "webp"

/**
* Base image for editing (edit mode). When provided, enables image editing instead of generation.
* Can be a base64-encoded string, Buffer, Uint8Array, or file path.
*/
image?: string | Uint8Array | Buffer

/**
* Optional mask for editing. Only used in edit mode when `image` is provided.
* Specifies which parts of the image to edit. Can be a base64-encoded string, Buffer, Uint8Array, or file path.
*/
mask?: string | Uint8Array | Buffer
}

interface TranscriptionOptions extends CacheOptions, RetryOptions {
Expand Down