spring-projects/spring-ai · warning

No moderation response returned

Error message

No moderation response returned

What it means

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.

Source

Thrown at models/spring-ai-openai/src/main/java/org/springframework/ai/openai/OpenAiModerationModel.java:136

	}

	/**
	 * Creates a RequestOptions instance from the given moderation options.
	 * @param options the moderation options
	 * @return a RequestOptions instance
	 */
	private RequestOptions buildRequestOptions(OpenAiModerationOptions options) {
		Assert.notNull(options, "Options cannot be null");
		RequestOptions.Builder requestOptionsBuilder = RequestOptions.builder();
		if (options.getTimeout() != null) {
			requestOptionsBuilder.timeout(options.getTimeout());
		}
		return requestOptionsBuilder.build();
	}

	private ModerationResponse convertResponse(ModerationCreateResponse response) {
		if (response == null) {
			logger.warn("No moderation response returned");
			return new ModerationResponse(null);
		}

		List<ModerationResult> moderationResults = new ArrayList<>();

		for (com.openai.models.moderations.Moderation result : response.results()) {
			Categories categories = Categories.builder()
				.sexual(result.categories().sexual())
				.hate(result.categories().hate())
				.harassment(result.categories().harassment())
				.selfHarm(result.categories().selfHarm())
				.sexualMinors(result.categories().sexualMinors())
				.hateThreatening(result.categories().hateThreatening())
				.violenceGraphic(result.categories().violenceGraphic())
				.selfHarmIntent(result.categories().selfHarmIntent())
				.selfHarmInstructions(result.categories().selfHarmInstructions())
				.harassmentThreatening(result.categories().harassmentThreatening())
				.violence(result.categories().violence())

View on GitHub (pinned to 98a7beda4f)

Solutions

  1. Log/inspect the raw HTTP exchange (enable DEBUG for the OpenAI client) to see what the API actually returned
  2. Verify the endpoint actually supports the moderations API and returns a results array
  3. Check network/proxy configuration and retry the call
  4. Handle the returned empty ModerationResponse defensively: check its results before flagging content

Example fix

// before
ModerationResponse resp = moderationModel.call(new ModerationPrompt(text));
boolean flagged = resp.getResults().get(0).isFlagged(); // empty results
// after
ModerationResponse resp = moderationModel.call(new ModerationPrompt(text));
if (resp.getResults() == null || resp.getResults().isEmpty()) {
    throw new IllegalStateException("Moderation API returned no results");
}
Defensive patterns

Strategy: try-catch

Validate before calling

ModerationResponse resp = moderationModel.call(prompt);
if (resp == null || resp.getResults() == null || resp.getResults().isEmpty()) {
    throw new IllegalStateException("No moderation results returned");
}

Try / catch

try {
    ModerationResponse resp = moderationModel.call(new ModerationPrompt(text));
    if (resp.getResults().isEmpty()) throw new IllegalStateException("empty moderation results");
} catch (RuntimeException e) {
    // fail-open or fail-closed policy for moderation
}

Prevention

When it happens

Trigger: 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.

Common situations: 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.

Understand the failure class

Background: "empty response", "returned no data", "empty embeddings": what HTTP 200-with-empty-body errors mean across libraries — this error's family across 36 libraries.

Related errors


AI-assisted analysis of spring-projects/spring-ai@98a7beda4f (2026-09-11). Data as JSON: /api/errors/d63dd9ba1b2672d8. Report an issue: GitHub.