Skip to content

CubeLoop — a Pythonic, async-native agent harness core designed for high performance, readability, and production-grade persistence.

fix(images): address codex round-2 P2s — URL responses + media_type honesty

xf gong54fd2e17ef5b
diff --git a/cubepi/providers/images/openai_images.py b/cubepi/providers/images/openai_images.py
index a062d5b..e9611b1 100644
--- a/cubepi/providers/images/openai_images.py
+++ b/cubepi/providers/images/openai_images.py
@@ -203,22 +203,35 @@ class OpenAIImagesProvider(BaseImagesProvider):
context: ImagesContext,
cap: ImagesCapabilityDescriptor,
) -> AssistantImages:
- # Resolve output_format the same way _build_payload does, so the
- # returned ImageContent.media_type matches what the user requested.
- # When the capability dropped output_format from the wire payload
- # (output_format_field=None), fall back to png labelling.
- out_format = (
- context.output_format
- if context.output_format is not None
- else model.default_output_format
- ) or "png"
+ # Resolve media_type. Only trust ctx/model defaults when the
+ # capability actually forwarded output_format on the wire; otherwise
+ # the backend chose the format independently and we cannot know it,
+ # so default to png labelling instead of lying about what came back.
+ if cap.output_format_field is not None:
+ out_format = (
+ context.output_format
+ if context.output_format is not None
+ else model.default_output_format
+ ) or "png"
+ else:
+ out_format = "png"
media_type = _OUTPUT_FORMAT_MEDIA_TYPE.get(out_format, "image/png")
+
+ # Build the output list. When the capability asked for url responses
+ # (response_format_value="url"), SDK items expose `url` instead of
+ # `b64_json` — surface those verbatim so they aren't silently lost.
data = getattr(resp, "data", None) or []
- images: list[ImageContent | TextContent] = [
- ImageContent(source=item.b64_json, media_type=media_type)
- for item in data
- if getattr(item, "b64_json", None)
- ]
+ images: list[ImageContent | TextContent] = []
+ for item in data:
+ b64 = getattr(item, "b64_json", None)
+ if b64:
+ images.append(ImageContent(source=b64, media_type=media_type))
+ continue
+ url = getattr(item, "url", None)
+ if url:
+ # ImageContent.source holds the URL string; callers can
+ # detect it via source.startswith(("http://", "https://")).
+ images.append(ImageContent(source=url, media_type=media_type))
if not images:
# Empty response: still a "stop" — but no images. Surface via empty output.
return AssistantImages(
diff --git a/tests/providers/images/test_openai_images.py b/tests/providers/images/test_openai_images.py
index c015573..31893ce 100644
--- a/tests/providers/images/test_openai_images.py
+++ b/tests/providers/images/test_openai_images.py
@@ -469,3 +469,67 @@ async def test_resp_to_dict_uses_model_dump_when_available():
await p.generate_images(model, ImagesContext(prompt="x"))
assert len(seen_bodies) == 1
assert seen_bodies[0] == {"data": [{"b64_json": "DUMPED"}], "id": "resp_123"}
+
+
+async def test_url_response_items_surface_as_imagecontent():
+ """When a capability requests response_format='url', SDK items expose
+ `url` instead of `b64_json`. Parse path must surface those rather than
+ silently drop them (P2 codex finding)."""
+ p = _provider(
+ capability=ImagesCapabilityDescriptor(
+ response_format_field="response_format",
+ response_format_value="url",
+ ),
+ )
+
+ async def _url_resp(**kwargs):
+ return SimpleNamespace(
+ data=[
+ SimpleNamespace(b64_json=None, url="https://cdn.example/img1.png"),
+ SimpleNamespace(b64_json=None, url="https://cdn.example/img2.png"),
+ ]
+ )
+
+ p._client.images.generate = _url_resp
+ model = p.model("gpt-image-1")
+ out = await p.generate_images(model, ImagesContext(prompt="x"))
+ assert out.stop_reason == "stop"
+ assert len(out.output) == 2
+ assert out.output[0].source == "https://cdn.example/img1.png"
+ assert out.output[1].source == "https://cdn.example/img2.png"
+ assert all(o.source.startswith("https://") for o in out.output)
+
+
+async def test_media_type_defaults_to_png_when_output_format_dropped():
+ """When the capability sets output_format_field=None (SiliconFlow shape),
+ the backend chooses its own format. The parse path must NOT trust the
+ user's requested output_format for media_type labelling — it defaults
+ to png instead of mislabelling backend-default bytes (P2 codex finding)."""
+ p = _provider(
+ capability=ImagesCapabilityDescriptor(
+ size_spec=SizeSpec(kind="image_size_string"),
+ count_field="batch_size",
+ output_format_field=None,
+ ),
+ )
+ model = p.model("Kwai-Kolors/Kolors")
+ # User asks for webp but the backend ignores that field and returns its
+ # own default — media_type should NOT lie and claim webp.
+ out = await p.generate_images(
+ model, ImagesContext(prompt="x", output_format="webp")
+ )
+ assert out.output[0].media_type == "image/png"
+
+
+async def test_media_type_still_uses_output_format_when_field_active():
+ """Sanity guard for the dual-condition fix: when output_format_field IS
+ active (default OpenAI shape), the media_type follows the request."""
+ p = _provider() # default descriptor → output_format_field="output_format"
+ model = p.model("gpt-image-1")
+ out = await p.generate_images(
+ model, ImagesContext(prompt="x", output_format="webp")
+ )
+ assert out.output[0].media_type == "image/webp"