Repolode
openai

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. 1

    Add the Gradle dependency

    implementation("com.openai:openai-java:4.90.0")
  2. 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