fix(image): use standalone Codex Images API (#6177)

* fix(image): use standalone Codex Images API

* docs(image): keep Codex setup focused on user actions
This commit is contained in:
chengyongru authored and GitHub committed 2026-10-12 17:05:38 +08:00
1 parent 73af08efab
commit bdfc8f7bd4
4 files changed
+126 -383

No files matched your search

+20
View File
@@ -96,6 +96,26 @@ OpenRouter uses a chat-completions style image response. Configure:
Use a model that supports image generation and image editing if you want reference-image edits.
### OpenAI Codex
Log in with `nanobot provider login openai-codex`, then select **OpenAI Codex** and `gpt-image-2` under **Settings → Image**. Image generation uses your Codex subscription.
For manual configuration:
```json
{
"tools": {
"imageGeneration": {
"enabled": true,
"provider": "openai_codex",
"model": "gpt-image-2"
}
}
}
```
Use Codex to generate images from text or edit existing images with reference images.
### Custom (OpenAI-compatible)
The `custom` image provider fits services that implement the synchronous OpenAI Images API:
+30 -124
View File
@@ -1371,13 +1371,13 @@ class CustomImageGenerationClient(ImageGenerationProvider):
class CodexImageGenerationClient(ImageGenerationProvider):
"""OpenAI image generation via Codex subscription OAuth.
Uses the Codex Responses API with the ``image_generation`` tool
(the same mechanism ChatGPT uses internally). No API key required —
the Codex OAuth token from ``oauth_cli_kit`` is used instead.
Uses the standalone Codex Images API, matching the official Codex
image-generation extension. The Codex OAuth token from ``oauth_cli_kit``
authenticates requests without an API key.
"""
provider_name = "openai_codex"
model_options = ("gpt-5.4",)
model_options = ("gpt-image-2",)
missing_key_message = (
"Codex OAuth token is unavailable. "
"Log in with Codex subscription first."
@@ -1417,63 +1417,56 @@ class CodexImageGenerationClient(ImageGenerationProvider):
if not token or not token.access:
raise ImageGenerationError(self.missing_key_message)
logger.info(
"Using Codex OAuth token for image generation (account: {})",
token.account_id,
)
if reference_images:
logger.warning(
"Codex image generation does not support reference images; "
"ignoring {} reference image(s)",
len(reference_images),
)
headers: dict[str, str] = {
"Authorization": f"Bearer {token.access}",
"chatgpt-account-id": str(token.account_id),
"OpenAI-Beta": "responses=experimental",
"originator": "nanobot",
"User-Agent": "nanobot (python)",
"Content-Type": "application/json",
**self.extra_headers,
}
clean_model = self._codex_model(model)
body: dict[str, Any] = {
"model": self._codex_model(model),
"instructions": "Generate an image based on the user's request.",
"input": [{"role": "user", "content": prompt}],
"tools": [{"type": "image_generation"}],
"tool_choice": "auto",
"stream": True,
"store": False,
"model": clean_model,
"prompt": prompt,
"background": "opaque",
"quality": "auto",
"size": (
_openai_size(clean_model, aspect_ratio, image_size)
if aspect_ratio or image_size else "auto"
),
}
body.update(self.extra_body)
body = {key: value for key, value in body.items() if value is not None}
endpoint = "generations"
if reference_images:
endpoint = "edits"
body["images"] = [
{"image_url": image_path_to_data_url(path)} for path in reference_images
]
logger.info("Codex Responses API request: POST {}/codex/responses body={}",
self.api_base, {k: v for k, v in body.items() if k != "input"})
base = self.api_base
if not base.endswith("/codex"):
base = f"{base}/codex"
response = await self._http_post(
f"{self.api_base}/codex/responses",
f"{base}/images/{endpoint}",
headers=headers,
body=body,
)
try:
response.raise_for_status()
except httpx.HTTPStatusError as exc:
detail = response.text[:1000]
logger.error("Codex Responses API error ({}): {}", response.status_code, detail)
raise ImageGenerationError(
f"Codex image generation failed (HTTP {response.status_code}): {detail}"
) from exc
images, content_text = await _parse_codex_sse_images(response)
raw = {"status": "completed"}
self._require_images(images, raw)
return GeneratedImageResponse(images=images, content=content_text, raw=raw)
payload = _as_json_object(response.json())
if payload is None:
raise ImageGenerationError("Codex image generation returned an invalid JSON response")
images = await _openai_images_from_payload(payload, proxy=self.proxy)
self._require_images(images, payload)
return GeneratedImageResponse(images=images, content="", raw=payload)
def _openai_size(
@@ -1572,93 +1565,6 @@ async def _openai_images_from_payload(
return images
async def _parse_codex_sse_images(
response: httpx.Response,
) -> tuple[list[str], str]:
"""Parse a Codex Responses API SSE stream for image generation output.
Returns ``(images, content_text)``.
"""
import json as _json
images: list[str] = []
text_parts: list[str] = []
buffer: list[str] = []
async for line_bytes in response.aiter_lines():
line = line_bytes.strip()
if line == "":
if buffer:
data_lines: list[str] = []
for bl in buffer:
if bl.startswith("data:"):
data_lines.append(bl[5:].strip())
buffer.clear()
if data_lines:
raw = "".join(data_lines)
if raw == "[DONE]":
break
try:
event = _as_json_object(_json.loads(raw))
except Exception:
continue
if event is None:
continue
ev_type = event.get("type", "")
if ev_type in ("error", "response.failed"):
logger.error("Codex SSE failure: {}", raw[:2000])
_collect_images_from_sse_event(event, images)
_collect_text_from_sse_event(event, text_parts)
if ev_type == "response.completed":
break
continue
buffer.append(line)
# flush remaining
if buffer:
data_lines = [bl[5:].strip() for bl in buffer if bl.startswith("data:")]
raw = "".join(data_lines)
if raw and raw != "[DONE]":
try:
event = _as_json_object(_json.loads(raw))
except Exception:
pass
else:
if event is not None:
_collect_images_from_sse_event(event, images)
_collect_text_from_sse_event(event, text_parts)
return images, "".join(text_parts).strip()
def _collect_images_from_sse_event(event: dict[str, Any], images: list[str]) -> None:
if event.get("type") != "response.output_item.done":
return
item = _as_json_object(event.get("item")) or {}
if item.get("type") != "image_generation_call":
return
result = item.get("result")
if isinstance(result, str):
if result.startswith("data:image/"):
images.append(result)
else:
images.append(_b64_image_data_url(result))
elif (result_object := _as_json_object(result)) is not None:
image_url = result_object.get("image_url") or result_object.get("image") or ""
if isinstance(image_url, str):
if image_url.startswith("data:image/"):
images.append(image_url)
else:
images.append(_b64_image_data_url(image_url))
def _collect_text_from_sse_event(event: dict[str, Any], text_parts: list[str]) -> None:
if event.get("type") == "response.output_text.delta":
delta = event.get("delta")
if isinstance(delta, str) and delta:
text_parts.append(delta)
# ---------------------------------------------------------------------------
# StepFun (阶跃星辰) image generation
# ---------------------------------------------------------------------------
+75 -258
View File
@@ -42,14 +42,12 @@ class FakeResponse:
payload: dict[str, Any],
status_code: int = 200,
content: bytes = b"",
sse_lines: list[str] | None = None,
) -> None:
self._payload = payload
self.status_code = status_code
self.text = str(payload)
self.content = content
self.request = httpx.Request("POST", "https://openrouter.ai/api/v1/chat/completions")
self._sse_lines = sse_lines
def json(self) -> dict[str, Any]:
return self._payload
@@ -59,15 +57,6 @@ class FakeResponse:
response = httpx.Response(self.status_code, request=self.request, text=self.text)
raise httpx.HTTPStatusError("failed", request=self.request, response=response)
async def aiter_lines(self):
if self._sse_lines is not None:
for line in self._sse_lines:
yield line
return
# Fallback: treat response text as SSE lines
for line in self.text.split("\n"):
yield line
class FakeClient:
def __init__(self, response: FakeResponse) -> None:
@@ -85,23 +74,6 @@ class FakeClient:
return self.get_response
class CodexStreamingCompleteThenErrorResponse(FakeResponse):
async def aiter_lines(self):
yield 'data: {"type":"response.output_item.added","item":{"id":"ig_1","type":"image_generation_call","status":"in_progress"}}'
yield ""
yield (
f'data: {{"type":"response.output_item.done","item":{{"id":"ig_1",'
f'"type":"image_generation_call","result":"{PNG_DATA_URL}","status":"completed"}}}}'
)
yield ""
yield 'data: {"type":"response.completed","response":{"status":"completed"}}'
yield ""
raise httpx.RemoteProtocolError(
"peer closed connection without sending complete message body "
"(incomplete chunked read)"
)
@pytest.fixture(autouse=True)
def generated_image_downloads(monkeypatch) -> list[tuple[str, str | None]]:
"""Keep provider response parsing tests independent from outbound HTTP."""
@@ -1238,82 +1210,61 @@ async def test_custom_generate_http_error() -> None:
# ---------------------------------------------------------------------------
@pytest.mark.asyncio
async def test_codex_payload_and_response(monkeypatch) -> None:
@pytest.fixture
def codex_oauth(monkeypatch):
import sys
from dataclasses import dataclass
from types import SimpleNamespace
@dataclass
class FakeToken:
account_id: str = "acct-123"
access: str = "oauth-token"
calls: list[dict[str, Any]] = []
async def fake_to_thread(fn, *args, **kwargs):
return fn(*args, **kwargs)
def get_token(**kwargs):
calls.append(kwargs)
return SimpleNamespace(account_id="acct-123", access="oauth-token")
monkeypatch.setattr("asyncio.to_thread", fake_to_thread)
fake_oauth = SimpleNamespace(get_token=lambda: FakeToken())
monkeypatch.setitem(sys.modules, "oauth_cli_kit", fake_oauth)
monkeypatch.setitem(sys.modules, "oauth_cli_kit", SimpleNamespace(get_token=get_token))
return calls
sse_lines = [
'data: {"type":"response.output_item.added","item":{"id":"ig_1","type":"image_generation_call","status":"in_progress"}}',
"",
f'data: {{"type":"response.output_item.done","item":{{"id":"ig_1","type":"image_generation_call","result":"{PNG_DATA_URL}","status":"completed"}}}}',
"",
'data: [DONE]',
"",
]
fake = FakeClient(FakeResponse({}, sse_lines=sse_lines))
client = CodexImageGenerationClient(
api_key=None,
api_base="https://chatgpt.com/backend-api",
extra_headers={"X-Test": "1"},
client=fake, # type: ignore[arg-type]
)
response = await client.generate(
prompt="draw a cat",
model="gpt-5.4",
)
@pytest.mark.asyncio
@pytest.mark.parametrize("base", [
"https://chatgpt.com/backend-api",
"https://chatgpt.com/backend-api/codex/",
])
async def test_codex_payload_and_response(codex_oauth, base: str) -> None:
import json
requests: list[httpx.Request] = []
payload = {"created": 1778832973, "data": [{"b64_json": PNG_DATA_URL.split(",")[1]}]}
def handle(request: httpx.Request) -> httpx.Response:
requests.append(request)
return httpx.Response(200, json=payload)
async with httpx.AsyncClient(transport=httpx.MockTransport(handle)) as http:
client = CodexImageGenerationClient(
api_key=None, api_base=base, extra_headers={"X-Test": "1"}, client=http,
)
response = await client.generate(prompt="draw a cat", model=client.model_options[0])
assert response.images == [PNG_DATA_URL]
assert response.content == ""
call = fake.calls[0]
assert call["url"] == "https://chatgpt.com/backend-api/codex/responses"
assert call["headers"]["Authorization"] == "Bearer oauth-token"
assert call["headers"]["chatgpt-account-id"] == "acct-123"
assert call["headers"]["OpenAI-Beta"] == "responses=experimental"
assert call["headers"]["X-Test"] == "1"
body = call["json"]
assert body["model"] == "gpt-5.4"
assert body["instructions"] == "Generate an image based on the user's request."
assert body["input"] == [{"role": "user", "content": "draw a cat"}]
assert body["tools"] == [{"type": "image_generation"}]
assert body["tool_choice"] == "auto"
assert body["store"] is False
assert body["stream"] is True
assert response.raw == payload
request = requests[0]
assert str(request.url) == "https://chatgpt.com/backend-api/codex/images/generations"
assert request.headers["Authorization"] == "Bearer oauth-token"
assert request.headers["chatgpt-account-id"] == "acct-123"
assert request.headers["X-Test"] == "1"
assert json.loads(request.content) == {
"model": "gpt-image-2", "prompt": "draw a cat",
"background": "opaque", "quality": "auto", "size": "auto",
}
@pytest.mark.asyncio
async def test_codex_proxy_applies_to_oauth_and_http(monkeypatch) -> None:
import sys
from types import SimpleNamespace
async def test_codex_proxy_applies_to_oauth_and_http(monkeypatch, codex_oauth) -> None:
proxy = "http://127.0.0.1:23458"
captured: dict[str, Any] = {}
async def fake_to_thread(fn, *args, **kwargs):
return fn(*args, **kwargs)
def fake_get_token(*, proxy=None):
captured["token_proxy"] = proxy
return SimpleNamespace(account_id="acct-123", access="oauth-token")
fake_oauth = SimpleNamespace(get_token=fake_get_token)
monkeypatch.setattr("asyncio.to_thread", fake_to_thread)
monkeypatch.setitem(sys.modules, "oauth_cli_kit", fake_oauth)
class FakeAsyncClient:
def __init__(self, **kwargs: Any) -> None:
captured["client_kwargs"] = kwargs
@@ -1326,90 +1277,46 @@ async def test_codex_proxy_applies_to_oauth_and_http(monkeypatch) -> None:
async def post(self, url: str, **kwargs: Any) -> FakeResponse:
captured["request"] = {"url": url, **kwargs}
return FakeResponse(
{},
sse_lines=[
f'data: {{"type":"response.output_item.done","item":{{"type":"image_generation_call","result":"{PNG_DATA_URL}"}}}}',
"",
"data: [DONE]",
"",
],
)
return FakeResponse({"data": [{"b64_json": PNG_DATA_URL.split(",")[1]}]})
monkeypatch.setattr(
"nanobot.providers.image_generation.httpx.AsyncClient",
FakeAsyncClient,
)
monkeypatch.setattr("nanobot.providers.image_generation.httpx.AsyncClient", FakeAsyncClient)
client = CodexImageGenerationClient(api_key=None, proxy=proxy)
response = await client.generate(prompt="draw", model="gpt-5.4")
response = await client.generate(prompt="draw", model="gpt-image-2")
assert response.images == [PNG_DATA_URL]
assert captured["token_proxy"] == proxy
assert codex_oauth == [{"proxy": proxy}]
assert captured["client_kwargs"]["proxy"] == proxy
assert captured["client_kwargs"]["trust_env"] is False
@pytest.mark.asyncio
async def test_codex_stops_reading_after_completed_event(monkeypatch) -> None:
import sys
from dataclasses import dataclass
from types import SimpleNamespace
@dataclass
class FakeToken:
account_id: str = "acct-123"
access: str = "oauth-token"
async def fake_to_thread(fn, *args, **kwargs):
return fn(*args, **kwargs)
monkeypatch.setattr("asyncio.to_thread", fake_to_thread)
fake_oauth = SimpleNamespace(get_token=lambda: FakeToken())
monkeypatch.setitem(sys.modules, "oauth_cli_kit", fake_oauth)
fake = FakeClient(CodexStreamingCompleteThenErrorResponse({}, sse_lines=[]))
@pytest.mark.parametrize("prefix", ["openai-codex", "openai_codex"])
async def test_codex_edit_reference_images_and_options(codex_oauth, tmp_path, prefix) -> None:
reference = tmp_path / "reference.png"
reference.write_bytes(PNG_BYTES)
fake = FakeClient(FakeResponse({"data": [{"b64_json": PNG_DATA_URL.split(",")[1]}]}))
client = CodexImageGenerationClient(
api_key=None, client=fake # type: ignore[arg-type]
api_key=None, extra_body={"quality": "high", "background": None},
client=fake, # type: ignore[arg-type]
)
response = await client.generate(
prompt="add a hat", model=f"{prefix}/gpt-image-2",
reference_images=[str(reference)], aspect_ratio="16:9",
)
response = await client.generate(prompt="draw a cat", model="gpt-5.4")
assert response.images == [PNG_DATA_URL]
assert response.content == ""
assert fake.calls[0]["url"] == "https://chatgpt.com/backend-api/codex/images/edits"
assert fake.calls[0]["json"] == {
"model": "gpt-image-2", "prompt": "add a hat", "quality": "high",
"size": "1536x1024",
"images": [{"image_url": "data:image/png;base64," + base64.b64encode(PNG_BYTES).decode()}],
}
@pytest.mark.asyncio
async def test_codex_strips_model_prefix(monkeypatch) -> None:
import sys
from dataclasses import dataclass
from types import SimpleNamespace
@dataclass
class FakeToken:
account_id: str = "acct-123"
access: str = "oauth-token"
async def fake_to_thread(fn, *args, **kwargs):
return fn(*args, **kwargs)
monkeypatch.setattr("asyncio.to_thread", fake_to_thread)
fake_oauth = SimpleNamespace(get_token=lambda: FakeToken())
monkeypatch.setitem(sys.modules, "oauth_cli_kit", fake_oauth)
fake = FakeClient(FakeResponse({}, sse_lines=[
f'data: {{"type":"response.output_item.done","item":{{"type":"image_generation_call","result":"{PNG_DATA_URL}"}}}}',
"",
'data: [DONE]',
"",
]))
client = CodexImageGenerationClient(
api_key=None, client=fake # type: ignore[arg-type]
)
await client.generate(prompt="draw", model="openai-codex/gpt-5.4")
assert fake.calls[0]["json"]["model"] == "gpt-5.4"
async def test_codex_explicit_size(codex_oauth) -> None:
fake = FakeClient(FakeResponse({"data": [{"b64_json": PNG_DATA_URL.split(",")[1]}]}))
client = CodexImageGenerationClient(api_key=None, client=fake) # type: ignore[arg-type]
await client.generate(prompt="draw", model="gpt-image-2", image_size="2048x1152")
assert fake.calls[0]["json"]["size"] == "2048x1152"
@pytest.mark.asyncio
@@ -1418,115 +1325,25 @@ async def test_codex_requires_oauth(monkeypatch) -> None:
raise RuntimeError("no token")
monkeypatch.setattr("asyncio.to_thread", fake_to_thread)
client = CodexImageGenerationClient(api_key=None)
with pytest.raises(ImageGenerationError, match="OAuth token"):
await client.generate(prompt="draw", model="gpt-5.4")
await client.generate(prompt="draw", model="gpt-image-2")
@pytest.mark.asyncio
async def test_codex_no_images_raises(monkeypatch) -> None:
import sys
from dataclasses import dataclass
from types import SimpleNamespace
@dataclass
class FakeToken:
account_id: str = "acct-123"
access: str = "oauth-token"
async def fake_to_thread(fn, *args, **kwargs):
return fn(*args, **kwargs)
monkeypatch.setattr("asyncio.to_thread", fake_to_thread)
fake_oauth = SimpleNamespace(get_token=lambda: FakeToken())
monkeypatch.setitem(sys.modules, "oauth_cli_kit", fake_oauth)
fake = FakeClient(FakeResponse({}, sse_lines=[
'data: {"type":"response.completed","response":{"status":"completed"}}',
"",
'data: [DONE]',
"",
]))
client = CodexImageGenerationClient(
api_key=None, client=fake # type: ignore[arg-type]
)
async def test_codex_no_images_raises(codex_oauth) -> None:
fake = FakeClient(FakeResponse({"data": []}))
client = CodexImageGenerationClient(api_key=None, client=fake) # type: ignore[arg-type]
with pytest.raises(ImageGenerationError, match="returned no images"):
await client.generate(prompt="draw", model="gpt-5.4")
await client.generate(prompt="draw", model="gpt-image-2")
@pytest.mark.asyncio
async def test_codex_extracts_text_content(monkeypatch) -> None:
import sys
from dataclasses import dataclass
from types import SimpleNamespace
@dataclass
class FakeToken:
account_id: str = "acct-123"
access: str = "oauth-token"
async def fake_to_thread(fn, *args, **kwargs):
return fn(*args, **kwargs)
monkeypatch.setattr("asyncio.to_thread", fake_to_thread)
fake_oauth = SimpleNamespace(get_token=lambda: FakeToken())
monkeypatch.setitem(sys.modules, "oauth_cli_kit", fake_oauth)
fake = FakeClient(FakeResponse({}, sse_lines=[
'data: {"type":"response.output_text.delta","delta":"Here "}',
"",
'data: {"type":"response.output_text.delta","delta":"is your cat image."}',
"",
f'data: {{"type":"response.output_item.done","item":{{"type":"image_generation_call","result":"{PNG_DATA_URL}"}}}}',
"",
'data: [DONE]',
"",
]))
client = CodexImageGenerationClient(
api_key=None, client=fake # type: ignore[arg-type]
)
response = await client.generate(prompt="draw a cat", model="gpt-5.4")
assert response.images == [PNG_DATA_URL]
assert response.content == "Here is your cat image."
@pytest.mark.asyncio
async def test_codex_json_result_format(monkeypatch) -> None:
"""image_generation_call result can be a dict with image_url key."""
import sys
from dataclasses import dataclass
from types import SimpleNamespace
@dataclass
class FakeToken:
account_id: str = "acct-123"
access: str = "oauth-token"
async def fake_to_thread(fn, *args, **kwargs):
return fn(*args, **kwargs)
monkeypatch.setattr("asyncio.to_thread", fake_to_thread)
fake_oauth = SimpleNamespace(get_token=lambda: FakeToken())
monkeypatch.setitem(sys.modules, "oauth_cli_kit", fake_oauth)
fake = FakeClient(FakeResponse({}, sse_lines=[
f'data: {{"type":"response.output_item.done","item":{{"type":"image_generation_call","result":{{"image_url":"{PNG_DATA_URL}"}}}}}}',
"",
'data: [DONE]',
"",
]))
client = CodexImageGenerationClient(
api_key=None, client=fake # type: ignore[arg-type]
)
response = await client.generate(prompt="draw", model="gpt-5.4")
assert response.images == [PNG_DATA_URL]
async def test_codex_http_error(codex_oauth) -> None:
fake = FakeClient(FakeResponse({"error": {"message": "image quota exceeded"}}, status_code=429))
client = CodexImageGenerationClient(api_key=None, client=fake) # type: ignore[arg-type]
with pytest.raises(ImageGenerationError, match="HTTP 429.*image quota exceeded"):
await client.generate(prompt="draw", model="gpt-image-2")
@pytest.mark.asyncio
+1 -1
View File
@@ -140,7 +140,7 @@ def test_image_generation_tool_passes_provider_proxy_to_client(
config=ImageGenerationToolConfig(
enabled=True,
provider="openai_codex",
model="openai-codex/gpt-5.4",
model="openai-codex/gpt-image-2",
),
provider_configs={"openai_codex": ProviderConfig(proxy=proxy)},
)