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., 4
Enums 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.
More Related questions...