AI / LangChain4j interview questions
How do you handle LLM output parsing failures gracefully in LangChain4j?
When LangChain4j requests structured output (returning a POJO from an AI Services method), the LLM occasionally produces malformed JSON despite format instructions — especially with smaller models or complex schemas. Without explicit error handling, this surfaces as a OutputParsingException or JsonParseException from Jackson. Graceful handling is critical for production reliability.
There are three layers where you can handle parsing failures:
1. Return Optional to signal missing/failed results:
interface ReviewExtractor {
Optional<ProductReview> extractReview(String rawText);
}
// Returns Optional.empty() if parsing fails (safer than exception-based control flow)2. Catch OutputParsingException at the call site and fall back:
try {
ProductReview review = extractor.extractReview(text);
return review;
} catch (OutputParsingException e) {
log.warn("Failed to parse review structure: {}. Falling back to raw text.", e.getMessage());
return ProductReview.unparsed(text); // your fallback model
}3. Retry with an explicit correction prompt:
@Retryable(retryFor = OutputParsingException.class, maxAttempts = 2)
ProductReview extractWithRetry(String text) {
return extractor.extractReview(text);
}Reducing parsing failures proactively:
- Use providers with native JSON mode (OpenAI's
response_format: json_object) — configure viaOpenAiChatModelNameand setresponseFormaton the model builder - Add few-shot examples of correct JSON structure in the system message
- Use simpler schemas — fewer fields, no deeply nested objects, enums instead of free-text strings for constrained values
- Use a more capable model for extraction tasks where schema adherence is critical
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...
