This repository uses Gradle with Kotlin DSL for building and dependency management. The SDK requires Java 8, but development requires JDK 21 for the Kotlin toolchain.
The SDK consists of three artifacts:
openai-java-core- Contains core SDK logic
- Does not depend on OkHttp
- Exposes
OpenAIClient,OpenAIClientAsync,OpenAIClientImpl, andOpenAIClientAsyncImpl, all of which can work with any HTTP client
openai-java-client-okhttp- Depends on OkHttp
- Exposes
OpenAIOkHttpClientandOpenAIOkHttpClientAsync, which provide a way to constructOpenAIClientImplandOpenAIClientAsyncImpl, respectively, using OkHttp
openai-java- Depends on and exposes the APIs of both
openai-java-coreandopenai-java-client-okhttp - Does not have its own logic
- Depends on and exposes the APIs of both
Most of the SDK is generated code. Modifications to code will be persisted between generations, but may
result in merge conflicts between manual patches and changes from the generator. The generator will never
modify the contents of the openai-java-example/ directory.
All files in the openai-java-example/ directory are not modified by the generator and can be freely edited or added to.
// openai-java-example/src/main/java/com/openai/example/YourExample.java
package com.openai.example;
public class YourExample {
public static void main(String[] args) {
// ...
}
}$ ./gradlew :openai-java-example:run -PmainClass=com.openai.example.YourExampleIf you'd like to use the repository from source, you can either install from git or link to a cloned repository.
To use a local version of this library from source in another project, you can publish it to your local Maven repository:
$ ./gradlew publishToMavenLocalNote
For now, to publish locally, you'll need to comment out the line for signAllPublications() here: buildSrc/src/main/kotlin/openai.publish.gradle.kts
Then in your project's build.gradle.kts or pom.xml, reference the locally published version:
implementation("com.openai:openai-java:2.9.1")<dependency>
<groupId>com.openai</groupId>
<artifactId>openai-java</artifactId>
<version>2.9.1</version>
</dependency>Alternatively, you can build and install the JAR files directly:
$ ./gradlew buildJAR files will be available in each module's build/libs/ directory.
Most tests require our mock server to be running against the OpenAPI spec to work.
The test script will automatically start the mock server for you (if it's not already running) and run the tests against it:
$ ./scripts/testYou can also manually start the mock server if you want to run tests repeatedly:
$ ./scripts/mockThen run the tests:
$ ./scripts/test
- Tests run in parallel for better performance
- Mock server runs on
localhost:4010 - You can disable mock server tests with
SKIP_MOCK_TESTS=true - You can target a custom API URL with
TEST_API_BASE_URL=<url>
The project uses:
- JUnit 5 for test framework
- Mockito for mocking
- AssertJ for fluent assertions
- WireMock for HTTP service mocking
- Custom TestServerExtension for mock server management
This repository uses Spotless with Palantir Java Format for code formatting and various linting tools.
To check formatting and run lints:
$ ./scripts/lintThis will compile all modules and run static analysis checks.
To fix all formatting issues automatically:
$ ./scripts/formatYou can also check formatting directly with Gradle:
$ ./gradlew spotlessCheck # Check formattingTo build all modules:
$ ./gradlew buildTo build a specific module:
$ ./gradlew :openai-java-core:buildAll files in the openai-java-example/ directory are not modified by the generator and can be freely edited or added to.
// add an example to openai-java-example/src/main/java/com/openai/example/<YourExample>.java
package com.openai.example;
public class YourExample {
public static void main(String[] args) {
// ...
}
}Changes made to this repository via the automated release PR pipeline should publish to Maven Central automatically. If the changes aren't made through the automated pipeline, you may want to make releases manually.
You can release to package managers by using the Publish Sonatype GitHub action. This requires setup organization or repository secrets to be configured.
If you need to manually release a package, you can run:
$ ./gradlew publishToSonatype closeAndReleaseSonatypeStagingRepositoryThis requires the following environment variables to be set:
SONATYPE_USER- Your Sonatype Central Portal usernameSONATYPE_PASSWORD- Your Sonatype Central Portal passwordGPG_SIGNING_KEY- Your GPG private key for signing artifactsGPG_SIGNING_PASSWORD- Your GPG key passphrase
Some useful Gradle tasks:
$ ./gradlew tasks # List all available tasks
$ ./gradlew build # Build all modules
$ ./gradlew test # Run all tests
$ ./gradlew spotlessApply # Format code
$ ./gradlew publishToMavenLocal # Publish to local Maven repository
$ ./gradlew dependencies # Show dependency tree