03-structured-outputs

Get structured data from the model instead of free text: the method returns a Java record.

Source on GitHub · Back to portfolio

03 - Structured outputs

Get structured data from the model instead of free text: the method returns a Java record.

What it demonstrates

• A `record` as the return type of an AI Service method.

• Instructions in `@SystemMessage` that describe each field.

• `temperature(0.0)` to make the extraction more deterministic.

Things to observe

• If the model returns something that cannot be mapped to the record, LangChain4J raises an error. Handling that case (retry, validation, or a fallback) is the next step after this example.

Requirements

• JDK 21

• Maven 3.9 or later

• An Anthropic API key

Run

export ANTHROPIC_API_KEY=your-key-here
mvn -q compile exec:java

Author

Jose Said Olano Garcia