Spring / Spring AI interview questions
What is the Spring AI content moderation strategy and how do you implement it?
Spring AI does not ship a built-in content moderation system, but the framework provides the right extension points — primarily Advisors — to implement moderation as a pre- and post-processing step in the ChatClient pipeline. This keeps moderation logic reusable and decoupled from business code.
There are two moderation approaches:
1. Provider moderation API (e.g. OpenAI Moderation endpoint) — Call the moderation API before sending user input to the chat model. If flagged, throw an exception or return a safe fallback response without ever calling the LLM.
@Component
public class ModerationAdvisor implements RequestResponseAdvisor {
private final OpenAiModerationModel moderationModel;
@Override
public AdvisedRequest adviseRequest(AdvisedRequest request, Map<String, Object> context) {
String userInput = request.userText();
ModerationResponse moderation = moderationModel.moderate(userInput);
if (moderation.isFlagged()) {
throw new ContentPolicyViolationException(
"Input violated content policy: " + moderation.categories());
}
return request;
}
@Override
public ChatClientResponse adviseResponse(ChatClientResponse response, Map<String, Object> context) {
return response; // optionally scan the output too
}
}2. LLM-based self-moderation (SafeGuardAdvisor) — Spring AI's built-in SafeGuardAdvisor sends the user message to a second LLM prompt that evaluates safety, then blocks or passes the original request. This is more flexible (works with any provider) but adds an extra model call per request.
Invest now in Acorns!!! 🚀
Join Acorns and get your $5 bonus!
Acorns is a micro-investing app that automatically invests your "spare change" from daily purchases into diversified, expert-built portfolios of ETFs. It is designed for beginners, allowing you to start investing with as little as $5. The service automates saving and investing. Disclosure: I may receive a referral bonus.
Invest now!!! Get Free equity stock (US, UK only)!
Use Robinhood app to invest in stocks. It is safe and secure. Use the Referral link to claim your free stock when you sign up!.
The Robinhood app makes it easy to trade stocks, crypto and more.
Webull! Receive free stock by signing up using the link: Webull signup.
More Related questions...
