{"record":{"id":"d63dd9ba1b2672d8","repo":"spring-projects/spring-ai","slug":"no-moderation-response-returned","errorCode":null,"errorMessage":"No moderation response returned","messagePattern":"No moderation response returned","errorType":"console","errorClass":null,"httpStatus":null,"severity":"warning","filePath":"models/spring-ai-openai/src/main/java/org/springframework/ai/openai/OpenAiModerationModel.java","lineNumber":136,"sourceCode":"\t}\n\n\t/**\n\t * Creates a RequestOptions instance from the given moderation options.\n\t * @param options the moderation options\n\t * @return a RequestOptions instance\n\t */\n\tprivate RequestOptions buildRequestOptions(OpenAiModerationOptions options) {\n\t\tAssert.notNull(options, \"Options cannot be null\");\n\t\tRequestOptions.Builder requestOptionsBuilder = RequestOptions.builder();\n\t\tif (options.getTimeout() != null) {\n\t\t\trequestOptionsBuilder.timeout(options.getTimeout());\n\t\t}\n\t\treturn requestOptionsBuilder.build();\n\t}\n\n\tprivate ModerationResponse convertResponse(ModerationCreateResponse response) {\n\t\tif (response == null) {\n\t\t\tlogger.warn(\"No moderation response returned\");\n\t\t\treturn new ModerationResponse(null);\n\t\t}\n\n\t\tList<ModerationResult> moderationResults = new ArrayList<>();\n\n\t\tfor (com.openai.models.moderations.Moderation result : response.results()) {\n\t\t\tCategories categories = Categories.builder()\n\t\t\t\t.sexual(result.categories().sexual())\n\t\t\t\t.hate(result.categories().hate())\n\t\t\t\t.harassment(result.categories().harassment())\n\t\t\t\t.selfHarm(result.categories().selfHarm())\n\t\t\t\t.sexualMinors(result.categories().sexualMinors())\n\t\t\t\t.hateThreatening(result.categories().hateThreatening())\n\t\t\t\t.violenceGraphic(result.categories().violenceGraphic())\n\t\t\t\t.selfHarmIntent(result.categories().selfHarmIntent())\n\t\t\t\t.selfHarmInstructions(result.categories().selfHarmInstructions())\n\t\t\t\t.harassmentThreatening(result.categories().harassmentThreatening())\n\t\t\t\t.violence(result.categories().violence())","sourceCodeStart":118,"sourceCodeEnd":154,"githubUrl":"https://github.com/spring-projects/spring-ai/blob/98a7beda4f29d80a71c5837eb4053b03a93a46f7/models/spring-ai-openai/src/main/java/org/springframework/ai/openai/OpenAiModerationModel.java#L118-L154","documentation":"OpenAiModerationModel.convertResponse expects a ModerationCreateResponse from the OpenAI moderation API. If the response object is null, the model logs a warning and returns an empty ModerationResponse(null) rather than throwing, since there are no moderation results to map. This indicates the API returned no usable moderation payload.","triggerScenarios":"Calling OpenAiModerationModel.call(...) when the OpenAI client returns a null ModerationCreateResponse — e.g. empty body, proxy/gateway stripping the response, or an unmodeled error path in the SDK.","commonSituations":"Network/gateway issues where a 200 with empty body is returned; using a compatible endpoint that doesn't implement /v1/moderations correctly; transient API outages.","solutions":["Log/inspect the raw HTTP exchange (enable DEBUG for the OpenAI client) to see what the API actually returned","Verify the endpoint actually supports the moderations API and returns a results array","Check network/proxy configuration and retry the call","Handle the returned empty ModerationResponse defensively: check its results before flagging content"],"exampleFix":"// before\nModerationResponse resp = moderationModel.call(new ModerationPrompt(text));\nboolean flagged = resp.getResults().get(0).isFlagged(); // empty results\n// after\nModerationResponse resp = moderationModel.call(new ModerationPrompt(text));\nif (resp.getResults() == null || resp.getResults().isEmpty()) {\n    throw new IllegalStateException(\"Moderation API returned no results\");\n}","handlingStrategy":"try-catch","validationCode":"ModerationResponse resp = moderationModel.call(prompt);\nif (resp == null || resp.getResults() == null || resp.getResults().isEmpty()) {\n    throw new IllegalStateException(\"No moderation results returned\");\n}","typeGuard":null,"tryCatchPattern":"try {\n    ModerationResponse resp = moderationModel.call(new ModerationPrompt(text));\n    if (resp.getResults().isEmpty()) throw new IllegalStateException(\"empty moderation results\");\n} catch (RuntimeException e) {\n    // fail-open or fail-closed policy for moderation\n}","preventionTips":["Verify endpoint supports /moderations","Check gateway/proxy behavior for empty bodies","Defensively check results before flagging"],"tags":["openai","moderation","null-response"],"backgroundTag":"empty-api-response","analyzedSha":"98a7beda4f29d80a71c5837eb4053b03a93a46f7","analyzedAt":"2026-09-11T14:15:49.441Z","contentChangedAt":"2026-09-11T14:15:49.441Z","schemaVersion":2},"datasetVersion":"2026-09-14T11:17:12.474Z"}