llm4j-schema brings structured LLM output to Java via Records
KarolannMauger's `llm4j-schema` library lets Java developers extract type-safe, structured data from LLM responses using Java 21 Records, automatic JSON Schema generation, and built-in retry logic — no manual parsing required.
Score breakdown
Java developers integrating LLMs can drop brittle string-parsing logic entirely and replace it with annotated Records, letting `llm4j-schema` handle schema generation, deserialization, and retries automatically.
- 01KarolannMauger authored `llm4j-schema`, a Java library for structured, type-safe LLM output extraction using Java 21 Records.
- 02The library auto-generates a JSON Schema from Record classes via reflection and injects it as a system prompt.
- 03Jackson handles typed deserialization of the LLM's JSON response directly into the Record.
KarolannMauger published `llm4j-schema`, a Java library designed to eliminate the brittle string-parsing boilerplate that plagues LLM integrations in Java. The core idea mirrors what Python libraries like Instructor have offered for years: developers declare a Java 21 Record annotated with `@LLMSchema` and optional `@FieldDescription` metadata, and the library's `LLMExtractor` class handles everything else — reading the Record via reflection to generate a JSON Schema, injecting that schema as a system prompt, deserializing the LLM's JSON response using Jackson, and automatically retrying up to 3 times if the output is malformed or wrapped in markdown fences.
The library ships with built-in `OpenAIClient` and `AnthropicClient` implementations, and also accepts any custom `LLMClient` via a simple functional interface.
The library ships with built-in `OpenAIClient` and `AnthropicClient` implementations, and also accepts any custom `LLMClient` via a simple functional interface. A dedicated Spring Boot starter (`llm4j-schema-spring-boot-starter 0.1.0`) reduces setup to adding a Maven dependency and setting `llm4j.provider` and `llm4j.api-key` in `application.yml` — switching between OpenAI and Anthropic requires changing only those two config values, with zero changes to Java code. Both the core module (`llm4j-schema-core 0.1.0`) and the Spring Boot starter are available on Maven Central today.
The `v0.2.0` roadmap includes nested Record support, list field extraction, validation annotations (`@NotNull`, `@Range`), async extraction via `CompletableFuture`, and Ollama support for local LLM inference.
Key facts
- 01KarolannMauger authored `llm4j-schema`, a Java library for structured, type-safe LLM output extraction using Java 21 Records.
- 02The library auto-generates a JSON Schema from Record classes via reflection and injects it as a system prompt.
- 03Jackson handles typed deserialization of the LLM's JSON response directly into the Record.
- 04Failed or malformed responses are automatically retried up to 3 times, including cleanup of markdown-wrapped JSON.
- 05A Spring Boot starter (`llm4j-schema-spring-boot-starter 0.1.0`) enables integration with minimal configuration.
- 06Switching LLM providers between OpenAI and Anthropic requires only a one-line change in `application.yml`.
- 07Version `0.2.0` roadmap includes nested Records, list fields, validation annotations, async extraction, and Ollama support.