{"record":{"id":"7671823a1a84da2d","repo":"conductor-oss/conductor","slug":"stability-ai-api-failed-with-status-d-s","errorCode":null,"errorMessage":"Stability AI API failed with status %d: %s","messagePattern":"Stability AI API failed with status (.+?): (.+?)","errorType":"exception","errorClass":"IOException","httpStatus":null,"severity":"error","filePath":"ai/src/main/java/org/conductoross/conductor/ai/providers/stabilityai/StabilityAiApi.java","lineNumber":129,"sourceCode":"        // Request raw image bytes with Accept: image/*\n        Request request =\n                new Request.Builder()\n                        .url(baseUrl + endpoint)\n                        .header(\"Authorization\", \"Bearer \" + apiKey)\n                        .header(\"Accept\", \"image/*\")\n                        .post(bodyBuilder.build())\n                        .build();\n\n        log.info(\n                \"Stability AI image generation request: endpoint={}, model={}, outputFormat={}\",\n                endpoint,\n                params.model(),\n                outputFormat);\n\n        try (Response response = httpClient.newCall(request).execute()) {\n            if (!response.isSuccessful()) {\n                String errorBody = readResponseBody(response);\n                throw new IOException(\n                        \"Stability AI API failed with status %d: %s\"\n                                .formatted(response.code(), errorBody));\n            }\n\n            ResponseBody body = response.body();\n            if (body == null) {\n                throw new IOException(\"Stability AI API returned empty response body\");\n            }\n\n            // Determine the actual content type returned\n            String contentType = response.header(\"Content-Type\", \"image/\" + outputFormat);\n            // Read the finish-reason header (e.g., SUCCESS, CONTENT_FILTERED)\n            String finishReason = response.header(\"finish-reason\", \"SUCCESS\");\n            // Read the seed header\n            String seedHeader = response.header(\"seed\");\n\n            byte[] imageBytes = body.bytes();\n            log.info(","sourceCodeStart":111,"sourceCodeEnd":147,"githubUrl":"https://github.com/conductor-oss/conductor/blob/cf7c3e4a8adfb158be778ab1ec525323c363cd3a/ai/src/main/java/org/conductoross/conductor/ai/providers/stabilityai/StabilityAiApi.java#L111-L147","documentation":"Thrown by StabilityAiApi.generateImage when the v2beta image-generation endpoint returns non-2xx. IOException carries the status code and the error body string. The endpoint is selected by model name (sd3/core/ultra) and the request is multipart/form-data with Accept: image/*.","triggerScenarios":"Invalid or missing API key (401), insufficient credits (402), rate limiting (429), bad params such as an unsupported aspect_ratio/seed/out-of-range (400), content-filtered prompt (400), or a model name that resolves to an endpoint the key cannot access.","commonSituations":"Expired STABILITY_API_KEY, depleted credits (402 is Stability-specific), unsupported style_preset value, an aspect_ratio not in the allowed set, or a prompt tripping content moderation.","solutions":["Read the %s errorBody: Stability returns JSON with an errors array naming the field at fault.","Confirm STABILITY_API_KEY is set and the account has credits (watch for 402).","Validate aspect_ratio against the allowed set and seed range 0-4294967294 before calling.","For 429, apply backoff; for 402, top up credits.","Ensure the model name maps to the intended endpoint (sd3/core/ultra)."],"exampleFix":null,"handlingStrategy":"try-catch","validationCode":"if (StringUtils.isBlank(params.prompt())) {\n    throw new IllegalArgumentException(\"Image prompt is required\");\n}\nSet<String> validRatios = Set.of(\"1:1\",\"16:9\",\"9:16\",\"3:2\",\"2:3\",\"4:5\",\"5:4\",\"21:9\",\"9:21\");\nif (params.aspectRatio() != null && !validRatios.contains(params.aspectRatio())) {\n    throw new IllegalArgumentException(\"Unsupported aspect_ratio: \" + params.aspectRatio());\n}\nif (params.seed() != null && (params.seed() < 0 || params.seed() > 4294967294L)) {\n    throw new IllegalArgumentException(\"seed must be 0-4294967294\");\n}","typeGuard":null,"tryCatchPattern":"try {\n    ImageResult img = api.generateImage(params);\n} catch (IOException e) {\n    String msg = e.getMessage();\n    if (msg.contains(\"status 402\")) {\n        // insufficient credits - top up\n    } else if (msg.contains(\"status 429\")) {\n        // rate limited - back off\n    } else if (msg.contains(\"status 401\")) {\n        // bad key - do not retry\n    } else {\n        throw e;\n    }\n}","preventionTips":["Pre-validate aspect_ratio against the allowed set and seed range.","Monitor Stability credit balance (watch for 402).","Add backoff for 429 responses.","Inspect the errorBody JSON for the specific field-level error."],"tags":["stabilityai","image-generation","http","api-key","credits"],"backgroundTag":null,"analyzedSha":"cf7c3e4a8adfb158be778ab1ec525323c363cd3a","analyzedAt":"2026-08-14T03:33:19.897Z","schemaVersion":2},"datasetVersion":"2026-08-14T05:17:29.042Z"}