CubeLoop — a Pythonic, async-native agent harness core designed for high performance, readability, and production-grade persistence.
Python agent-frameworkagent-harnessagent-runtimeagentic-aiai-agentsai-frameworkanthropicharnessharness-engineeringharness-frameworklanggraphlanggraph-alternativellmmcpopenaipi-agentpi-agent-corepythonpython-agent
docs(images): rewrite image-generation guide for redesigned surface
xf gong67c8129698f9
diff --git a/website/docs/guides/providers/image-generation.md b/website/docs/guides/providers/image-generation.mdindex 687d1a1..ba785b1 100644--- a/website/docs/guides/providers/image-generation.md+++ b/website/docs/guides/providers/image-generation.md@@ -1,88 +1,245 @@---title: Image Generation-description: "Generate images with CubePi image providers."+description: "Generate images with CubePi image providers — OpenAI, Doubao Seedream, SiliconFlow, Together AI, and other OpenAI-compatible backends."---# Image Generation-This page documents CubePi's separate image generation path. It is intentionally-independent from `Agent`: chat models use the `Model` path, while image models-use `ImagesModel` and run through an image provider instance directly.+CubePi's image-generation path mirrors the chat-provider conventions: a+provider holds the connection (`provider_id`, `api_key`, `base_url`,+`capability`), a model spec holds model-level defaults, and per-call work+goes through a typed `ImagesContext` plus an optional `ImagesOptions`+cross-cutting bag. Failures raise typed `ProviderError` subclasses; UI+hosts catch them like they would any chat error.-## `ImagesModel` types+The single concrete provider class — `OpenAIImagesProvider` — reaches+multiple OpenAI-shape backends through an `ImagesCapabilityDescriptor`+that declares wire differences as data (size field name, count field+name, supports_seed/steps/guidance gates, …).-Image generation uses three small types from `cubepi.providers.images.types`:+> **Async-task backends** (Aliyun Wanxiang, Google Imagen, Stability,+> Replicate, fal, FLUX official) follow a submit→poll→fetch pattern that+> is not modeled by the capability descriptor. Custom subclasses can+> implement them today; first-class async-task scaffolding is a Roadmap+> item — see the [bottom of this page](#roadmap).++## Quickstart — OpenAI```python-from cubepi.providers.images.types import AssistantImages, ImagesContext, ImagesModel+import os+from cubepi.providers.images import OpenAIImagesProvider, ImagesContext++provider = OpenAIImagesProvider(+ provider_id="openai",+ api_key=os.environ["OPENAI_API_KEY"],+)+model = provider.model(+ "gpt-image-1",+ default_size="1024x1024",+ default_quality="high",+)++result = await provider.generate_images(+ model,+ ImagesContext(prompt="A cute robot at sunrise"),+)++# result.stop_reason in {"stop", "aborted"}; failures raise ProviderError.+for block in result.output:+ print(block.type, block.media_type, len(block.source))```-- `ImagesModel`: minimal model binding for image APIs (`id`, `provider`, `api`).-- `ImagesContext`: request body for generation (`prompt`, optional `input_images`).-- `AssistantImages`: provider response with `output`, `stop_reason`, and- optional `error_message`.+## `provider.model("id", ...)` — model factory-`ImageContent` (from `cubepi.providers.base`) is used in `input_images` and-`AssistantImages.output`.+`provider.model(...)` builds an `ImagesModel`. The provider's+`provider_id` is propagated automatically so the model knows where it+came from (used for tracing, error messages, and response metadata).-## Create image models+| Parameter | Type | Effect |+|---|---|---|+| `id` (positional) | `str` | The wire model id (e.g. `"gpt-image-1"`) |+| `api` | `str` | Routing tag (e.g. `"openai-images"`) |+| `default_size` | `str \| None` | Used when `ImagesContext.size` is `None` |+| `default_n` | `int \| None` | Used when `ImagesContext.n` is `None` |+| `default_quality` | `Literal["low","medium","high"] \| None` | Default when context omits it |+| `default_output_format` | `Literal["png","jpeg","webp"] \| None` | Default output format |+| `cost` | `ImagesCost \| None` | Per-image / per-megapixel pricing metadata |+| `max_input_images` | `int \| None` | Edit-path cap; meaningful only when the capability supports edit |-For OpenAI image models, use `create_images_provider("openai-images", ...)` and-an `ImagesModel`:+## `ImagesContext` — per-call payload```python-import os+ctx = ImagesContext(+ prompt="A robot",+ size="1024x1024",+ n=2,+ quality="high",+ output_format="png",+ seed=42, # only written if capability.supports_seed+ negative_prompt="...", # only written if capability.supports_negative_prompt+ steps=20, # only written if capability.supports_steps+ guidance=7.5, # only written if capability.supports_guidance+ extra={"watermark": False}, # always written verbatim+ input_images=[...], # ImageContent list; triggers edit path+)+```-from cubepi.providers.images import create_images_provider-from cubepi.providers.images.types import ImagesContext, ImagesModel+Field merge rule: `ctx.<field>` wins over `model.default_<field>`; if both+are `None`, the field is omitted from the wire payload (the backend uses+its own default). Value semantics — `"1024x1024"` vs `"1K"` vs `"1:1"` —+remain the user's responsibility; the capability descriptor only renames+the wire **key**.-image_provider = create_images_provider(- "openai-images",- api_key=os.environ["OPENAI_API_KEY"],+## `ImagesOptions` — per-call cross-cutting++```python+from cubepi.providers.images import ImagesOptions++opts = ImagesOptions(+ signal=cancel_event, # asyncio.Event; set to abort+ on_payload=lambda p, m: p, # pre-send payload mutator (per-call)+ on_response=lambda r, m: None, # response observer (per-call))-model = ImagesModel(id="gpt-image-1", provider="openai", api="openai-images")```-`api` is an identifier used by downstream systems for routing and reporting.+When `signal` is set mid-call, the SDK request is cancelled and the+provider returns `AssistantImages(stop_reason="aborted", output=[])` —+the `CancelledError` does not escape.++`on_payload` and `on_response` are per-call hooks; for persistent+observers (tracing, audit), use `provider.subscribe_request()` /+`provider.subscribe_response()` — see [Observability](#observability).++## `ImagesCapabilityDescriptor` — reach other OpenAI-shape backends++Different OpenAI-shape backends use different field names. The+descriptor lets one `OpenAIImagesProvider` reach all of them:-## Generate text-to-image+### Volcengine Ark / Doubao Seedream++Mostly OpenAI-compatible with a `watermark` extra and `seed` support:```python-import os+OpenAIImagesProvider(+ provider_id="doubao",+ api_key=os.environ["ARK_API_KEY"],+ base_url="https://ark.cn-beijing.volces.com/api/v3",+ capability=ImagesCapabilityDescriptor(+ supports_seed=True,+ extra_payload={"watermark": False},+ ),+)+```-from cubepi.providers.images import create_images_provider-from cubepi.providers.images.types import ImagesContext, ImagesModel+### SiliconFlow-provider = create_images_provider(- "openai-images",- api_key=os.environ["OPENAI_API_KEY"],+OpenAI-shape URL but field names differ:++```python+from cubepi.providers.images.capability import ImagesCapabilityDescriptor, SizeSpec++OpenAIImagesProvider(+ provider_id="siliconflow",+ api_key=os.environ["SILICONFLOW_API_KEY"],+ base_url="https://api.siliconflow.cn/v1",+ capability=ImagesCapabilityDescriptor(+ size_spec=SizeSpec(kind="image_size_string"),+ count_field="batch_size",+ supports_seed=True,+ supports_steps=True, steps_field="num_inference_steps",+ supports_guidance=True, guidance_field="guidance_scale",+ supports_negative_prompt=True,+ output_format_field=None, # not supported+ ),)-model = ImagesModel(id="gpt-image-1", provider="openai", api="openai-images")+```-resp = await provider.generate_images(- model=model,- context=ImagesContext(prompt="A cute robot at sunrise"),- options={"size": "1024x1024", "quality": "high", "n": 2},+### Together AI — FLUX schnell++FLUX schnell uses `aspect_ratio` instead of `size`:++```python+OpenAIImagesProvider(+ provider_id="together",+ api_key=os.environ["TOGETHER_API_KEY"],+ base_url="https://api.together.xyz/v1",+ capability=ImagesCapabilityDescriptor(+ size_spec=SizeSpec(kind="aspect_ratio"),+ supports_seed=True,+ supports_steps=True, steps_field="steps",+ ),)+```++### Mixed-model gateways++When one gateway serves multiple models with different shapes, use+`model_capability_overrides`:++```python+provider = OpenAIImagesProvider(+ provider_id="together",+ api_key="...",+ base_url="https://api.together.xyz/v1",+ capability=together_pro_cap, # default+ model_capability_overrides={+ "black-forest-labs/FLUX.1-schnell": together_schnell_cap,+ },+)+```++Resolution is exact-match on `model.id`; unmatched models fall back to+the base `capability`.++## Error handling++All built-in image providers raise typed `cubepi.errors.ProviderError`+subclasses on failure — never in-band error strings:++```python+from cubepi.errors import RateLimited, ProviderAuthFailed, ProviderUnavailable++try:+ result = await provider.generate_images(model, ctx)+except RateLimited as exc:+ # exc.retry_after may be populated+ ...+except ProviderAuthFailed:+ ...+except ProviderUnavailable:+ # 5xx, timeout, network — typically retryable+ ...+```++`AssistantImages.stop_reason` is now only `"stop"` (success) or+`"aborted"` (signal-triggered cancel). There is no `"error"` value and+no `error_message` field.-if resp.stop_reason == "error":- raise RuntimeError(resp.error_message or "image generation failed")+## Observability-for block in resp.output:- print(block.type, block.source[:30], block.media_type)+Persistent observers register on the provider:++```python+provider.subscribe_request(lambda payload, model: log_payload(payload))+provider.subscribe_response(lambda body, model, exc: log_response(body, exc))```-If `options` is omitted, the provider sends defaults (`n=1`) and no optional-image-only params.+- `subscribe_request` fires once per call, just before the SDK send, with+ the final assembled payload dict (after `on_payload` mutators).+- `subscribe_response` fires once per call in the provider's `finally`+ block, with the assembled response body (or `None` on failure) and the+ exception (or `None` on success).-## Edit with input images+There is **no** `subscribe_chunk` — image generation is one-shot.-Passing `input_images` switches to the edit path automatically:+## Edit path++Passing `input_images` triggers the edit path when the capability+supports it:```pythonimport base64from cubepi.providers.base import ImageContent-from cubepi.providers.images.types import ImagesContextwith open("source.png", "rb") as fh:source_b64 = base64.b64encode(fh.read()).decode("ascii")@@ -91,51 +248,52 @@ ctx = ImagesContext(prompt="Make it brighter and warmer.",input_images=[ImageContent(source=source_b64, media_type="image/png")],)+result = await provider.generate_images(model, ctx)```-`generate_images(model, context=ctx)` calls `edit` instead of `generate`.--## `options` forwarding+Setting `capability=ImagesCapabilityDescriptor(supports_edit=False)`+falls back to the generate path even when `input_images` is non-empty —+useful when targeting a backend whose model can't edit.-`generate_images(..., options)` is a free-form dict passed straight into the-backend call (after adding the required `model`, `prompt`, and default `n`).-The OpenAI image provider currently supports common keys like:--- `size`-- `quality`-- `n`-- `output_format` (`png`, `jpeg`, `webp`)-- `background`-- any backend extension fields accepted by your gateway--The provider only filters output media type for `output_format`:-`png` -> `image/png`, `jpeg` -> `image/jpeg`, `webp` -> `image/webp`.--## Error handling+## Faux provider for tests-On backend failures, `generate_images(...)` returns `AssistantImages` with:--- `stop_reason="error"`-- `output=[]`-- `error_message=<provider error>`--This keeps the API failure-in-band and lets callers render a single-success/error branch.+```python+from cubepi.providers.images import FauxImagesProvider+from cubepi.errors import RateLimited-## Testing and stubs+# Happy path:+provider = FauxImagesProvider(png_b64="iVBORw0KGgo...")-For local tests, `FauxImagesProvider` provides a deterministic image output:+# Inject an error to exercise retry middleware:+provider = FauxImagesProvider(+ png_b64="iVBORw0KGgo...",+ raise_on_call=RateLimited,+)+```-```python-from cubepi.providers.images.faux import FauxImagesProvider+`FauxImagesProvider` inherits the listener registry, `.model()` factory,+and `provider_id` propagation from `BaseImagesProvider`, so tests that+exercise observability against the image path can use it interchangeably+with `OpenAIImagesProvider`.-provider = FauxImagesProvider(png_b64="iVBORw0KGgoAAAANSUh...")-```+## Roadmap-It is useful for end-to-end UI tests where you only need a stable `ImageContent`.+- **Async-task backends.** Aliyun Wanxiang, Google Imagen on Vertex,+ Stability, Replicate, fal, FLUX official all follow a submit→poll→fetch+ pattern that this version does not model first-class. Subclasses of+ `BaseImagesProvider` can implement them by hand today; a future release+ will likely add an `AsyncTaskImagesProvider` base with shared polling+ scaffolding.+- **Tracing wiring.** This release adds the listener registry on image+ providers, but `cubepi.tracing` does not yet auto-subscribe to image+ calls. Hosts that want image-call spans should subscribe manually for+ now.## See also-- [Providers Overview](./overview) — when model-level wiring differs from defaults.-- [OpenAI Provider](./openai) — shared OpenAI config patterns.-- [API Reference → Image types in providers](../../api/cubepi-providers).+- [Providers Overview](./overview) — chat-provider configuration; image+ providers follow the same `provider_id` / `.model()` / capability+ conventions.+- [OpenAI Provider](./openai) — shared OpenAI-shape patterns on the chat+ side.+- [API Reference → `cubepi.providers.images`](../../api/cubepi-providers).