Skip to content

The agent that grows with you

refactor(image_gen/openrouter): split generate into chat/image-api phase helpers, compact docs

Tekniumb54d21697e63
diff --git a/plugins/image_gen/openrouter/__init__.py b/plugins/image_gen/openrouter/__init__.py
index 4bff8f218ed..3e82ce9bebd 100644
--- a/plugins/image_gen/openrouter/__init__.py
+++ b/plugins/image_gen/openrouter/__init__.py
@@ -1,42 +1,11 @@
"""OpenRouter-compatible image generation backend (OpenRouter + Nous Portal).
-Both OpenRouter and the Nous Portal inference endpoint speak the same
-OpenAI-style ``/chat/completions`` image-generation protocol: send
-``modalities: ["image", "text"]`` with an image-output model, pass reference
-images as ``image_url`` content parts for grounding, and read generated
-images back from ``choices[0].message.images[].image_url.url`` (a base64 data
-URI). Nous Portal proxies OpenRouter, so one implementation services both —
-only the resolved ``(base_url, api_key)`` differs, obtained through
-:func:`~hermes_cli.runtime_provider.resolve_runtime_provider` (which already
-understands OpenRouter's key pool and the Nous OAuth token).
-
-Reference grounding matters for pet sprite generation: every animation row
-must stay the same character as the base frame, which only works on models
-that accept image input.
-
-Two request surfaces
---------------------
-OpenRouter also ships a separate **Dedicated Image API** at
-``POST /api/v1/images/generations`` with its own catalog
-(``GET /api/v1/images/models``) and the full OpenAI-style parameter set;
-``openai/gpt-image-2``, ``krea/krea-2-*``, ``qwen/qwen-image-3-pro``,
-``microsoft/mai-image-2.5*`` … are reachable *only* there.
-
-Routing is not "is it in the image catalog?" — the two catalogs overlap and
-``/images/models`` lists this backend's own chat-completions defaults. Routing
-on membership alone would silently move every default call off
-``/chat/completions`` and change how references are passed (content parts vs
-``input_references``). So :func:`_select_surface` decides conservatively:
-``image_gen.openrouter.surface`` / ``OPENROUTER_IMAGE_API_SURFACE`` force a
-surface; otherwise curated :data:`_IMAGE_API_MODELS` take the Image API, the
-chat defaults stay pinned to chat-completions, and an id in neither table is
-checked against the cached live catalog. On the Image API path the request
-gains exact per-model aspect ratios plus ``resolution`` / ``quality`` /
-``background`` / ``seed`` / ``n`` / ``output_compression`` and up to 16
-reference images instead of 3.
-
-The Image API is OpenRouter-only: Nous Portal has no ``/images/generations``
-route, so the surface is enabled per provider (``supports_image_api``).
+Both endpoints speak the OpenAI-style ``/chat/completions`` image protocol
+(``modalities: ["image","text"]``, references as ``image_url`` parts, output in
+``choices[0].message.images[].image_url.url``); only ``(base_url, api_key)``
+differs, via :func:`hermes_cli.runtime_provider.resolve_runtime_provider`.
+OpenRouter alone also has the Dedicated Image API (``POST /images/generations``,
+own catalog, exact ratios, up to 16 references) — see :func:`_select_surface`.
"""
from __future__ import annotations
@@ -68,10 +37,9 @@ from plugins.image_gen._common import (
logger = logging.getLogger(__name__)
-# Quality-first default chain: try the highest-fidelity OpenAI image model,
-# then fall back to Gemini 3 Pro Image if it is access-gated / unavailable /
-# times out. Any explicit override (env, scoped config, or ``image_gen.model``
-# from ``hermes tools``) means "use exactly that model", no auto fallback.
+# Quality-first default chain: highest-fidelity OpenAI model, then Gemini 3 Pro
+# Image if it is gated / unavailable / times out. Any explicit override means
+# "use exactly that model" — no auto fallback.
DEFAULT_MODEL = "openai/gpt-5.4-image-2"
_FALLBACK_MODEL = "google/gemini-3-pro-image"
_DEFAULT_MODEL_CHAIN = (DEFAULT_MODEL, _FALLBACK_MODEL)
@@ -79,17 +47,12 @@ _MODEL_PRIORITY = {DEFAULT_MODEL: 0, _FALLBACK_MODEL: 1}
# Semantic aspect ratio → OpenRouter ``image_config.aspect_ratio``.
_ASPECT_RATIOS = {"square": "1:1", "landscape": "16:9", "portrait": "9:16"}
-
# Gemini Flash Image accepts up to 3 input images per prompt.
_MAX_REFERENCE_IMAGES = 3
-
-# Per single image call. The quality-first default is genuinely slow — a cold
-# row can run past 3 minutes — so give each call real headroom before we treat
-# it as hung and fall back / retry.
+# Per image call; a cold quality-first row can run past 3 minutes.
_REQUEST_TIMEOUT = 300.0
-# Curated metadata for well-known chat-completions image models; anything else
-# discovered via the live catalog gets a generic strengths line.
+# Curated metadata for well-known chat-completions image models.
_KNOWN_MODEL_META = {
DEFAULT_MODEL: {
"display": "OpenAI GPT-5.4 Image 2",
@@ -100,141 +63,25 @@ _KNOWN_MODEL_META = {
"strengths": "Fast, reliable fallback with good layout adherence",
},
}
-
# Router pseudo-models advertise image output but are not image models.
_EXCLUDED_MODEL_PREFIXES = ("openrouter/auto",)
-
_LIVE_CACHE_TTL = 300.0
_LIVE_TIMEOUT = 10.0
-
_load_image_gen_config = load_image_gen_config
-
-def _to_image_url_part(ref: str) -> Optional[str]:
- """Turn a reference (local path or http URL) into an ``image_url`` value.
-
- Remote URLs pass through; local files are inlined as base64 data URIs so
- the request is self-contained. ``None`` when the reference can't be read.
- """
- ref = str(ref or "").strip()
- if not ref:
- return None
- if ref.startswith(("http://", "https://", "data:")):
- return ref
- path = Path(ref)
- # Enforce the shared credential-read guard before inlining local bytes.
- from agent.file_safety import raise_if_read_blocked
-
- raise_if_read_blocked(ref)
- try:
- raw = path.read_bytes()
- except OSError as exc:
- logger.debug("could not read reference image %s: %s", ref, exc)
- return None
- mime = mimetypes.guess_type(path.name)[0] or "image/png"
- return f"data:{mime};base64,{base64.b64encode(raw).decode('ascii')}"
-
-
-def _extract_images(payload: Dict[str, Any]) -> List[str]:
- """Generated image URLs from ``choices[].message.images[].image_url.url``."""
- out: List[str] = []
- choices = payload.get("choices") if isinstance(payload, dict) else None
- for choice in choices if isinstance(choices, list) else []:
- message = choice.get("message") if isinstance(choice, dict) else None
- images = message.get("images") if isinstance(message, dict) else None
- for image in images if isinstance(images, list) else []:
- image_url = image.get("image_url") if isinstance(image, dict) else None
- url = image_url.get("url") if isinstance(image_url, dict) else None
- if isinstance(url, str) and url.strip():
- out.append(url.strip())
- return out
-
-
-def _access_error_hint(
- display: str, model_id: str, env_var: str, status: int, err_msg: str
-) -> Optional[str]:
- """One actionable line when an access-gated OpenAI image model is unreachable.
-
- Some OpenAI image models on OpenRouter need account enablement / BYOK, so
- the key is valid but the *model* is unreachable and "check your key" would
- mislead. ``None`` when this isn't that case.
- """
- if not model_id.startswith("openai/"):
- return None
- low = (err_msg or "").lower()
- gated = status in (402, 403, 404) or any(
- s in low for s in ("no endpoints", "no allowed", "not a valid model", "data policy")
- )
- if not gated:
- return None
- return (
- f"{display} can't reach image model '{model_id}' ({status}) — enable OpenAI "
- f"image access in your {display} account, or set {env_var}={_FALLBACK_MODEL}."
- )
-
-
-def _fetch_catalog(
- base_url: str, api_key: str, *, path: str, meta: Dict[str, Dict[str, Any]],
- generic: str, image_output_only: bool,
-) -> List[Dict[str, Any]]:
- """Picker rows from ``GET {base_url}{path}``; raises on failure.
-
- ``image_output_only`` keeps entries whose ``architecture.output_modalities``
- includes ``image`` and drops router pseudo-models. Curated ``meta`` wins
- for known ids; unknown ones get the API name and ``generic`` strengths.
- """
- import requests
-
- response = requests.get(
- f"{base_url}{path}",
- headers={"Authorization": f"Bearer {api_key}"} if api_key else {},
- timeout=_LIVE_TIMEOUT,
- )
- response.raise_for_status()
- out: List[Dict[str, Any]] = []
- for entry in response.json().get("data") or []:
- if not isinstance(entry, dict):
- continue
- model_id = entry.get("id")
- if not isinstance(model_id, str) or not model_id.strip():
- continue
- model_id = model_id.strip()
- arch_raw = entry.get("architecture")
- arch: Dict[str, Any] = arch_raw if isinstance(arch_raw, dict) else {}
- if image_output_only and (
- model_id.startswith(_EXCLUDED_MODEL_PREFIXES)
- or "image" not in (arch.get("output_modalities") or [])
- ):
- continue
- known = meta.get(model_id, {})
- out.append({
- "id": model_id,
- "display": known.get("display", entry.get("name") or model_id),
- "strengths": known.get("strengths", generic),
- "input_modalities": arch.get("input_modalities") or [],
- })
- return out
-
-
-# ===========================================================================
+# ---------------------------------------------------------------------------
# OpenRouter Dedicated Image API (POST /images/generations)
-# ===========================================================================
+# ---------------------------------------------------------------------------
-#: Env prefix for the Image API knobs (``OPENROUTER_IMAGE_API_QUALITY`` …),
-#: distinct from ``OPENROUTER_IMAGE_MODEL`` which selects the model on either surface.
+#: Env prefix for Image API knobs (``OPENROUTER_IMAGE_API_QUALITY`` …); distinct
+#: from ``OPENROUTER_IMAGE_MODEL`` which selects the model on either surface.
_IMAGE_API_ENV_PREFIX = "OPENROUTER_IMAGE_API_"
-
-#: Connect budget, separate from the read budget: if DNS + TCP + TLS hasn't
-#: completed in 20s the endpoint is down, and waiting out the generation
-#: timeout only delays the error.
+#: Connect budget separate from the read budget: no TLS in 20s means the
+#: endpoint is down, and waiting out the generation timeout only delays the error.
_IMAGE_API_CONNECT_TIMEOUT = 20.0
-
-#: How long a fetched ``/images/models`` catalog stays usable. It only grows,
-#: and a stale entry costs one wasted 404 at worst.
+#: Cached ``/images/models`` probes keyed by base URL: ``(fetched_at, ids)``.
+#: Empty sets are cached too, so a failing endpoint is probed once per TTL.
_CATALOG_TTL_SECONDS = 900.0
-
-#: Cached catalog probes keyed by base URL: ``(fetched_at, model_ids)``. Empty
-#: sets are cached too, so a failing endpoint is probed once per TTL.
_CATALOG_CACHE: Dict[str, Tuple[float, frozenset]] = {}
_GEMINI_RATIOS = (
@@ -251,11 +98,10 @@ def _image_api_model(display: str, strengths: str, **spec: Any) -> Dict[str, Any
return {"display": display, "strengths": strengths, **_NO_KNOBS, "resolutions": (), **spec}
-#: Curated Image API models and the parameters each declares in
-#: ``GET /images/models``. The catalog is larger and keeps growing; an id
-#: missing here still works — it just gets no per-model parameter filtering
-#: and costs one cached catalog probe to recognise. Keys mirror the payload
-#: field they gate; an empty tuple means the model has no such knob.
+#: Curated Image API models and the parameters each declares in ``GET
+#: /images/models``. An id missing here still works — it just gets no per-model
+#: parameter filtering and costs one cached catalog probe to recognise. Keys
+#: mirror the payload field they gate; an empty tuple means "no such knob".
_IMAGE_API_MODELS: Dict[str, Dict[str, Any]] = {
"google/gemini-3.1-flash-lite-image": _image_api_model(
"Nano Banana 2 Lite (Gemini 3.1 Flash Lite Image)",
@@ -316,21 +162,20 @@ _IMAGE_API_MODELS: Dict[str, Dict[str, Any]] = {
),
}
-#: Applied to a catalog model the table doesn't describe, so a newly released
-#: id still generates. Empty ``aspect_ratios`` means "enum unknown" and the
-#: field is omitted rather than guessed.
+#: Applied to a catalog model the table doesn't describe. Empty ``aspect_ratios``
+#: means "enum unknown" and the field is omitted rather than guessed.
_UNKNOWN_IMAGE_API_MODEL = _image_api_model("", "", aspect_ratios=(), max_n=1, max_refs=16)
-#: Union of exact ratios the endpoint's validator accepts across all models;
-#: sanity-checks an override aimed at a model whose own enum we don't know.
+#: Union of exact ratios the endpoint accepts across all models; sanity-checks
+#: an override aimed at a model whose own enum we don't know.
_ENDPOINT_ASPECT_RATIOS = frozenset({
"1:1", "1:2", "1:4", "1:8", "2:1", "2:3", "3:2", "3:4", "4:1", "4:3",
"4:5", "5:4", "8:1", "9:16", "16:9", "9:19.5", "19.5:9", "9:20", "20:9",
"9:21", "21:9", "auto",
})
-#: Semantic ratio → exact ratios, best first. ``landscape`` lands on 16:9 where
-#: it exists and degrades to 3:2 on ``gpt-image-1-mini``, which has no 16:9.
+#: Semantic ratio → exact ratios, best first (``landscape`` degrades to 3:2 on
+#: ``gpt-image-1-mini``, which has no 16:9).
_ASPECT_PREFERENCES: Dict[str, Tuple[str, ...]] = {
"landscape": ("16:9", "3:2", "4:3", "5:4", "21:9", "2:1", "19.5:9", "20:9", "4:1", "8:1"),
"portrait": ("9:16", "2:3", "3:4", "4:5", "9:21", "1:2", "9:19.5", "9:20", "1:4", "1:8"),
@@ -338,24 +183,121 @@ _ASPECT_PREFERENCES: Dict[str, Tuple[str, ...]] = {
}
_MEDIA_TYPE_EXTENSIONS = {
- "image/png": "png",
- "image/jpeg": "jpg",
- "image/jpg": "jpg",
- "image/webp": "webp",
- "image/gif": "gif",
- "image/svg+xml": "svg",
+ "image/png": "png", "image/jpeg": "jpg", "image/jpg": "jpg",
+ "image/webp": "webp", "image/gif": "gif", "image/svg+xml": "svg",
}
#: HTTP statuses worth retrying on the next model of the chain. 400 is our own
#: payload and would repeat; 401/403 are account-level and answered before this
-#: set is consulted. 502 matters because the Image API bills all-or-nothing and
-#: reports a failed (unbilled) generation that way.
+#: set is consulted. 502 is how the all-or-nothing-billed Image API reports a
+#: failed (unbilled) generation.
_IMAGE_API_FALLBACK_STATUSES = frozenset({402, 404, 408, 409, 425, 429, 500, 502, 503, 504})
#: Ids that stay on ``/chat/completions`` whatever the image catalog says —
-#: they are this backend's tested defaults (see the module docstring).
+#: they are this backend's tested defaults.
_CHAT_ONLY_MODELS = frozenset({DEFAULT_MODEL, _FALLBACK_MODEL})
+#: Image API payload knobs copied into the success ``extra``.
+_IMAGE_API_EXTRA_KEYS = ("resolution", "quality", "background", "output_format", "seed", "n")
+
+
+def _to_image_url_part(ref: str) -> Optional[str]:
+ """``image_url`` value for a reference: URLs pass through, local files are
+ inlined as base64 data URIs. ``None`` when the file can't be read."""
+ ref = str(ref or "").strip()
+ if not ref:
+ return None
+ if ref.startswith(("http://", "https://", "data:")):
+ return ref
+ path = Path(ref)
+ # Enforce the shared credential-read guard before inlining local bytes.
+ from agent.file_safety import raise_if_read_blocked
+
+ raise_if_read_blocked(ref)
+ try:
+ raw = path.read_bytes()
+ except OSError as exc:
+ logger.debug("could not read reference image %s: %s", ref, exc)
+ return None
+ mime = mimetypes.guess_type(path.name)[0] or "image/png"
+ return f"data:{mime};base64,{base64.b64encode(raw).decode('ascii')}"
+
+
+def _extract_images(payload: Dict[str, Any]) -> List[str]:
+ """Generated image URLs from ``choices[].message.images[].image_url.url``."""
+ out: List[str] = []
+ choices = payload.get("choices") if isinstance(payload, dict) else None
+ for choice in choices if isinstance(choices, list) else []:
+ message = choice.get("message") if isinstance(choice, dict) else None
+ images = message.get("images") if isinstance(message, dict) else None
+ for image in images if isinstance(images, list) else []:
+ image_url = image.get("image_url") if isinstance(image, dict) else None
+ url = image_url.get("url") if isinstance(image_url, dict) else None
+ if isinstance(url, str) and url.strip():
+ out.append(url.strip())
+ return out
+
+
+def _access_error_hint(
+ display: str, model_id: str, env_var: str, status: int, err_msg: str
+) -> Optional[str]:
+ """Actionable line when an access-gated OpenAI image model is unreachable
+ (the key is valid but the *model* needs account enablement / BYOK, so
+ "check your key" would mislead). ``None`` when this isn't that case."""
+ if not model_id.startswith("openai/"):
+ return None
+ low = (err_msg or "").lower()
+ gated = status in (402, 403, 404) or any(
+ s in low for s in ("no endpoints", "no allowed", "not a valid model", "data policy")
+ )
+ if not gated:
+ return None
+ return (
+ f"{display} can't reach image model '{model_id}' ({status}) — enable OpenAI "
+ f"image access in your {display} account, or set {env_var}={_FALLBACK_MODEL}."
+ )
+
+
+def _fetch_catalog(
+ base_url: str, api_key: str, *, path: str, meta: Dict[str, Dict[str, Any]],
+ generic: str, image_output_only: bool,
+) -> List[Dict[str, Any]]:
+ """Picker rows from ``GET {base_url}{path}``; raises on failure.
+
+ ``image_output_only`` keeps entries whose ``architecture.output_modalities``
+ includes ``image`` and drops router pseudo-models. Curated ``meta`` wins
+ for known ids; unknown ones get the API name and ``generic`` strengths.
+ """
+ import requests
+
+ response = requests.get(
+ f"{base_url}{path}",
+ headers={"Authorization": f"Bearer {api_key}"} if api_key else {},
+ timeout=_LIVE_TIMEOUT,
+ )
+ response.raise_for_status()
+ out: List[Dict[str, Any]] = []
+ for entry in response.json().get("data") or []:
+ model_id = entry.get("id") if isinstance(entry, dict) else None
+ if not isinstance(model_id, str) or not model_id.strip():
+ continue
+ model_id = model_id.strip()
+ arch_raw = entry.get("architecture")
+ arch: Dict[str, Any] = arch_raw if isinstance(arch_raw, dict) else {}
+ if image_output_only and (
+ model_id.startswith(_EXCLUDED_MODEL_PREFIXES)
+ or "image" not in (arch.get("output_modalities") or [])
+ ):
+ continue
+ known = meta.get(model_id, {})
+ out.append({
+ "id": model_id,
+ "display": known.get("display", entry.get("name") or model_id),
+ "strengths": known.get("strengths", generic),
+ "input_modalities": arch.get("input_modalities") or [],
+ })
+ return out
+
def _image_api_model_meta(model_id: str) -> Dict[str, Any]:
"""Catalog metadata for *model_id*, or permissive defaults when unknown."""
@@ -365,17 +307,15 @@ def _image_api_model_meta(model_id: str) -> Dict[str, Any]:
def _fetch_image_api_catalog(base_url: str, api_key: str) -> frozenset:
"""Model ids served by ``GET {base_url}/images/models``, cached per base URL.
- Best-effort by design: any failure caches and returns an empty set, which
- routes the call to chat-completions. Guessing the other way would send a
- chat-completions id to ``/images/generations`` and turn a working setup
- into a 404.
+ Best-effort: any failure caches and returns an empty set, which routes the
+ call to chat-completions — guessing the other way would send a
+ chat-completions id to ``/images/generations`` and 404 a working setup.
"""
import requests
cached = _CATALOG_CACHE.get(base_url)
if cached and (time.monotonic() - cached[0]) < _CATALOG_TTL_SECONDS:
return cached[1]
-
ids: set = set()
try:
response = requests.get(
@@ -392,28 +332,25 @@ def _fetch_image_api_catalog(base_url: str, api_key: str) -> frozenset:
ids.add(model_id.strip())
except Exception as exc: # noqa: BLE001 - probe must never break generation
logger.debug("image API catalog probe failed for %s: %s", base_url, exc)
-
resolved = frozenset(ids)
_CATALOG_CACHE[base_url] = (time.monotonic(), resolved)
return resolved
def _select_surface(model_id: str, base_url: str, api_key: str, config_key: str) -> str:
- """Return ``"images"`` or ``"chat"`` for *model_id*.
+ """``"images"`` or ``"chat"`` for *model_id*.
- Deterministic and offline for curated ids (:data:`_IMAGE_API_MODELS`,
- :data:`_CHAT_ONLY_MODELS`). An id in neither table is checked against the
- cached live ``/images/models`` catalog — a positive probe must route there,
- since staying on chat would 404/400 a model the live picker offered.
- Offline the probe is empty and unknown ids stay on chat-completions.
+ ``image_gen.<key>.surface`` / ``OPENROUTER_IMAGE_API_SURFACE`` force it;
+ otherwise curated ids are deterministic and offline, and an id in neither
+ table is checked against the cached live catalog (a positive probe must
+ route there — staying on chat would 404 a model the live picker offered;
+ offline the probe is empty and unknown ids stay on chat).
"""
if not model_id:
return "chat"
-
forced = _image_api_setting("surface", None, config_key)
if isinstance(forced, str) and forced.strip().lower() in {"images", "chat"}:
return forced.strip().lower()
-
if model_id in _CHAT_ONLY_MODELS:
return "chat"
if model_id in _IMAGE_API_MODELS or model_id in _fetch_image_api_catalog(base_url, api_key):
@@ -422,8 +359,8 @@ def _select_surface(model_id: str, base_url: str, api_key: str, config_key: str)
def _image_api_setting(name: str, explicit: Any, config_key: str) -> Any:
- """Resolve one Image API knob: call kwarg → ``OPENROUTER_IMAGE_API_<NAME>``
- env → scoped config. Blank strings count as unset."""
+ """One Image API knob: call kwarg → ``OPENROUTER_IMAGE_API_<NAME>`` env →
+ scoped config. Blank strings count as unset."""
if explicit is not None and not (isinstance(explicit, str) and not explicit.strip()):
return explicit
env_value = os.environ.get(f"{_IMAGE_API_ENV_PREFIX}{name.upper()}", "").strip()
@@ -431,41 +368,28 @@ def _image_api_setting(name: str, explicit: Any, config_key: str) -> Any:
return env_value
scoped = _load_image_gen_config().get(config_key)
value = scoped.get(name) if isinstance(scoped, dict) else None
- if isinstance(value, str):
- return value.strip() or None
- return value
+ return (value.strip() or None) if isinstance(value, str) else value
def _coerce_int(value: Any) -> Optional[int]:
"""Best-effort int (env vars arrive as strings); ``None`` when not numeric."""
if isinstance(value, bool):
return None
- if isinstance(value, int):
- return value
- if isinstance(value, float):
+ if isinstance(value, (int, float)):
return int(value)
- if isinstance(value, str):
- try:
- return int(value.strip())
- except ValueError:
- return None
- return None
+ try:
+ return int(value.strip()) if isinstance(value, str) else None
+ except ValueError:
+ return None
def _pick_exact_aspect_ratio(
- semantic: str,
- meta: Dict[str, Any],
- forced: Optional[str],
- notes: List[str],
+ semantic: str, meta: Dict[str, Any], forced: Optional[str], notes: List[str]
) -> Optional[str]:
- """Exact ``aspect_ratio`` to send, or ``None`` to omit it.
-
- *forced* (an exact ratio from config/env/kwarg) wins when the model
- supports it; otherwise the downgrade is noted and the per-model mapping of
- *semantic* applies.
- """
+ """Exact ``aspect_ratio`` to send, or ``None`` to omit it. *forced* wins when
+ the model supports it; otherwise the downgrade is noted and the per-model
+ mapping of *semantic* applies."""
supported: Tuple[str, ...] = tuple(meta.get("aspect_ratios") or ())
-
if isinstance(forced, str) and forced.strip():
value = forced.strip()
if (supported and value in supported) or (not supported and value in _ENDPOINT_ASPECT_RATIOS):
@@ -474,32 +398,23 @@ def _pick_exact_aspect_ratio(
f"requested aspect_ratio '{value}' is unsupported by this model; "
f"used the '{semantic}' mapping instead"
)
-
if not supported:
# Unknown enum: an out-of-enum aspect_ratio is a hard 400 (unlike an
- # unknown *parameter*, which the endpoint ignores), so omit the field
- # and let the model apply its own default.
+ # unknown *parameter*, which the endpoint ignores), so omit the field.
notes.append(
"model is not in this backend's catalog, so its aspect_ratio enum is "
f"unknown; the field was omitted and '{semantic}' was not applied"
)
return None
-
for candidate in _ASPECT_PREFERENCES.get(semantic, ()):
if candidate in supported:
return candidate
-
return "auto" if "auto" in supported else supported[0]
def _image_api_enum(
- name: str,
- explicit: Any,
- meta: Dict[str, Any],
- config_key: str,
- notes: List[str],
- *,
- meta_key: Optional[str] = None,
+ name: str, explicit: Any, meta: Dict[str, Any], config_key: str, notes: List[str],
+ *, meta_key: Optional[str] = None,
) -> Optional[str]:
"""Resolve an enum knob and drop it when this model doesn't accept it."""
value = _image_api_setting(name, explicit, config_key)
@@ -511,29 +426,20 @@ def _image_api_enum(
notes.append(f"'{name}' is not supported by this model; dropped")
return None
if value not in allowed:
- notes.append(
- f"'{name}={value}' is not valid for this model "
- f"(accepts {', '.join(allowed)}); dropped"
- )
+ notes.append(f"'{name}={value}' is not valid for this model (accepts {', '.join(allowed)}); dropped")
return None
return value
def _build_image_api_payload(
- *,
- model_id: str,
- prompt: str,
- semantic_aspect: str,
- references: List[str],
- config_key: str,
- kwargs: Dict[str, Any],
+ *, model_id: str, prompt: str, semantic_aspect: str, references: List[str],
+ config_key: str, kwargs: Dict[str, Any],
) -> Tuple[Dict[str, Any], List[str]]:
- """Assemble the ``/images/generations`` body. Returns ``(payload, notes)``.
+ """Assemble the ``/images/generations`` body: ``(payload, notes)``.
- Every optional knob is filtered against what the model declares, because a
- parameter the model doesn't know is silently *ignored* by the endpoint —
- which would let a caller believe ``background=transparent`` took effect on
- a model that has no transparency.
+ Every optional knob is filtered against what the model declares, because
+ the endpoint silently *ignores* unknown parameters — which would let a
+ caller believe ``background=transparent`` took effect on a model without it.
"""
meta = _image_api_model_meta(model_id)
notes: List[str] = []
@@ -547,9 +453,7 @@ def _build_image_api_payload(
for enum_name, meta_key in (
("resolution", "resolutions"), ("quality", None), ("background", None), ("output_format", None),
):
- value = _image_api_enum(
- enum_name, kwargs.get(enum_name), meta, config_key, notes, meta_key=meta_key
- )
+ value = _image_api_enum(enum_name, kwargs.get(enum_name), meta, config_key, notes, meta_key=meta_key)
if value:
payload[enum_name] = value
@@ -580,20 +484,14 @@ def _build_image_api_payload(
f"accepts {max_refs}; extras dropped"
)
if usable:
- payload["input_references"] = [
- {"type": "image_url", "image_url": {"url": url}} for url in usable
- ]
-
+ payload["input_references"] = [{"type": "image_url", "image_url": {"url": url}} for url in usable]
return payload, notes
def _extract_image_api_error(response: Any, fallback: str) -> str:
- """Flatten either error shape this endpoint produces into one line.
-
- ``{"error": {"message", "code"}}`` covers routing / auth / unknown model;
- ``{"success": false, "error": {"name": "ZodError", "message": "<json>"}}``
- covers request validation, where ``message`` is a JSON-encoded issue array.
- """
+ """Flatten either error shape into one line: ``{"error": {"message", "code"}}``
+ (routing / auth / unknown model) or ``{"success": false, "error": {"name":
+ "ZodError", "message": "<json issue array>"}}`` (request validation)."""
if response is None:
return fallback
try:
@@ -616,12 +514,10 @@ def _extract_image_api_error(response: Any, fallback: str) -> str:
return message[:300]
parts: List[str] = []
for issue in issues if isinstance(issues, list) else []:
- if not isinstance(issue, dict):
- continue
- field = ".".join(str(p) for p in (issue.get("path") or [])) or "request"
- parts.append(f"{field}: {issue.get('message') or 'invalid'}")
+ if isinstance(issue, dict):
+ field = ".".join(str(p) for p in (issue.get("path") or [])) or "request"
+ parts.append(f"{field}: {issue.get('message') or 'invalid'}")
return "; ".join(parts)[:400] or message[:300]
-
if isinstance(message, str) and message.strip():
return message.strip()[:300]
return json.dumps(error)[:300]
@@ -644,20 +540,39 @@ def _save_image_api_entry(entry: Dict[str, Any], prefix: str) -> Optional[str]:
b64 = entry.get("b64_json")
if isinstance(b64, str) and b64.strip():
return str(save_b64_image(b64, prefix=prefix, extension=_extension_for(entry.get("media_type"))))
-
url = entry.get("url")
if isinstance(url, str) and url.strip():
return str(save_url_image(url.strip(), prefix=prefix))
-
return None
+def _image_api_extra(
+ payload: Dict[str, Any], saved: List[str], usable_refs: List[str], notes: List[str], body: Any
+) -> Dict[str, Any]:
+ """Success ``extra`` for an Image API result (knobs sent, extra images, usage)."""
+ extra: Dict[str, Any] = {"endpoint": "images/generations", "exact_aspect_ratio": payload.get("aspect_ratio")}
+ extra.update({key: payload[key] for key in _IMAGE_API_EXTRA_KEYS if key in payload})
+ if len(saved) > 1:
+ extra["additional_images"] = saved[1:]
+ if usable_refs:
+ extra["reference_images_used"] = len(payload.get("input_references") or [])
+ if notes:
+ extra["notes"] = notes
+ usage = body.get("usage") if isinstance(body, dict) else None
+ if isinstance(usage, dict):
+ if isinstance(usage.get("cost"), (int, float)):
+ extra["cost_usd"] = usage["cost"]
+ if isinstance(usage.get("total_tokens"), int):
+ extra["total_tokens"] = usage["total_tokens"]
+ return extra
+
+
class OpenRouterCompatImageProvider(ImageGenProvider):
"""Image generation over an OpenRouter-compatible chat-completions endpoint.
- Instantiated once per backend (OpenRouter, Nous Portal); they differ only
- in which runtime provider supplies ``(base_url, api_key)``, the config
- namespace for the model override, and Image API support.
+ One instance per backend (OpenRouter, Nous Portal); they differ only in the
+ runtime provider supplying ``(base_url, api_key)``, the config namespace for
+ the model override, and Image API support (OpenRouter only).
"""
def __init__(
@@ -679,7 +594,6 @@ class OpenRouterCompatImageProvider(ImageGenProvider):
self._setup_schema = setup_schema
self._live_models_cache: Optional[tuple] = None
self._image_api_models_cache: Optional[tuple] = None
- # OpenRouter only: Nous Portal has no /images/generations route.
self._supports_image_api = supports_image_api
@property
@@ -691,7 +605,6 @@ class OpenRouterCompatImageProvider(ImageGenProvider):
return self._display
def _resolve_runtime(self) -> Dict[str, Any]:
- """Resolve ``(base_url, api_key)`` via the shared runtime resolver."""
from hermes_cli.runtime_provider import resolve_runtime_provider
return resolve_runtime_provider(requested=self._runtime_name)
@@ -712,9 +625,8 @@ class OpenRouterCompatImageProvider(ImageGenProvider):
return False
def capabilities(self) -> Dict[str, Any]:
- # Text-to-image and image-to-image (reference grounding). Image API
- # models take far more references than chat-completions, so report the
- # cap of the model that would service the next call.
+ # Image API models take far more references than chat-completions, so
+ # report the cap of the model that would service the next call.
max_refs = _MAX_REFERENCE_IMAGES
if self._supports_image_api:
chain = self._resolve_model_chain()
@@ -726,11 +638,10 @@ class OpenRouterCompatImageProvider(ImageGenProvider):
def list_models(self) -> List[Dict[str, Any]]:
"""Picker catalog: the endpoint's full live image-model surface.
- OpenRouter: union of ``GET /images/models`` (40+ models) and the
- chat-completions image models, so every image model the endpoint serves
- — including ones released after this code shipped — is selectable. Nous
- Portal (no ``/images`` route) lists the chat-completions catalog only.
- Offline fallback: the static default chain plus the curated Image API snapshot.
+ OpenRouter: ``GET /images/models`` ∪ chat-completions image models, so
+ models released after this code shipped are selectable. Nous Portal
+ lists the chat-completions catalog only. Offline fallback: the static
+ default chain plus the curated Image API snapshot.
"""
merged: Dict[str, Dict[str, Any]] = {}
if self._supports_image_api:
@@ -739,13 +650,10 @@ class OpenRouterCompatImageProvider(ImageGenProvider):
for entry in self._live_models():
merged.setdefault(entry["id"], entry)
if merged:
- return sorted(
- merged.values(), key=lambda m: (_MODEL_PRIORITY.get(m["id"], 2), m["id"])
- )
+ return sorted(merged.values(), key=lambda m: (_MODEL_PRIORITY.get(m["id"], 2), m["id"]))
models = [{"id": model_id, **meta} for model_id, meta in _KNOWN_MODEL_META.items()]
if self._supports_image_api:
- # Curated snapshot keeps the Image API models pickable offline.
models.extend(
{"id": model_id, "display": meta["display"], "strengths": f"{meta['strengths']} (Image API)"}
for model_id, meta in _IMAGE_API_MODELS.items()
@@ -769,7 +677,6 @@ class OpenRouterCompatImageProvider(ImageGenProvider):
return models
def _image_api_live_models(self) -> List[Dict[str, Any]]:
- """Cached live ``GET /images/models`` entries; curated metadata for known ids."""
return self._cached_catalog(
"_image_api_models_cache", path="/images/models", meta=_IMAGE_API_MODELS,
generic="Image API model (from live OpenRouter catalog)", image_output_only=False,
@@ -777,7 +684,6 @@ class OpenRouterCompatImageProvider(ImageGenProvider):
)
def _live_models(self) -> List[Dict[str, Any]]:
- """Cached image-output models from the endpoint's ``/models`` catalog."""
return self._cached_catalog(
"_live_models_cache", path="/models", meta=_KNOWN_MODEL_META,
generic="Image-output model (from live OpenRouter catalog)", image_output_only=True,
@@ -785,42 +691,33 @@ class OpenRouterCompatImageProvider(ImageGenProvider):
)
def default_model(self) -> Optional[str]:
- # The catalog default, not the effective runtime model
- # (that is _resolve_model_chain()).
+ # The catalog default, not the effective runtime model (_resolve_model_chain).
return DEFAULT_MODEL
def get_setup_schema(self) -> Dict[str, Any]:
return dict(self._setup_schema)
def _resolve_model(self, explicit: Optional[str] = None) -> str:
- """Pick the image model (first of :meth:`_resolve_model_chain`)."""
return self._resolve_model_chain(explicit)[0]
def _resolve_model_chain(self, explicit: Optional[str] = None) -> list[str]:
- """Ordered model attempts: explicit ``model`` kwarg → ``*_IMAGE_MODEL``
- env → ``image_gen.<provider>.model`` → top-level ``image_gen.model`` →
- the quality-first default chain. Any explicit selection means "use
- exactly this model", so only the bare default chain carries a fallback."""
+ """Ordered model attempts: explicit ``model`` kwarg → ``*_IMAGE_MODEL`` env
+ → ``image_gen.<provider>.model`` → ``image_gen.model`` → the default
+ chain. Any explicit selection is exact, so only the bare default chain
+ carries a fallback."""
for candidate in (explicit, os.environ.get(self._model_env_var)):
if isinstance(candidate, str) and candidate.strip():
return [candidate.strip()]
cfg = _load_image_gen_config()
scoped = cfg.get(self._config_key)
- for candidate in ((scoped or {}).get("model") if isinstance(scoped, dict) else None, cfg.get("model")):
+ for candidate in (scoped.get("model") if isinstance(scoped, dict) else None, cfg.get("model")):
if isinstance(candidate, str) and candidate.strip():
return [candidate.strip()]
return list(_DEFAULT_MODEL_CHAIN)
def _generate_via_image_api(
- self,
- *,
- model_id: str,
- prompt: str,
- semantic_aspect: str,
- references: List[str],
- base_url: str,
- headers: Dict[str, str],
- kwargs: Dict[str, Any],
+ self, *, model_id: str, prompt: str, semantic_aspect: str, references: List[str],
+ base_url: str, headers: Dict[str, str], kwargs: Dict[str, Any],
) -> Dict[str, Any]:
"""Serve one model through ``POST {base_url}/images/generations``.
@@ -842,21 +739,15 @@ class OpenRouterCompatImageProvider(ImageGenProvider):
try:
for ref in references:
part = _to_image_url_part(ref)
- if part:
- usable_refs.append(part)
- else:
- unreadable.append(str(ref))
+ (usable_refs if part else unreadable).append(part or str(ref))
except Exception as exc: # noqa: BLE001 - blocked by the file-safety guard
return _fail(f"Could not load reference image: {exc}", "io_error")
-
- # An edit whose every source image failed to load must not quietly
- # become a text-to-image generation: that bills an unrelated picture
- # and success=True hides it.
+ # An edit whose every source image failed to load must not quietly become
+ # a text-to-image generation: that bills an unrelated picture.
if unreadable and not usable_refs:
return _fail(
"Could not read the reference image(s) requested for editing: "
- + ", ".join(unreadable)
- + ". Refusing to silently fall back to text-to-image.",
+ + ", ".join(unreadable) + ". Refusing to silently fall back to text-to-image.",
"io_error",
)
@@ -875,8 +766,7 @@ class OpenRouterCompatImageProvider(ImageGenProvider):
except (TypeError, ValueError):
logger.debug("%s: ignoring non-numeric image API timeout %r", self._name, configured)
- # (connect, read): an unreachable endpoint fails in seconds instead of
- # burning the whole read budget.
+ # (connect, read): an unreachable endpoint fails in seconds, not the whole read budget.
body, failure = post_json(
f"{base_url}/images/generations", headers=headers, payload=payload,
timeout=(min(_IMAGE_API_CONNECT_TIMEOUT, timeout), timeout), label=self._display,
@@ -887,9 +777,7 @@ class OpenRouterCompatImageProvider(ImageGenProvider):
if failure.kind != "http":
return _fail(failure.error, failure.error_type, retryable=failure.kind == "timeout")
status, message = failure.status, failure.message
- logger.error(
- "%s image API generation failed (%s) on %s: %s", self._name, status, model_id, message,
- )
+ logger.error("%s image API generation failed (%s) on %s: %s", self._name, status, model_id, message)
# 401/403 first so one account-level rejection does not get two
# different error_types depending on where in the chain it landed.
if status in (401, 403):
@@ -899,59 +787,73 @@ class OpenRouterCompatImageProvider(ImageGenProvider):
f"Model '{model_id}' does not exist on the OpenRouter Image API "
f"(its catalog is separate from chat-completions — check "
f"GET {base_url}/images/models).",
- "model_access",
- retryable=True,
+ "model_access", retryable=True,
)
return _fail(failure.error, "api_error", retryable=status in _IMAGE_API_FALLBACK_STATUSES)
entries = body.get("data") if isinstance(body, dict) else None
entries = [e for e in entries if isinstance(e, dict)] if isinstance(entries, list) else []
if not entries:
- return _fail(
- f"{self._display} returned no image data for '{model_id}'.",
- "empty_response",
- retryable=True,
- )
-
+ return _fail(f"{self._display} returned no image data for '{model_id}'.", "empty_response", retryable=True)
prefix = f"{self._name}_{_model_slug(model_id)}"
try:
saved = [p for p in (_save_image_api_entry(e, prefix) for e in entries) if p]
except Exception as exc: # noqa: BLE001
return _fail(f"Could not save generated image: {exc}", "io_error")
-
if not saved:
return _fail(f"{self._display} response carried neither b64_json nor url.", "empty_response")
+ return success_response(
+ image=saved[0], model=model_id, prompt=prompt, aspect_ratio=semantic_aspect,
+ provider=self._name, modality="image" if usable_refs else "text",
+ extra=_image_api_extra(payload, saved, usable_refs, notes, body),
+ )
- extra: Dict[str, Any] = {
- "endpoint": "images/generations",
- "exact_aspect_ratio": payload.get("aspect_ratio"),
+ def _generate_via_chat(
+ self, *, model_id: str, prompt: str, aspect: str, content: List[Dict[str, Any]],
+ base_url: str, headers: Dict[str, str],
+ ) -> Tuple[Dict[str, Any], Optional[str]]:
+ """One ``/chat/completions`` attempt: ``(result, retry_reason)``; the reason
+ is set when the chain may continue with the fallback model."""
+ fail = error_factory(self._name, aspect, model=model_id, prompt=prompt)
+ payload: Dict[str, Any] = {
+ "model": model_id,
+ "modalities": ["image", "text"],
+ "messages": [{"role": "user", "content": content}],
+ "image_config": {"aspect_ratio": _ASPECT_RATIOS.get(aspect, "1:1")},
}
- for key in ("resolution", "quality", "background", "output_format", "seed", "n"):
- if key in payload:
- extra[key] = payload[key]
- if len(saved) > 1:
- extra["additional_images"] = saved[1:]
- if usable_refs:
- extra["reference_images_used"] = len(payload.get("input_references") or [])
- if notes:
- extra["notes"] = notes
-
- usage = body.get("usage") if isinstance(body, dict) else None
- if isinstance(usage, dict):
- if isinstance(usage.get("cost"), (int, float)):
- extra["cost_usd"] = usage["cost"]
- if isinstance(usage.get("total_tokens"), int):
- extra["total_tokens"] = usage["total_tokens"]
-
- return success_response(
- image=saved[0],
- model=model_id,
- prompt=prompt,
- aspect_ratio=semantic_aspect,
- provider=self._name,
- modality="image" if usable_refs else "text",
- extra=extra,
+ result, failure = post_json(
+ f"{base_url}/chat/completions", headers=headers, payload=payload,
+ timeout=_REQUEST_TIMEOUT, label=self._display,
)
+ if failure is not None:
+ if failure.kind == "http":
+ logger.error(
+ "%s image gen failed (%d) on %s: %s", self._name, failure.status, model_id, failure.message,
+ )
+ hint = _access_error_hint(
+ self._display, model_id, self._model_env_var, failure.status, failure.message
+ )
+ return fail(hint or failure.error, "model_access" if hint else "api_error"), "unavailable" if hint else None
+ return fail(failure.error, failure.error_type), "timed out" if failure.kind == "timeout" else None
+
+ images = _extract_images(result)
+ if not images:
+ # Text but no image usually means the model didn't honor image output.
+ return fail(
+ f"{self._display} returned no image. Ensure the model '{model_id}' supports image output.",
+ "empty_response",
+ ), "returned no image"
+ first = images[0]
+ try:
+ if first.startswith("data:"):
+ saved_path = save_b64_image(first.split(",", 1)[1] if "," in first else "", prefix=f"{self._name}_gen")
+ else:
+ saved_path = save_url_image(first, prefix=f"{self._name}_gen")
+ except Exception as exc: # noqa: BLE001
+ return fail(f"Could not save generated image: {exc}", "io_error"), None
+ return success_response(
+ image=str(saved_path), model=model_id, prompt=prompt, aspect_ratio=aspect, provider=self._name,
+ ), None
def generate(
self,
@@ -962,42 +864,31 @@ class OpenRouterCompatImageProvider(ImageGenProvider):
reference_image_urls: Optional[List[str]] = None,
**kwargs: Any,
) -> Dict[str, Any]:
+ fail = error_factory(self._name, aspect_ratio)
try:
api_key, base_url = self._credentials()
except Exception as exc: # noqa: BLE001
- return error_response(
- error=f"Could not resolve {self._display} credentials: {exc}",
- error_type="missing_api_key",
- provider=self._name,
- aspect_ratio=aspect_ratio,
- )
+ return fail(f"Could not resolve {self._display} credentials: {exc}", "missing_api_key")
if not api_key or not base_url:
- return error_response(
- error=(
- f"No {self._display} credentials found. "
- f"Configure {self._display} in `hermes tools` → Image Generation."
- ),
- error_type="missing_api_key",
- provider=self._name,
- aspect_ratio=aspect_ratio,
+ return fail(
+ f"No {self._display} credentials found. "
+ f"Configure {self._display} in `hermes tools` → Image Generation.",
+ "missing_api_key",
)
model_chain = self._resolve_model_chain(kwargs.get("model"))
aspect = resolve_aspect_ratio(aspect_ratio)
-
# The pet generator passes local paths via ``reference_images``; the
# generic tool surface uses ``image_url`` / ``reference_image_urls``.
references: List[str] = [str(ref) for ref in kwargs.get("reference_images") or []]
if image_url:
references.append(str(image_url))
references.extend(str(ref) for ref in reference_image_urls or [])
-
content: List[Dict[str, Any]] = [{"type": "text", "text": prompt}]
for ref in references[:_MAX_REFERENCE_IMAGES]:
part = _to_image_url_part(ref)
if part:
content.append({"type": "image_url", "image_url": {"url": part}})
-
headers = {
"Authorization": f"Bearer {api_key}",
"Content-Type": "application/json",
@@ -1005,100 +896,31 @@ class OpenRouterCompatImageProvider(ImageGenProvider):
"HTTP-Referer": "https://github.com/NousResearch/hermes-agent",
"X-Title": "Hermes Agent",
}
+
last_error: Optional[Dict[str, Any]] = None
for i, model_id in enumerate(model_chain):
- is_last = i == len(model_chain) - 1
- fail = error_factory(self._name, aspect, model=model_id, prompt=prompt)
-
- def retry_with_fallback(reason: str) -> None:
- logger.info(
- "%s model %s %s; retrying with fallback %s",
- self._name, model_id, reason, model_chain[i + 1],
- )
-
# An Image API model cannot be served by /chat/completions (and vice
# versa), so this is a routing decision, not a preference.
- if self._supports_image_api and _select_surface(
- model_id, base_url, api_key, self._config_key
- ) == "images":
+ if self._supports_image_api and _select_surface(model_id, base_url, api_key, self._config_key) == "images":
outcome = self._generate_via_image_api(
model_id=model_id, prompt=prompt, semantic_aspect=aspect,
references=references, base_url=base_url, headers=headers, kwargs=kwargs,
)
- retryable = outcome.pop("_retryable", False)
- if outcome.get("success") or is_last or not retryable:
- return outcome
- retry_with_fallback("failed on the image API")
- last_error = outcome
- continue
-
- payload: Dict[str, Any] = {
- "model": model_id,
- "modalities": ["image", "text"],
- "messages": [{"role": "user", "content": content}],
- "image_config": {"aspect_ratio": _ASPECT_RATIOS.get(aspect, "1:1")},
- }
- result, failure = post_json(
- f"{base_url}/chat/completions", headers=headers, payload=payload,
- timeout=_REQUEST_TIMEOUT, label=self._display,
- )
- if failure is not None:
- if failure.kind == "http":
- logger.error(
- "%s image gen failed (%d) on %s: %s",
- self._name, failure.status, model_id, failure.message,
- )
- hint = _access_error_hint(
- self._display, model_id, self._model_env_var, failure.status, failure.message
- )
- if hint and not is_last:
- retry_with_fallback("unavailable")
- continue
- last_error = fail(hint or failure.error, "model_access" if hint else "api_error")
- return last_error
- if failure.kind == "timeout" and not is_last:
- retry_with_fallback("timed out")
- continue
- return fail(failure.error, failure.error_type)
-
- images = _extract_images(result)
- if not images:
- if not is_last:
- retry_with_fallback("returned no image")
- continue
- # Text but no image usually means the model didn't honor image
- # output (wrong model or modalities); surface that.
- return fail(
- f"{self._display} returned no image. Ensure the model "
- f"'{model_id}' supports image output.",
- "empty_response",
+ reason = "failed on the image API" if outcome.pop("_retryable", False) else None
+ else:
+ outcome, reason = self._generate_via_chat(
+ model_id=model_id, prompt=prompt, aspect=aspect, content=content,
+ base_url=base_url, headers=headers,
)
-
- first = images[0]
- try:
- if first.startswith("data:"):
- b64 = first.split(",", 1)[1] if "," in first else ""
- saved_path = save_b64_image(b64, prefix=f"{self._name}_gen")
- else:
- saved_path = save_url_image(first, prefix=f"{self._name}_gen")
- except Exception as exc: # noqa: BLE001
- return fail(f"Could not save generated image: {exc}", "io_error")
-
- return success_response(
- image=str(saved_path),
- model=model_id,
- prompt=prompt,
- aspect_ratio=aspect,
- provider=self._name,
- )
+ if outcome.get("success") or reason is None or i == len(model_chain) - 1:
+ return outcome
+ logger.info("%s model %s %s; retrying with fallback %s", self._name, model_id, reason, model_chain[i + 1])
+ last_error = outcome
return last_error or error_response(
error=f"{self._display} image generation failed after trying all candidate models.",
- error_type="api_error",
- provider=self._name,
- model=model_chain[-1] if model_chain else "",
- prompt=prompt,
- aspect_ratio=aspect,
+ error_type="api_error", provider=self._name,
+ model=model_chain[-1] if model_chain else "", prompt=prompt, aspect_ratio=aspect,
)
@@ -1115,13 +937,11 @@ def _build_providers() -> List[OpenRouterCompatImageProvider]:
"name": "OpenRouter (image)",
"badge": "paid",
"tag": "Gemini Flash Image, gpt-image-2, Krea 2, Qwen Image 3 & more via OpenRouter; uses OPENROUTER_API_KEY",
- "env_vars": [
- {
- "key": "OPENROUTER_API_KEY",
- "prompt": "OpenRouter API key",
- "url": "https://openrouter.ai/keys",
- }
- ],
+ "env_vars": [{
+ "key": "OPENROUTER_API_KEY",
+ "prompt": "OpenRouter API key",
+ "url": "https://openrouter.ai/keys",
+ }],
},
),
OpenRouterCompatImageProvider(