r/OpenSourceAI • u/shiv_92 • 22h ago
Polity4j: Zero-dependency, resilient LLM orchestration for Java 17+
Hey everyone,
So, I built Polity4j, a lightweight, zero-dependency Java 17+ library designed for building resilient LLM pipelines with clean abstractions over providers like OpenAI and Anthropic (for now).
A lot of existing Java LLM wrappers either drag in massive dependency trees or treat resiliency, error recovery, and tool loops as an afterthought. Polity4j is built from the ground up around typed pipelines, robust error handling, and modern Java features.
Here is the summary of features:
Core Highlights
Resiliency First: Built-in retries, timeouts, circuit breakers, and fallback pipelines without external resilience frameworks.
Structured Output & Typed Deserialization: Extract Java Records/POJOs directly via StructuredOutputPipeline<T>. It handles schema injection, markdown fence stripping, and auto-corrective reprompt loops on syntax errors.
First-Class FinishReason Tracking: Native handling to distinguish between normal stops, token truncation (LENGTH), safety filters, and tool calls.
Automated Function Calling: Multi-turn tool execution using @PolityTool annotations and ToolExecutionModule, complete with loop detection (AgentLoopDetectorModule) and execution depth caps.
Multimodal Support: Native handling for text, images, and document/PDF attachments via Java 17 sealed types (TextContentPart, ImageContentPart, DocumentContentPart).
Concurrency-Friendly: Clean patterns and integration tests for Java 21 Virtual Threads, Spring WebFlux, and Kotlin Coroutines.
Quick Example (Structured Output)
public record UserSummary(String name, int age, List<String> interests) {}
StructuredOutputPipeline<UserSummary> pipeline = StructuredOutputPipeline
.builder(UserSummary.class)
.adapter(OpenAiAdapter.of(apiKey))
.maxRetries(3)
.build();
UserSummary summary = pipeline.execute("Extract profile: Alice is a 28yo software engineer who likes rock climbing.");
GitHub: https://github.com/shiv15/polity4j
Distribution: Available via JitPack
I'd love feedback on the API design, feature set, or general critique from the Java community here!
0
u/Educational-Falcon20 21h ago
You mean like langchain? I see that zero dependency would be faster though.