A Python library that gets structured, Pydantic-validated data from LLMs across multiple providers, with automatic retries on validation failure.
openai/openai-java
Official Java and Kotlin SDK for the OpenAI REST API, with sync and async clients, streaming, and typed request and response classes.
- Stars
- 1,554
- Forks
- 272
- Open issues
- 66
- Last commit
- 6 hours ago
What is it?
openai-java is the official Java library for calling the OpenAI API from Java applications. It wraps the REST endpoints in typed builders and response classes, so developers work with Java objects rather than hand-written JSON. The main entry point is the Responses API, with the Chat Completions API also supported.
The client is available in synchronous and asynchronous forms, and streaming methods return chunk streams that can be processed as data arrives. Helper classes accumulate streamed chat completions and responses back into complete objects. Structured outputs can be derived automatically from plain Java classes, with optional Jackson and Swagger annotations to shape the generated JSON schema.
The SDK also covers configuration through system properties or environment variables, workload identity authentication for Kubernetes, Azure and GCP, and an optional artifact for calling OpenAI-compatible APIs on Amazon Bedrock.
Key features
- 01Typed Java builders and response classes for the Responses and Chat Completions APIs.
- 02Synchronous and asynchronous clients, with async methods returning CompletableFuture.
- 03Streaming via SSE with helpers that accumulate chunks into complete responses.
- 04Structured outputs generated from Java classes, with local JSON schema validation.
- 05Workload identity authentication for Kubernetes, Azure and GCP.
Use cases
- Adding OpenAI-powered text generation to a Java or Kotlin backend service.
- Streaming model output to a web or desktop client as it is generated.
- Extracting structured data from model responses into Java domain objects.
- Calling OpenAI-compatible models on Amazon Bedrock from a Java application.
Quick start
- 1
Add the Gradle dependency
implementation("com.openai:openai-java:4.90.0") - 2
Create a client from environment variables
OpenAIClient client = OpenAIOkHttpClient.fromEnv();
Things to know
- The README's examples use the OPENAI_API_KEY environment variable, so you need an OpenAI API key to make requests.
Topics
FAQ
- What is openai-java?
- Official Java and Kotlin SDK for the OpenAI REST API, with sync and async clients, streaming, and typed request and response classes. openai-java is the official Java library for calling the OpenAI API from Java applications. It wraps the REST endpoints in typed builders and response classes, so developers work with Java objects rather than hand-written JSON. The main entry point is the Responses API, with the Chat Completions API also supported.
- Is openai-java free to use? What license does it have?
- openai-java is open source under the Apache-2.0 license. Check the license terms before using it in commercial products.
- Is openai-java still maintained?
- Actively maintained. The last commit to openai-java was on Oct 22, 2026.
- What can I use openai-java for?
- Adding OpenAI-powered text generation to a Java or Kotlin backend service. Streaming model output to a web or desktop client as it is generated. Extracting structured data from model responses into Java domain objects. Calling OpenAI-compatible models on Amazon Bedrock from a Java application.
- How do I install openai-java?
- Add the Gradle dependency: implementation("com.openai:openai-java:4.90.0") Create a client from environment variables: OpenAIClient client = OpenAIOkHttpClient.fromEnv();
- Where can I get openai-java?
- The source code and installation instructions are on GitHub: https://github.com/openai/openai-java
Summary generated by AI from the project's README. GitHub stats refresh daily.
Related projects
Official TypeScript SDK for calling Anthropic's Claude API from server-side JavaScript and TypeScript applications.
An SDK for building AI agents and multi-agent systems in Python, .NET and Java, with model, plugin and vector database integrations.
The official Python library for the OpenAI REST API, with sync and async clients and typed request and response models.
Open source autonomous coding agent that runs as an IDE extension, terminal CLI, desktop app, or SDK.
TypeScript and Python SDK giving AI agents 1000+ pre-authenticated app toolkits, per-user sessions, and a sandbox to take actions.