Skip to content

feat(codex): add Azure OpenAI image generation support with direct routing#2960

Closed
zilianpn wants to merge 1 commit into
router-for-me:devfrom
zilianpn:feature/azure-image-direct
Closed

feat(codex): add Azure OpenAI image generation support with direct routing#2960
zilianpn wants to merge 1 commit into
router-for-me:devfrom
zilianpn:feature/azure-image-direct

Conversation

@zilianpn
Copy link
Copy Markdown
Contributor

@zilianpn zilianpn commented Apr 22, 2026

Description

为 Azure OpenAI 图片生成模型(如 gpt-image-2)添加直接路由支持。

Problem

网关正常的图片生成路径通过 Responses API 编排:

/v1/responses → gpt-5.4-mini → image_generation tool call → gpt-image-2

这在 OpenAI 上可以正常工作(所有模型共享同一端点),但在 Azure 上会失败。因为 Azure 每个模型是独立的部署,拥有不同的 URL,不支持跨部署的工具路由,导致 tool call 返回 404。

Solution

当检测到图片模型有独立的 upstream 配置(provider 已定义)时,绕过 Responses API 编排路径,直接将请求转发到模型自身的 /images/generations/images/edits 端点。

Changes

SDK 图片处理器 (sdk/api/handlers/openai/openai_images_handlers.go)

  • ImagesGenerationsimagesEditsFromMultipartimagesEditsFromJSON 中增加判断:若模型有 provider 配置,走直接路由
  • 新增 directImageGeneration 方法:通过 AuthManager 执行直接请求,返回标准 OpenAI 格式响应
  • 新增 directImageEditsMultipartdirectImageEditsJSON 方法:处理 edits 场景的 multipart 和 JSON 请求体转换

