AI / LangChain4j interview questions
How does LangChain4j handle structured output from LLMs?
Structured output means getting the LLM to return data that maps directly to a Java object — a POJO, record, enum, or collection — rather than free-form text that you parse yourself. LangChain4j makes this transparent: declare the return type of your AI Services method as the desired Java type, and the library handles everything else.
Internally, LangChain4j uses one of two strategies depending on the provider:
- JSON schema injection — For models that do not natively support constrained output, LangChain4j generates a JSON schema from the return type and appends it to the prompt as instructions (e.g., "respond only in this JSON format"). The response is then deserialized using Jackson.
- Native JSON mode / response format — For providers that support constrained JSON output (OpenAI's
response_format: { type: json_object }or Anthropic's tool-use-for-structured-output), LangChain4j activates the native mode for more reliable output.
record ProductReview(
String productName,
int ratingOutOf5,
List<String> pros,
List<String> cons
) {}
interface ReviewAnalyzer {
@UserMessage("Analyze this customer review and extract key information: {{review}}")
ProductReview analyze(String review);
}
// Returns a fully populated ProductReview object
ProductReview result = analyzer.analyze("Great laptop, very fast but battery life is poor");
System.out.println(result.ratingOutOf5()); // e.g., 4Enums work too: if you return an enum Sentiment { POSITIVE, NEUTRAL, NEGATIVE }, LangChain4j instructs the model to return exactly one of those values and maps the response to the correct enum constant. For complex nested objects and lists, Jackson handles the deserialization as long as the model produces valid JSON matching the schema.
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...
