assafelovic/gpt-researcher · error · RuntimeError

ModelsLab API error

Error message

ModelsLab API error

What it means

RuntimeError raised right after the initial ModelsLab text2img POST: the API immediately responded with status "error" instead of "processing" or "success". The message is taken from the API's "messege" field (misspelled by ModelsLab), defaulting to "ModelsLab API error".

Source

Thrown at gpt_researcher/llm_provider/image/modelslab_image_generator.py:223

        try:
            import aiohttp

            async with aiohttp.ClientSession() as session:
                async with session.post(
                    TEXT2IMG_URL,
                    json=payload,
                    timeout=aiohttp.ClientTimeout(total=30),
                ) as resp:
                    body = await resp.json()
        except ImportError:
            import requests

            body = await asyncio.to_thread(
                lambda: requests.post(TEXT2IMG_URL, json=payload, timeout=30).json()
            )

        if body.get("status") == "error":
            raise RuntimeError(body.get("messege", "ModelsLab API error"))

        if body.get("status") == "processing" and body.get("id"):
            return await self._poll_for_result(body["id"])

        return body.get("output", [])

    def is_available(self) -> bool:
        """Return True if the API key is configured."""
        return bool(self.api_key)

    @classmethod
    def from_config(cls, config) -> Optional["ModelsLabImageGeneratorProvider"]:
        """Create a ModelsLabImageGeneratorProvider from a Config object."""
        enabled = getattr(config, "IMAGE_GENERATION_ENABLED", False)
        provider = getattr(config, "IMAGE_GENERATION_PROVIDER", "google")
        if not enabled or provider != "modelslab":
            return None
        model = getattr(config, "IMAGE_GENERATION_MODEL", None)

View on GitHub (pinned to 6f998577d5)

Solutions

  1. Inspect the exception message — it carries the API's stated reason.
  2. Verify the ModelsLab API key and remaining credits.
  3. Check that the payload keys/model name match the current ModelsLab text2img API docs.
  4. If the key was rotated, update the MODELslab credentials in config/env.

Example fix

// before
out = await generator._request_images(payload)

// after
try:
    out = await generator._request_images(payload)
except RuntimeError as e:
    raise ImageProviderError(f"ModelsLab rejected request: {e}") from e
Defensive patterns

Strategy: try-catch

Validate before calling

null

Type guard

null

Try / catch

try:
    out = await generator._request_images(payload)
except RuntimeError as e:
    raise ImageProviderError(str(e)) from e

Prevention

When it happens

Trigger: generate_image() → _request_images() posts payload to TEXT2IMG_URL; response JSON has body["status"] == "error" on the first call, before any polling starts.

Common situations: Invalid or exhausted API key, malformed payload (bad model_id, missing prompt), or content-policy rejection at request validation time.

Related errors


AI-assisted analysis of assafelovic/gpt-researcher@6f998577d5 (2026-08-28). Data as JSON: /api/errors/5865244db3e526ee. Report an issue: GitHub.