{"record":{"id":"9e883d77a26ffdcf","repo":"iflytek/astron-agent","slug":"open-ai-api-error","errorCode":"OPEN_AI_API_ERROR","errorMessage":"OPEN_AI_API_ERROR","messagePattern":"OPEN_AI_API_ERROR","errorType":"error_code","errorClass":"BusinessException","httpStatus":null,"severity":"error","filePath":"console/backend/toolkit/src/main/java/com/iflytek/astron/console/toolkit/service/bot/OpenAiModelProcessService.java","lineNumber":64,"sourceCode":"            OpenAIClient client = buildClient(config);\n            // Build request parameters\n            ChatCompletionCreateParams params = ChatCompletionCreateParams.builder()\n                    .model(config.getModel())\n                    .addUserMessage(prompt)\n                    .build();\n\n            // Call API\n            ChatCompletion completion = client.chat().completions().create(params);\n\n            // Extract response content\n            String content = completion.choices().getFirst().message().content().orElse(\"\");\n\n            log.info(\"Non-streaming call completed, response length: {}\", content.length());\n            return content;\n\n        } catch (Exception e) {\n            log.error(\"Non-streaming OpenAI API call failed\", e);\n            throw new BusinessException(ResponseEnum.OPEN_AI_API_ERROR);\n        }\n    }\n\n    /**\n     * Streaming call to OpenAI API\n     *\n     * @param prompt User input prompt\n     * @return SseEmitter object for real-time streaming response data\n     */\n    public SseEmitter processStreaming(String prompt) {\n        log.info(\"Starting streaming OpenAI API call, prompt: {}\", prompt);\n        PlatformAccountConfigDto.AiAbilityChatConfig config = platformAccountService.requireAiAbilityChat();\n        OpenAIClient client = buildClient(config);\n\n        // Create SseEmitter\n        SseEmitter emitter = SseEmitterUtil.createSseEmitter();\n        String streamId = UUID.randomUUID().toString();\n        String chatId = UUID.randomUUID().toString();","sourceCodeStart":46,"sourceCodeEnd":82,"githubUrl":"https://github.com/iflytek/astron-agent/blob/5e758547a83371a5a4b29dadf4ac03e8dd527635/console/backend/toolkit/src/main/java/com/iflytek/astron/console/toolkit/service/bot/OpenAiModelProcessService.java#L46-L82","documentation":"OpenAiModelProcessService.processNonStreaming catches any exception thrown during the non-streaming OpenAI-compatible chat completion call and rethrows it as a BusinessException with code OPEN_AI_API_ERROR. The original exception (network, timeout, auth, model error, response parsing) is logged but its detail is not carried into the business exception.","triggerScenarios":"The OpenAI-compatible HTTP request fails for any reason inside processNonStreaming: invalid/missing API key (401), unknown model name (404/400), rate limit (429), network/timeout to the endpoint, or malformed response that breaks content extraction.","commonSituations":"Wrong OPENAI base URL or key in configuration; model name not available on the configured endpoint; quota exhausted; egress firewall blocking the LLM endpoint; long prompts exceeding the model context or request timeout.","solutions":["Check the error log 'Non-streaming OpenAI API call failed' for the root cause (status code, message).","Verify the API key and base URL configuration for the model provider.","Confirm the requested model name exists and is enabled on the endpoint.","Handle 429 rate limits with retry/backoff and increase the request timeout for long completions.","Preserve the cause in the thrown exception for easier diagnosis."],"exampleFix":"// before\nthrow new BusinessException(ResponseEnum.OPEN_AI_API_ERROR);\n\n// after\nthrow new BusinessException(ResponseEnum.OPEN_AI_API_ERROR, e);","handlingStrategy":"retry","validationCode":"// Pre-flight checks before the LLM call\nAssert.hasText(apiKey, \"OpenAI API key must be set\");\nAssert.hasText(baseUrl, \"OpenAI base URL must be set\");\nAssert.hasText(model, \"model name must be set\");","typeGuard":null,"tryCatchPattern":"try {\n    return openAiModelProcessService.processNonStreaming(req);\n} catch (BusinessException e) {\n    if (ResponseEnum.OPEN_AI_API_ERROR.getCode().equals(e.getCode())) {\n        // inspect cause/log for 401/429/timeout; retry 429 with backoff, fail fast on 401\n        return fallbackAnswer(req);\n    }\n    throw e;\n}","preventionTips":["Validate key/base URL/model config at startup with a smoke completion call.","Implement exponential backoff for 429/timeout responses.","Set request timeouts proportional to prompt size and cap context length."],"tags":["openai","llm","api","java","http"],"backgroundTag":"api-request-failed","analyzedSha":"5e758547a83371a5a4b29dadf4ac03e8dd527635","analyzedAt":"2026-09-12T08:03:51.356Z","contentChangedAt":"2026-09-12T08:03:51.356Z","schemaVersion":2},"datasetVersion":"2026-09-15T23:17:13.987Z"}