Codex Executor (internal/runtime/executor/codex_executor.go)

  • 新增 executeImagesGeneration 方法:
    • 构建完整 URL,将 endpoint path(/images/generations/images/edits)插入 baseURL,保留查询参数(如 Azure 的 ?api-version=
    • Azure 部署自动剥离 response_format 参数(Azure 始终返回 base64,不接受此参数)
    • 记录请求/响应日志,返回原始 API 响应给客户端

Auth Conductor (sdk/cliproxy/auth/conductor.go)

  • 修复重试回退逻辑:isRequestInvalidError 中新增 image_generation_user_error 判断,将 400 用户错误标记为不可重试,防止 AuthManager 错误地回退到其他 auth entry

请求详情 (sdk/api/handlers/handlers.go)

  • 放宽 gpt-image-2 的拦截逻辑:仅在没有 provider 配置时返回错误,有独立 upstream 配置时允许通过 Responses API 编排路径

Affected Files

  • sdk/api/handlers/openai/openai_images_handlers.go
  • sdk/api/handlers/handlers.go
  • internal/runtime/executor/codex_executor.go
  • sdk/cliproxy/auth/conductor.go
image

@zilianpn zilianpn force-pushed the feature/azure-image-direct branch from 625d630 to 70f969e Compare April 22, 2026 14:19
Copy link
Copy Markdown
Contributor

@gemini-code-assist gemini-code-assist Bot left a comment

Choose a reason for hiding this comment

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

Code Review

This pull request introduces a direct execution path for image generation and editing models to bypass the standard Responses API orchestration. This change is specifically designed to support providers like Azure OpenAI, where models are deployed to unique URLs that do not support cross-deployment tool routing. The feedback provided focuses on generalizing the implementation to support both generation and editing endpoints dynamically, improving context propagation by using the request context, and ensuring compatibility with non-Azure providers by making the removal of the response_format parameter conditional. Additionally, it is recommended to use consistent error-handling utilities for better status reporting.

Comment thread internal/runtime/executor/codex_executor.go Outdated
Comment thread internal/runtime/executor/codex_executor.go
Comment thread sdk/api/handlers/openai/openai_images_handlers.go Outdated
Comment thread sdk/api/handlers/openai/openai_images_handlers.go Outdated
Comment thread sdk/api/handlers/openai/openai_images_handlers.go Outdated
Comment thread sdk/api/handlers/openai/openai_images_handlers.go Outdated
Comment thread sdk/api/handlers/openai/openai_images_handlers.go Outdated
Comment thread internal/runtime/executor/codex_executor.go Outdated
Comment thread internal/runtime/executor/codex_executor.go Outdated
Copy link
Copy Markdown

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Choose a reason for hiding this comment

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

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: 625d63026c

ℹ️ About Codex in GitHub

Codex has been enabled to automatically review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

When you sign up for Codex through ChatGPT, Codex can also answer questions or update the PR, like "@codex address that feedback".

Comment thread sdk/api/handlers/openai/openai_images_handlers.go Outdated
Comment thread sdk/api/handlers/openai/openai_images_handlers.go
@zilianpn zilianpn force-pushed the feature/azure-image-direct branch from 70f969e to 5e4c2d3 Compare April 22, 2026 14:44
Copy link
Copy Markdown

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Choose a reason for hiding this comment

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

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: 5e4c2d3f93

ℹ️ About Codex in GitHub

Codex has been enabled to automatically review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

When you sign up for Codex through ChatGPT, Codex can also answer questions or update the PR, like "@codex address that feedback".

Comment thread sdk/api/handlers/openai/openai_images_handlers.go
@zilianpn zilianpn force-pushed the feature/azure-image-direct branch 2 times, most recently from e3f074f to c02507b Compare April 22, 2026 14:56
@luispater
Copy link
Copy Markdown
Collaborator

/images/generations/images/edits端点已经在现有main分支中支持。

@luispater luispater closed this Apr 22, 2026
Copy link
Copy Markdown

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Choose a reason for hiding this comment

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

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: c02507bca3

ℹ️ About Codex in GitHub

Codex has been enabled to automatically review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

When you sign up for Codex through ChatGPT, Codex can also answer questions or update the PR, like "@codex address that feedback".

Comment thread sdk/api/handlers/openai/openai_images_handlers.go Outdated
Comment thread sdk/api/handlers/openai/openai_images_handlers.go
@zilianpn zilianpn changed the title feat(codex): support direct /images/generations for independent upstream models feat(codex): add Azure OpenAI image generation support with direct routing Apr 22, 2026
@zilianpn
Copy link
Copy Markdown
Contributor Author

zilianpn commented Apr 22, 2026

@luispater 描述可能不准确,是增加 azure openai gpt-image的支持

@luispater
Copy link
Copy Markdown
Collaborator

OK,我reopen了,回头看一下

@luispater luispater reopened this Apr 22, 2026
@zilianpn zilianpn force-pushed the feature/azure-image-direct branch 3 times, most recently from 8efeb04 to e51eb77 Compare April 22, 2026 15:34
Copy link
Copy Markdown

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Choose a reason for hiding this comment

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

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: 8efeb04681

ℹ️ About Codex in GitHub

Codex has been enabled to automatically review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

When you sign up for Codex through ChatGPT, Codex can also answer questions or update the PR, like "@codex address that feedback".

Comment thread sdk/api/handlers/openai/openai_images_handlers.go
Comment thread internal/runtime/executor/codex_executor.go
@zilianpn zilianpn force-pushed the feature/azure-image-direct branch 2 times, most recently from 8e6e30b to 15319a6 Compare April 22, 2026 17:22
Copy link
Copy Markdown

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Choose a reason for hiding this comment

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

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: 15319a6dcb

ℹ️ About Codex in GitHub

Codex has been enabled to automatically review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

When you sign up for Codex through ChatGPT, Codex can also answer questions or update the PR, like "@codex address that feedback".

Comment thread sdk/api/handlers/openai/openai_images_handlers.go
Comment thread sdk/api/handlers/openai/openai_images_handlers.go
@zilianpn zilianpn force-pushed the feature/azure-image-direct branch from 15319a6 to 8d3c82d Compare April 23, 2026 03:15
Copy link
Copy Markdown

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Choose a reason for hiding this comment

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

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: 8d3c82ddf9

ℹ️ About Codex in GitHub

Codex has been enabled to automatically review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

When you sign up for Codex through ChatGPT, Codex can also answer questions or update the PR, like "@codex address that feedback".

Comment thread sdk/cliproxy/auth/conductor.go
Comment thread sdk/api/handlers/openai/openai_images_handlers.go
@zilianpn zilianpn force-pushed the feature/azure-image-direct branch 2 times, most recently from a766ab4 to 131724a Compare April 23, 2026 03:39
Copy link
Copy Markdown

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Choose a reason for hiding this comment

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

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: 131724a909

ℹ️ About Codex in GitHub

Codex has been enabled to automatically review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

When you sign up for Codex through ChatGPT, Codex can also answer questions or update the PR, like "@codex address that feedback".

Comment thread sdk/cliproxy/service.go
Copy link
Copy Markdown
Collaborator

@luispater luispater left a comment

Choose a reason for hiding this comment

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

Summary

This PR adds a direct execution path for /v1/images/generations and /v1/images/edits to support Azure OpenAI image deployments (e.g. gpt-image-2) where cross-deployment tool routing via the Responses API can fail. It introduces:

  • CodexExecutor alt handling for images/generations + images/edits by constructing {base_url}/{images/*}?api-version=... and forwarding JSON.
  • OpenAI images handlers that bypass the Responses-orchestration path when a codex provider + base_url is configured.
  • Auth conductor tweaks to treat image_generation_user_error (400) as non-retryable and to stop fallback on HTTP 500.
  • Restricting gpt-image-2 builtin injection to auth entries that actually configure image models + base_url.

Blocking

  • stream field handling for direct routing
    • The direct generations path forwards the original JSON body as-is. If clients send stream: true (supported by the gateway’s current orchestration path), that field will be forwarded upstream to /images/generations and may be rejected as an unknown parameter (or change semantics silently).
    • Same risk applies to the JSON edits direct path (directImageEditsJSON copies the original body).
    • Suggested fix: strip stream (and any other gateway-only fields) before calling ExecuteWithAuthManager, and explicitly define behavior when stream=true (reject with a clear error, or ignore and return non-stream response).

Non-blocking

  • Fallback policy change on HTTP 500
    • isServerError now prevents fallback on HTTP 500 across Execute, ExecuteCount, and ExecuteStream. Please confirm this is intended at global scope (and consider whether 502/503/504 should be treated similarly).
  • Azure detection
    • Stripping response_format based on baseURL containing openai/deployments is a pragmatic heuristic, but brittle; consider an explicit provider/type marker or URL parsing.
  • Edits multipart parameter typing
    • partial_images and output_compression are coerced to integers; please confirm these match the upstream contract (otherwise preserve original types).

Test plan (suggested)

  • Add a unit/integration test that exercises the direct route and asserts stream is not sent upstream (and/or that stream=true yields a clear error for direct routing).
  • Manual sanity on an Azure deployment base_url:
    • /v1/images/generations with/without stream
    • /v1/images/edits (multipart and JSON) with optional fields (size/quality/background/output_format/etc.)

…ge models

Route /v1/images/generations to independent upstream model deployments
instead of the Responses API orchestration path. This enables Azure OpenAI
gpt-image-2 (and future gpt-image models) which require direct deployment
URLs and do not support cross-deployment tool routing.

Key changes:
- Add images/generations and images/edits endpoint routing in CodexExecutor
- Direct path gated to codex provider with configured base_url (e.g. Azure deployment)
- Treat image_generation_user_error (400) as non-retryable to prevent
  AuthManager from falling back to the wrong auth entry
- Preserve all caller-supplied edit parameters (size, quality, background, etc.)
@zilianpn zilianpn force-pushed the feature/azure-image-direct branch from 131724a to 060d4ad Compare April 26, 2026 05:52
@zilianpn
Copy link
Copy Markdown
Contributor Author

@luispater

  1. stream 字段处理 (Blocking) — ✅ 已修复

已修复。在 ImagesGenerations handler(line ~240)和 directImageEditsJSON(line ~1036)中,进入直连路径前剥离 stream 字段。直连模式本身就是非流式的(ExecuteWithAuthManager 是非流式调用),该字段不应传递给上游。

  1. 500 不回退策略 (Non-blocking) — ✅ 确认是预期行为

确认这是全局预期行为。500 是上游服务器错误,切换 auth/provider 无法解决。实际遇到的问题:Azure 返回 500 后,fallback 到另一个 base_url 不匹配的 auth,产生了误导性的 400(unknown_parameter: response_format),掩盖了真实的 500。直接返回原始 500 让客户端能准确重试。502/503/504 属于网络/代理层错误,切换 provider 理论上可能有效,后续可根据需要扩展。

  1. Azure 检测 (Non-blocking) — ✅ 已优化

已从 strings.Contains(baseURL, "openai/deployments") 改为 isAzureOpenAIURL() 函数,通过解析 URL host 判断是否以 .openai.azure.com 结尾,比字符串子串匹配更准确。

  1. Edits multipart 参数类型 (Non-blocking) — ✅ 确认正确

output_compression(百分比)和 partial_images(数量)确实是 int 类型,与上游 Azure /images/edits 契约一致。

  1. 测试建议 (Suggested) — 后续迭代

感谢建议,后续会补充单元测试验证 stream 字段剥离和直连路径的端到端流程。

Copy link
Copy Markdown

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Choose a reason for hiding this comment

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

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: 060d4ad90b

ℹ️ About Codex in GitHub

Codex has been enabled to automatically review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

When you sign up for Codex through ChatGPT, Codex can also answer questions or update the PR, like "@codex address that feedback".

break
}
}
if hasCodexProvider && h.AuthManager.HasAuthWithBaseURL("codex") {
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

P1 Badge Gate direct image routing on model-specific base_url auth

The new direct path is enabled whenever HasAuthWithBaseURL("codex") is true, but that check is global and not tied to the auths that can actually serve the requested image model. Fresh evidence: the hasImageModel/base_url filtering added in buildCodexConfigModels only runs for entries with custom models, so default Codex entries can still advertise gpt-image-2 without a base_url; in that mixed setup this branch forces direct routing and executeImagesGeneration returns 401 missing provider baseURL instead of using the prior Responses fallback.

Useful? React with 👍 / 👎.

@zilianpn zilianpn closed this Apr 30, 2026
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