Skip to content

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

fix(images): record CancelledError on abort + correct media_type per output_format

xf gongb00c5bde399a
diff --git a/cubepi/providers/images/openai_images.py b/cubepi/providers/images/openai_images.py
index 05be8f8..ea0a55e 100644
--- a/cubepi/providers/images/openai_images.py
+++ b/cubepi/providers/images/openai_images.py
@@ -119,10 +119,13 @@ class OpenAIImagesProvider(BaseImagesProvider):
model, # type: ignore[arg-type]
)
- return self._parse_response(sdk_resp, model, cap)
+ return self._parse_response(sdk_resp, model, context, cap)
- except asyncio.CancelledError:
+ except asyncio.CancelledError as cancel:
# Signal-triggered abort: return as AssistantImages, do not re-raise.
+ # The CancelledError is recorded so subscribe_response observers
+ # can distinguish abort from successful completion.
+ exc = cancel
return AssistantImages(
api=model.api,
provider_id=model.provider_id,
@@ -179,16 +182,22 @@ class OpenAIImagesProvider(BaseImagesProvider):
self,
resp: Any,
model: ImagesModel,
+ context: ImagesContext,
cap: ImagesCapabilityDescriptor,
) -> AssistantImages:
- # Determine output media type from output_format if present.
- out_format = "png"
+ # 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"
+ media_type = _OUTPUT_FORMAT_MEDIA_TYPE.get(out_format, "image/png")
data = getattr(resp, "data", None) or []
images: list[ImageContent | TextContent] = [
- ImageContent(
- source=item.b64_json,
- media_type=_OUTPUT_FORMAT_MEDIA_TYPE.get(out_format, "image/png"),
- )
+ ImageContent(source=item.b64_json, media_type=media_type)
for item in data
if getattr(item, "b64_json", None)
]
diff --git a/tests/providers/images/test_openai_images.py b/tests/providers/images/test_openai_images.py
index a7a53e9..fef4629 100644
--- a/tests/providers/images/test_openai_images.py
+++ b/tests/providers/images/test_openai_images.py
@@ -275,3 +275,64 @@ def test_base_url_accepted_and_constructor_works():
base_url="https://ark.cn-beijing.volces.com/api/v3",
)
assert p.provider_id == "doubao"
+
+
+async def test_subscribe_response_fires_on_abort_with_cancelled_error():
+ """Abort path must surface CancelledError to response listeners so tracing
+ can distinguish abort from successful completion (spec §6.5)."""
+ p = _provider(sleep=0.5)
+ model = p.model("gpt-image-1")
+ signal = asyncio.Event()
+ seen: list[tuple[dict | None, BaseException | None]] = []
+ p.subscribe_response(lambda body, m, exc: seen.append((body, exc)))
+
+ async def trigger():
+ await asyncio.sleep(0.05)
+ signal.set()
+
+ async with asyncio.TaskGroup() as tg:
+ tg.create_task(trigger())
+ task = tg.create_task(
+ p.generate_images(
+ model, ImagesContext(prompt="x"), options=ImagesOptions(signal=signal)
+ )
+ )
+
+ result = task.result()
+ assert result.stop_reason == "aborted"
+ assert len(seen) == 1
+ body, exc = seen[0]
+ assert body is None
+ assert isinstance(exc, asyncio.CancelledError)
+
+
+ "output_format,expected_media_type",
+ [
+ ("png", "image/png"),
+ ("jpeg", "image/jpeg"),
+ ("webp", "image/webp"),
+ ],
+)
+async def test_media_type_follows_requested_output_format(
+ output_format, expected_media_type
+):
+ """media_type on returned ImageContent must reflect the requested
+ output_format, not a hardcoded default (final-review fix)."""
+ p = _provider()
+ model = p.model("gpt-image-1")
+ out = await p.generate_images(
+ model, ImagesContext(prompt="x", output_format=output_format)
+ )
+ assert out.output[0].media_type == expected_media_type
+
+
+async def test_media_type_uses_model_default_output_format():
+ """When ctx.output_format is None, fall back to model.default_output_format."""
+ p = _provider()
+ model = p.model("gpt-image-1", default_output_format="webp")
+ out = await p.generate_images(model, ImagesContext(prompt="x"))
+ assert out.output[0].media_type == "image/webp"