This repository demonstrates how to reliably get JSON responses from LLMs using LangChain4j ChatModel Structured Output, enforce strict JSON schemas, and safely handle AI responses in production systems.
π Complete Guide: For detailed explanations and a full code walkthrough, read our comprehensive tutorial.
π LangChain4j ChatModel Structured Output
π₯ Video Tutorial: Prefer hands-on learning? Watch our step-by-step implementation guide.
π YouTube Tutorial - LangChain4j Structured Output: Stop Getting Broken JSON from AI Models!
This application serves as a deep dive into Structured Outputs, covering different strategies to extract data:
- Text Fallback - Handling scenarios where models do not support structured output natively.
- Generic JSON Mode - Getting loose JSON data without a pre-defined schema.
- Strict Schema Enforcement - Using JSON Schema to force the model to adhere to shared structure.
- Raw JSON Schemas - Generating structured output by passing raw JSON Schema strings directly to the model.
To run this application, you will need the following:
- OpenRouter API Key: This project uses OpenRouter to access free AI models (DeepSeek, Llama, etc.) via OpenAI-compatible endpoints.
- Sign up at OpenRouter.ai to generate your key.
- Setup Environment Variables: Set your API key as an environment variable:
# Set your OpenRouter API Key
export OPENROUTER_API_KEY=your_api_key_hereFor detailed instructions on how to set up, configure, and test the application, kindly go through our comprehensive article:
π Click here for Setup & Testing Instructions