{"record":{"id":"6d8f761830d5a580","repo":"MiniMax-AI/skills","slug":"api-error-base-resp-get-status-code-base","errorCode":null,"errorMessage":"API Error [{base_resp.get('status_code')}]: {base_resp.get('status_msg')}","messagePattern":"API Error \\[(.+?)\\]: (.+?)","errorType":"exception","errorClass":"SystemExit","httpStatus":null,"severity":"error","filePath":"skills/frontend-dev/scripts/minimax_image.py","lineNumber":71,"sourceCode":"        \"n\": n,\n        \"response_format\": response_format,\n        \"prompt_optimizer\": prompt_optimizer,\n    }\n    if seed is not None:\n        payload[\"seed\"] = seed\n\n    resp = requests.post(\n        f\"{API_BASE}/image_generation\",\n        headers=_headers(),\n        json=payload,\n        timeout=120,\n    )\n    resp.raise_for_status()\n    data = resp.json()\n\n    base_resp = data.get(\"base_resp\", {})\n    if base_resp.get(\"status_code\", 0) != 0:\n        raise SystemExit(f\"API Error [{base_resp.get('status_code')}]: {base_resp.get('status_msg')}\")\n\n    return data\n\n\ndef download_and_save(url: str, output_path: str):\n    \"\"\"Download image from URL and save.\"\"\"\n    resp = requests.get(url, timeout=60)\n    resp.raise_for_status()\n    with open(output_path, \"wb\") as f:\n        f.write(resp.content)\n    return len(resp.content)\n\n\ndef main():\n    p = argparse.ArgumentParser(description=\"MiniMax Text-to-Image\")\n    p.add_argument(\"prompt\", help=\"Image description (max 1500 chars)\")\n    p.add_argument(\"-o\", \"--output\", required=True, help=\"Output file path (.png/.jpg)\")\n    p.add_argument(\"--model\", default=\"image-01\", help=\"Model (default: image-01)\")","sourceCodeStart":53,"sourceCodeEnd":89,"githubUrl":"https://github.com/MiniMax-AI/skills/blob/60aaae52bb2af8162732751a4332f62a5fef518b/skills/frontend-dev/scripts/minimax_image.py#L53-L89","documentation":"`raise SystemExit(f\"API Error [{status_code}]: {status_msg}\")` in `generate_image` after the MiniMax `image_generation` endpoint returns a 2xx HTTP response whose JSON body's `base_resp.status_code` is non-zero. MiniMax signals application-level errors (as opposed to HTTP errors) inside a 200 response via `base_resp`; a non-zero status indicates the request was received but rejected for a business reason. The message prints both the numeric code and the API's status message for diagnosis.","triggerScenarios":"Valid HTTP response (passed `raise_for_status`) but `base_resp.status_code != 0`. Common MiniMax status codes include authentication/permission failures, invalid model name, prompt-policy violations (content moderation), rate limiting, quota exhaustion, or an invalid `aspect_ratio`/`n` payload value.","commonSituations":"Wrong/region-mismatched API key (overseas key against China base URL or vice versa), an invalid `model` value, a prompt that trips content moderation, exceeded quota/rate limit, or a malformed `aspect_ratio` not in the allowed list.","solutions":["Read the numeric code and `status_msg`: 1001x → auth/key/permission (verify key and region match the base URL); 1027/1039 → content policy — revise the prompt; 1xxx rate/quota — back off and retry.","Confirm the key's region matches `MINIMAX_API_BASE` (overseas `api.minimax.io` keys vs China `api.minimaxi.com` keys are not interchangeable).","Validate `aspect_ratio` is one of the allowed values and `model`/`n` are correct before sending.","For rate/quota errors, implement exponential backoff and retry the same idempotent request."],"exampleFix":"# before — overseas key + China base URL (region mismatch)\nexport MINIMAX_API_BASE='https://api.minimaxi.com/v1'  # China\nexport MINIMAX_API_KEY='overseas-key'\n# -> SystemExit: API Error [1004]: ... auth failure\n\n# after — align regions\nexport MINIMAX_API_BASE='https://api.minimax.io/v1'   # overseas\nexport MINIMAX_API_KEY='overseas-key'","handlingStrategy":"retry","validationCode":"def is_retryable(status_code: int) -> bool:\n    # MiniMax rate-limit / quota / transient codes -> safe to back off and retry\n    return status_code in {1027, 1039, 1008, 1009} or 1000 <= status_code < 1100 and status_code not in {1004, 1005}\n\n# before sending\nif aspect_ratio not in ASPECT_RATIOS:\n    raise ValueError(f\"aspect_ratio must be one of {ASPECT_RATIOS}\")","typeGuard":"def is_auth_error(base_resp: dict) -> bool:\n    code = base_resp.get(\"status_code\", 0)\n    return code in (1004, 1005, 1027)  # key/permission/auth-region errors","tryCatchPattern":"import time\nfor attempt in range(4):\n    try:\n        return generate_image(prompt, **kwargs)\n    except SystemExit as e:\n        if is_retryable(extract_code(e)) and attempt < 3:\n            time.sleep(2 ** attempt)\n            continue\n        raise","preventionTips":["Align the API key region with `MINIMAX_API_BASE` (overseas vs China are not interchangeable).","Validate `aspect_ratio`, `model`, and `n` against allowed values before the request.","Implement exponential backoff for rate/quota codes; surface content-policy codes to the user for prompt revision.","Log `base_resp.status_code` + `status_msg` to correlate recurring failures."],"tags":["python","api","minimax","image-generation","error-handling","auth"],"backgroundTag":null,"analyzedSha":"60aaae52bb2af8162732751a4332f62a5fef518b","analyzedAt":"2026-08-13T17:32:34.717Z","schemaVersion":2},"datasetVersion":"2026-08-13T19:17:28.613Z"}