openai/openai-java

Public

mirrored from https://github.com/openai/openai-javaAvailable

CodeCommitsIssuesPull requestsActionsInsightsSecurity
v2.18.0

Branches

Tags

  • No tags available.
0Branches0Tags
Go to file
Add file
Code

Clone

HTTPS

Download ZIP

README.md

1778lines · modecode

1# OpenAI Java API Library
2
3<!-- x-release-please-start-version -->
4
5[![Maven Central](https://img.shields.io/maven-central/v/com.openai/openai-java)](https://central.sonatype.com/artifact/com.openai/openai-java/2.18.0)
6[![javadoc](https://javadoc.io/badge2/com.openai/openai-java/2.18.0/javadoc.svg)](https://javadoc.io/doc/com.openai/openai-java/2.18.0)
7
8<!-- x-release-please-end -->
9
10The OpenAI Java SDK provides convenient access to the [OpenAI REST API](https://platform.openai.com/docs) from applications written in Java.
11
12<!-- x-release-please-start-version -->
13
14The REST API documentation can be found on [platform.openai.com](https://platform.openai.com/docs). Javadocs are available on [javadoc.io](https://javadoc.io/doc/com.openai/openai-java/2.18.0).
15
16<!-- x-release-please-end -->
17
18## Installation
19
20<!-- x-release-please-start-version -->
21
22[_Try `openai-java-spring-boot-starter` if you're using Spring Boot!_](#spring-boot)
23
24### Gradle
25
26```kotlin
27implementation("com.openai:openai-java:2.18.0")
28```
29
30### Maven
31
32```xml
33<dependency>
34 <groupId>com.openai</groupId>
35 <artifactId>openai-java</artifactId>
36 <version>2.18.0</version>
37</dependency>
38```
39
40<!-- x-release-please-end -->
41
42## Requirements
43
44This library requires Java 8 or later.
45
46## Usage
47
48See the [`openai-java-example`](openai-java-example/src/main/java/com/openai/example) directory for complete and runnable examples.
49
50The primary API for interacting with OpenAI models is the [Responses API](https://platform.openai.com/docs/api-reference/responses). You can generate text from the model with the code below.
51
52```java
53import com.openai.client.OpenAIClient;
54import com.openai.client.okhttp.OpenAIOkHttpClient;
55import com.openai.models.ChatModel;
56import com.openai.models.responses.Response;
57import com.openai.models.responses.ResponseCreateParams;
58
59// Configures using the `OPENAI_API_KEY`, `OPENAI_ORG_ID` and `OPENAI_PROJECT_ID` environment variables
60OpenAIClient client = OpenAIOkHttpClient.fromEnv();
61
62ResponseCreateParams params = ResponseCreateParams.builder()
63 .input("Say this is a test")
64 .model(ChatModel.GPT_4_1)
65 .build();
66Response response = client.responses().create(params);
67```
68
69The previous standard (supported indefinitely) for generating text is the [Chat Completions API](https://platform.openai.com/docs/api-reference/chat). You can use that API to generate text from the model with the code below.
70
71```java
72import com.openai.client.OpenAIClient;
73import com.openai.client.okhttp.OpenAIOkHttpClient;
74import com.openai.models.ChatModel;
75import com.openai.models.chat.completions.ChatCompletion;
76import com.openai.models.chat.completions.ChatCompletionCreateParams;
77
78// Configures using the `openai.apiKey`, `openai.orgId`, `openai.projectId`, `openai.webhookSecret` and `openai.baseUrl` system properties
79// Or configures using the `OPENAI_API_KEY`, `OPENAI_ORG_ID`, `OPENAI_PROJECT_ID`, `OPENAI_WEBHOOK_SECRET` and `OPENAI_BASE_URL` environment variables
80OpenAIClient client = OpenAIOkHttpClient.fromEnv();
81
82ChatCompletionCreateParams params = ChatCompletionCreateParams.builder()
83 .addUserMessage("Say this is a test")
84 .model(ChatModel.GPT_4_1)
85 .build();
86ChatCompletion chatCompletion = client.chat().completions().create(params);
87```
88
89## Client configuration
90
91Configure the client using system properties or environment variables:
92
93```java
94import com.openai.client.OpenAIClient;
95import com.openai.client.okhttp.OpenAIOkHttpClient;
96
97// Configures using the `openai.apiKey`, `openai.orgId`, `openai.projectId`, `openai.webhookSecret` and `openai.baseUrl` system properties
98// Or configures using the `OPENAI_API_KEY`, `OPENAI_ORG_ID`, `OPENAI_PROJECT_ID`, `OPENAI_WEBHOOK_SECRET` and `OPENAI_BASE_URL` environment variables
99OpenAIClient client = OpenAIOkHttpClient.fromEnv();
100```
101
102Or manually:
103
104```java
105import com.openai.client.OpenAIClient;
106import com.openai.client.okhttp.OpenAIOkHttpClient;
107
108OpenAIClient client = OpenAIOkHttpClient.builder()
109 .apiKey("My API Key")
110 .build();
111```
112
113Or using a combination of the two approaches:
114
115```java
116import com.openai.client.OpenAIClient;
117import com.openai.client.okhttp.OpenAIOkHttpClient;
118
119OpenAIClient client = OpenAIOkHttpClient.builder()
120 // Configures using the `openai.apiKey`, `openai.orgId`, `openai.projectId`, `openai.webhookSecret` and `openai.baseUrl` system properties
121 Or configures using the `OPENAI_API_KEY`, `OPENAI_ORG_ID`, `OPENAI_PROJECT_ID`, `OPENAI_WEBHOOK_SECRET` and `OPENAI_BASE_URL` environment variables
122 .fromEnv()
123 .apiKey("My API Key")
124 .build();
125```
126
127See this table for the available options:
128
129| Setter | System property | Environment variable | Required | Default value |
130| --------------- | ---------------------- | ----------------------- | -------- | ----------------------------- |
131| `apiKey` | `openai.apiKey` | `OPENAI_API_KEY` | true | - |
132| `organization` | `openai.orgId` | `OPENAI_ORG_ID` | false | - |
133| `project` | `openai.projectId` | `OPENAI_PROJECT_ID` | false | - |
134| `webhookSecret` | `openai.webhookSecret` | `OPENAI_WEBHOOK_SECRET` | false | - |
135| `baseUrl` | `openai.baseUrl` | `OPENAI_BASE_URL` | true | `"https://api.openai.com/v1"` |
136
137System properties take precedence over environment variables.
138
139> [!TIP]
140> Don't create more than one client in the same application. Each client has a connection pool and
141> thread pools, which are more efficient to share between requests.
142
143### Modifying configuration
144
145To temporarily use a modified client configuration, while reusing the same connection and thread pools, call `withOptions()` on any client or service:
146
147```java
148import com.openai.client.OpenAIClient;
149
150OpenAIClient clientWithOptions = client.withOptions(optionsBuilder -> {
151 optionsBuilder.baseUrl("https://example.com");
152 optionsBuilder.maxRetries(42);
153});
154```
155
156The `withOptions()` method does not affect the original client or service.
157
158## Requests and responses
159
160To send a request to the OpenAI API, build an instance of some `Params` class and pass it to the corresponding client method. When the response is received, it will be deserialized into an instance of a Java class.
161
162For example, `client.chat().completions().create(...)` should be called with an instance of `ChatCompletionCreateParams`, and it will return an instance of `ChatCompletion`.
163
164## Immutability
165
166Each class in the SDK has an associated [builder](https://blogs.oracle.com/javamagazine/post/exploring-joshua-blochs-builder-design-pattern-in-java) or factory method for constructing it.
167
168Each class is [immutable](https://docs.oracle.com/javase/tutorial/essential/concurrency/immutable.html) once constructed. If the class has an associated builder, then it has a `toBuilder()` method, which can be used to convert it back to a builder for making a modified copy.
169
170Because each class is immutable, builder modification will _never_ affect already built class instances.
171
172## Asynchronous execution
173
174The default client is synchronous. To switch to asynchronous execution, call the `async()` method:
175
176```java
177import com.openai.client.OpenAIClient;
178import com.openai.client.okhttp.OpenAIOkHttpClient;
179import com.openai.models.ChatModel;
180import com.openai.models.chat.completions.ChatCompletion;
181import com.openai.models.chat.completions.ChatCompletionCreateParams;
182import java.util.concurrent.CompletableFuture;
183
184// Configures using the `openai.apiKey`, `openai.orgId`, `openai.projectId`, `openai.webhookSecret` and `openai.baseUrl` system properties
185// Or configures using the `OPENAI_API_KEY`, `OPENAI_ORG_ID`, `OPENAI_PROJECT_ID`, `OPENAI_WEBHOOK_SECRET` and `OPENAI_BASE_URL` environment variables
186OpenAIClient client = OpenAIOkHttpClient.fromEnv();
187
188ChatCompletionCreateParams params = ChatCompletionCreateParams.builder()
189 .addUserMessage("Say this is a test")
190 .model(ChatModel.GPT_4_1)
191 .build();
192CompletableFuture<ChatCompletion> chatCompletion = client.async().chat().completions().create(params);
193```
194
195Or create an asynchronous client from the beginning:
196
197```java
198import com.openai.client.OpenAIClientAsync;
199import com.openai.client.okhttp.OpenAIOkHttpClientAsync;
200import com.openai.models.ChatModel;
201import com.openai.models.chat.completions.ChatCompletion;
202import com.openai.models.chat.completions.ChatCompletionCreateParams;
203import java.util.concurrent.CompletableFuture;
204
205// Configures using the `openai.apiKey`, `openai.orgId`, `openai.projectId`, `openai.webhookSecret` and `openai.baseUrl` system properties
206// Or configures using the `OPENAI_API_KEY`, `OPENAI_ORG_ID`, `OPENAI_PROJECT_ID`, `OPENAI_WEBHOOK_SECRET` and `OPENAI_BASE_URL` environment variables
207OpenAIClientAsync client = OpenAIOkHttpClientAsync.fromEnv();
208
209ChatCompletionCreateParams params = ChatCompletionCreateParams.builder()
210 .addUserMessage("Say this is a test")
211 .model(ChatModel.GPT_4_1)
212 .build();
213CompletableFuture<ChatCompletion> chatCompletion = client.chat().completions().create(params);
214```
215
216The asynchronous client supports the same options as the synchronous one, except most methods return `CompletableFuture`s.
217
218## Streaming
219
220The SDK defines methods that return response "chunk" streams, where each chunk can be individually processed as soon as it arrives instead of waiting on the full response. Streaming methods generally correspond to [SSE](https://developer.mozilla.org/en-US/docs/Web/API/Server-sent_events) or [JSONL](https://jsonlines.org) responses.
221
222Some of these methods may have streaming and non-streaming variants, but a streaming method will always have a `Streaming` suffix in its name, even if it doesn't have a non-streaming variant.
223
224These streaming methods return [`StreamResponse`](openai-java-core/src/main/kotlin/com/openai/core/http/StreamResponse.kt) for synchronous clients:
225
226```java
227import com.openai.core.http.StreamResponse;
228import com.openai.models.chat.completions.ChatCompletionChunk;
229
230try (StreamResponse<ChatCompletionChunk> streamResponse = client.chat().completions().createStreaming(params)) {
231 streamResponse.stream().forEach(chunk -> {
232 System.out.println(chunk);
233 });
234 System.out.println("No more chunks!");
235}
236```
237
238Or [`AsyncStreamResponse`](openai-java-core/src/main/kotlin/com/openai/core/http/AsyncStreamResponse.kt) for asynchronous clients:
239
240```java
241import com.openai.core.http.AsyncStreamResponse;
242import com.openai.models.chat.completions.ChatCompletionChunk;
243import java.util.Optional;
244
245client.async().chat().completions().createStreaming(params).subscribe(chunk -> {
246 System.out.println(chunk);
247});
248
249// If you need to handle errors or completion of the stream
250client.async().chat().completions().createStreaming(params).subscribe(new AsyncStreamResponse.Handler<>() {
251 @Override
252 public void onNext(ChatCompletionChunk chunk) {
253 System.out.println(chunk);
254 }
255
256 @Override
257 public void onComplete(Optional<Throwable> error) {
258 if (error.isPresent()) {
259 System.out.println("Something went wrong!");
260 throw new RuntimeException(error.get());
261 } else {
262 System.out.println("No more chunks!");
263 }
264 }
265});
266
267// Or use futures
268client.async().chat().completions().createStreaming(params)
269 .subscribe(chunk -> {
270 System.out.println(chunk);
271 })
272 .onCompleteFuture();
273 .whenComplete((unused, error) -> {
274 if (error != null) {
275 System.out.println("Something went wrong!");
276 throw new RuntimeException(error);
277 } else {
278 System.out.println("No more chunks!");
279 }
280 });
281```
282
283Async streaming uses a dedicated per-client cached thread pool [`Executor`](https://docs.oracle.com/javase/8/docs/api/java/util/concurrent/Executor.html) to stream without blocking the current thread. This default is suitable for most purposes.
284
285To use a different `Executor`, configure the subscription using the `executor` parameter:
286
287```java
288import java.util.concurrent.Executor;
289import java.util.concurrent.Executors;
290
291Executor executor = Executors.newFixedThreadPool(4);
292client.async().chat().completions().createStreaming(params).subscribe(
293 chunk -> System.out.println(chunk), executor
294);
295```
296
297Or configure the client globally using the `streamHandlerExecutor` method:
298
299```java
300import com.openai.client.OpenAIClient;
301import com.openai.client.okhttp.OpenAIOkHttpClient;
302import java.util.concurrent.Executors;
303
304OpenAIClient client = OpenAIOkHttpClient.builder()
305 .fromEnv()
306 .streamHandlerExecutor(Executors.newFixedThreadPool(4))
307 .build();
308```
309
310### Streaming helpers
311
312The SDK provides conveniences for streamed chat completions. A
313[`ChatCompletionAccumulator`](openai-java-core/src/main/kotlin/com/openai/helpers/ChatCompletionAccumulator.kt)
314can record the stream of chat completion chunks in the response as they are processed and accumulate
315a [`ChatCompletion`](openai-java-core/src/main/kotlin/com/openai/models/chat/completions/ChatCompletion.kt)
316object similar to that which would have been returned by the non-streaming API.
317
318For a synchronous response add a
319[`Stream.peek()`](https://docs.oracle.com/javase/8/docs/api/java/util/stream/Stream.html#peek-java.util.function.Consumer-)
320call to the stream pipeline to accumulate each chunk:
321
322```java
323import com.openai.core.http.StreamResponse;
324import com.openai.helpers.ChatCompletionAccumulator;
325import com.openai.models.chat.completions.ChatCompletion;
326import com.openai.models.chat.completions.ChatCompletionChunk;
327
328ChatCompletionAccumulator chatCompletionAccumulator = ChatCompletionAccumulator.create();
329
330try (StreamResponse<ChatCompletionChunk> streamResponse =
331 client.chat().completions().createStreaming(createParams)) {
332 streamResponse.stream()
333 .peek(chatCompletionAccumulator::accumulate)
334 .flatMap(completion -> completion.choices().stream())
335 .flatMap(choice -> choice.delta().content().stream())
336 .forEach(System.out::print);
337}
338
339ChatCompletion chatCompletion = chatCompletionAccumulator.chatCompletion();
340```
341
342For an asynchronous response, add the `ChatCompletionAccumulator` to the `subscribe()` call:
343
344```java
345import com.openai.helpers.ChatCompletionAccumulator;
346import com.openai.models.chat.completions.ChatCompletion;
347
348ChatCompletionAccumulator chatCompletionAccumulator = ChatCompletionAccumulator.create();
349
350client.chat()
351 .completions()
352 .createStreaming(createParams)
353 .subscribe(chunk -> chatCompletionAccumulator.accumulate(chunk).choices().stream()
354 .flatMap(choice -> choice.delta().content().stream())
355 .forEach(System.out::print))
356 .onCompleteFuture()
357 .join();
358
359ChatCompletion chatCompletion = chatCompletionAccumulator.chatCompletion();
360```
361
362The SDK provides conveniences for streamed responses. A
363[`ResponseAccumulator`](openai-java-core/src/main/kotlin/com/openai/helpers/ResponseAccumulator.kt)
364can record the stream of response events as they are processed and accumulate a
365[`Response`](openai-java-core/src/main/kotlin/com/openai/models/responses/Response.kt)
366object similar to that which would have been returned by the non-streaming API.
367
368For a synchronous response add a
369[`Stream.peek()`](https://docs.oracle.com/javase/8/docs/api/java/util/stream/Stream.html#peek-java.util.function.Consumer-)
370call to the stream pipeline to accumulate each event:
371
372```java
373import com.openai.core.http.StreamResponse;
374import com.openai.helpers.ResponseAccumulator;
375import com.openai.models.responses.Response;
376import com.openai.models.responses.ResponseStreamEvent;
377
378ResponseAccumulator responseAccumulator = ResponseAccumulator.create();
379
380try (StreamResponse<ResponseStreamEvent> streamResponse =
381 client.responses().createStreaming(createParams)) {
382 streamResponse.stream()
383 .peek(responseAccumulator::accumulate)
384 .flatMap(event -> event.outputTextDelta().stream())
385 .forEach(textEvent -> System.out.print(textEvent.delta()));
386}
387
388Response response = responseAccumulator.response();
389```
390
391For an asynchronous response, add the `ResponseAccumulator` to the `subscribe()` call:
392
393```java
394import com.openai.helpers.ResponseAccumulator;
395import com.openai.models.responses.Response;
396
397ResponseAccumulator responseAccumulator = ResponseAccumulator.create();
398
399client.responses()
400 .createStreaming(createParams)
401 .subscribe(event -> responseAccumulator.accumulate(event)
402 .outputTextDelta().ifPresent(textEvent -> System.out.print(textEvent.delta())))
403 .onCompleteFuture()
404 .join();
405
406Response response = responseAccumulator.response();
407```
408
409## Structured outputs with JSON schemas
410
411Open AI [Structured Outputs](https://platform.openai.com/docs/guides/structured-outputs?api-mode=chat)
412is a feature that ensures that the model will always generate responses that adhere to a supplied
413[JSON schema](https://json-schema.org/overview/what-is-jsonschema).
414
415A JSON schema can be defined by creating a
416[`ResponseFormatJsonSchema`](openai-java-core/src/main/kotlin/com/openai/models/ResponseFormatJsonSchema.kt)
417and setting it on the input parameters. However, for greater convenience, a JSON schema can instead
418be derived automatically from the structure of an arbitrary Java class. The JSON content from the
419response will then be converted automatically to an instance of that Java class. A full, working
420example of the use of Structured Outputs with arbitrary Java classes can be seen in
421[`StructuredOutputsExample`](openai-java-example/src/main/java/com/openai/example/StructuredOutputsExample.java).
422
423Java classes can contain fields declared to be instances of other classes and can use collections
424(see [Defining JSON schema properties](#defining-json-schema-properties) for more details):
425
426```java
427class Person {
428 public String name;
429 public int birthYear;
430}
431
432class Book {
433 public String title;
434 public Person author;
435 public int publicationYear;
436}
437
438class BookList {
439 public List<Book> books;
440}
441```
442
443Pass the top-level class—`BookList` in this example—to `responseFormat(Class<T>)` when building the
444parameters and then access an instance of `BookList` from the generated message content in the
445response:
446
447```java
448import com.openai.models.ChatModel;
449import com.openai.models.chat.completions.ChatCompletionCreateParams;
450import com.openai.models.chat.completions.StructuredChatCompletionCreateParams;
451
452StructuredChatCompletionCreateParams<BookList> params = ChatCompletionCreateParams.builder()
453 .addUserMessage("List some famous late twentieth century novels.")
454 .model(ChatModel.GPT_4_1)
455 .responseFormat(BookList.class)
456 .build();
457
458client.chat().completions().create(params).choices().stream()
459 .flatMap(choice -> choice.message().content().stream())
460 .flatMap(bookList -> bookList.books.stream())
461 .forEach(book -> System.out.println(book.title + " by " + book.author.name));
462```
463
464You can start building the parameters with an instance of
465[`ChatCompletionCreateParams.Builder`](openai-java-core/src/main/kotlin/com/openai/models/chat/completions/ChatCompletionCreateParams.kt)
466or
467[`StructuredChatCompletionCreateParams.Builder`](openai-java-core/src/main/kotlin/com/openai/models/chat/completions/StructuredChatCompletionCreateParams.kt).
468If you start with the former (which allows for more compact code) the builder type will change to
469the latter when `ChatCompletionCreateParams.Builder.responseFormat(Class<T>)` is called.
470
471If a field in a class is optional and does not require a defined value, you can represent this using
472the [`java.util.Optional`](https://docs.oracle.com/javase/8/docs/api/java/util/Optional.html) class.
473It is up to the AI model to decide whether to provide a value for that field or leave it empty.
474
475```java
476import java.util.Optional;
477
478class Book {
479 public String title;
480 public Person author;
481 public int publicationYear;
482 public Optional<String> isbn;
483}
484```
485
486Generic type information for fields is retained in the class's metadata, but _generic type erasure_
487applies in other scopes. While, for example, a JSON schema defining an array of books can be derived
488from the `BookList.books` field with type `List<Book>`, a valid JSON schema cannot be derived from a
489local variable of that same type, so the following will _not_ work:
490
491```java
492List<Book> books = new ArrayList<>();
493
494StructuredChatCompletionCreateParams<List<Book>> params = ChatCompletionCreateParams.builder()
495 .responseFormat(books.getClass())
496 // ...
497 .build();
498```
499
500If an error occurs while converting a JSON response to an instance of a Java class, the error
501message will include the JSON response to assist in diagnosis. For instance, if the response is
502truncated, the JSON data will be incomplete and cannot be converted to a class instance. If your
503JSON response may contain sensitive information, avoid logging it directly, or ensure that you
504redact any sensitive details from the error message.
505
506### Local JSON schema validation
507
508Structured Outputs supports a
509[subset](https://platform.openai.com/docs/guides/structured-outputs#supported-schemas) of the JSON
510Schema language. Schemas are generated automatically from classes to align with this subset.
511However, due to the inherent structure of the classes, the generated schema may still violate
512certain OpenAI schema restrictions, such as exceeding the maximum nesting depth or utilizing
513unsupported data types.
514
515To facilitate compliance, the method `responseFormat(Class<T>)` performs a validation check on the
516schema derived from the specified class. This validation ensures that all restrictions are adhered
517to. If any issues are detected, an exception will be thrown, providing a detailed message outlining
518the reasons for the validation failure.
519
520- **Local Validation**: The validation process occurs locally, meaning no requests are sent to the
521 remote AI model. If the schema passes local validation, it is likely to pass remote validation as
522 well.
523- **Remote Validation**: The remote AI model will conduct its own validation upon receiving the JSON
524 schema in the request.
525- **Version Compatibility**: There may be instances where local validation fails while remote
526 validation succeeds. This can occur if the SDK version is outdated compared to the restrictions
527 enforced by the remote AI model.
528- **Disabling Local Validation**: If you encounter compatibility issues and wish to bypass local
529 validation, you can disable it by passing
530 [`JsonSchemaLocalValidation.NO`](openai-java-core/src/main/kotlin/com/openai/core/JsonSchemaLocalValidation.kt)
531 to the `responseFormat(Class<T>, JsonSchemaLocalValidation)` method when building the parameters.
532 (The default value for this parameter is `JsonSchemaLocalValidation.YES`.)
533
534```java
535import com.openai.core.JsonSchemaLocalValidation;
536import com.openai.models.ChatModel;
537import com.openai.models.chat.completions.ChatCompletionCreateParams;
538import com.openai.models.chat.completions.StructuredChatCompletionCreateParams;
539
540StructuredChatCompletionCreateParams<BookList> params = ChatCompletionCreateParams.builder()
541 .addUserMessage("List some famous late twentieth century novels.")
542 .model(ChatModel.GPT_4_1)
543 .responseFormat(BookList.class, JsonSchemaLocalValidation.NO)
544 .build();
545```
546
547By following these guidelines, you can ensure that your structured outputs conform to the necessary
548schema requirements and minimize the risk of remote validation errors.
549
550### Usage with the Responses API
551
552_Structured Outputs_ are also supported for the Responses API. The usage is the same as described
553except where the Responses API differs slightly from the Chat Completions API. Pass the top-level
554class to `text(Class<T>)` when building the parameters and then access an instance of the class from
555the generated message content in the response.
556
557You can start building the parameters with an instance of
558[`ResponseCreateParams.Builder`](openai-java-core/src/main/kotlin/com/openai/models/responses/ResponseCreateParams.kt)
559or
560[`StructuredResponseCreateParams.Builder`](openai-java-core/src/main/kotlin/com/openai/models/responses/StructuredResponseCreateParams.kt).
561If you start with the former (which allows for more compact code) the builder type will change to
562the latter when `ResponseCreateParams.Builder.text(Class<T>)` is called.
563
564For a full example of the usage of _Structured Outputs_ with the Responses API, see
565[`ResponsesStructuredOutputsExample`](openai-java-example/src/main/java/com/openai/example/ResponsesStructuredOutputsExample.java).
566
567### Usage with streaming
568
569_Structured Outputs_ can also be used with [Streaming](#streaming) and the Chat Completions API. As
570responses are returned in "chunks", the full response must first be accumulated to concatenate the
571JSON strings that can then be converted into instances of the arbitrary Java class. Normal streaming
572operations can be performed while accumulating the JSON strings.
573
574Use the [`ChatCompletionAccumulator`](openai-java-core/src/main/kotlin/com/openai/helpers/ChatCompletionAccumulator.kt)
575as described in the section on [Streaming helpers](#streaming-helpers) to accumulate the JSON
576strings. Once accumulated, use `ChatCompletionAccumulator.chatCompletion(Class<T>)` to convert the
577accumulated `ChatCompletion` into a
578[`StructuredChatCompletion`](openai-java-core/src/main/kotlin/com/openai/models/chat/completions/StructuredChatCompletion.kt).
579The `StructuredChatCompletion` can then automatically deserialize the JSON strings into instances of
580your Java class.
581
582For a full example of the usage of _Structured Outputs_ with Streaming and the Chat Completions API,
583see
584[`StructuredOutputsStreamingExample`](openai-java-example/src/main/java/com/openai/example/StructuredOutputsStreamingExample.java).
585
586With the Responses API, accumulate events while streaming using the
587[`ResponseAccumulator`](openai-java-core/src/main/kotlin/com/openai/helpers/ResponseAccumulator.kt).
588Once accumulated, use `ResponseAccumulator.response(Class<T>)` to convert the accumulated `Response`
589into a
590[`StructuredResponse`](openai-java-core/src/main/kotlin/com/openai/models/responses/StructuredResponse.kt).
591The [`StructuredResponse`] can then automatically deserialize the JSON strings into instances of
592your Java class.
593
594For a full example of the usage of _Structured Outputs_ with Streaming and the Responses API, see
595[`ResponsesStructuredOutputsStreamingExample`](openai-java-example/src/main/java/com/openai/example/ResponsesStructuredOutputsStreamingExample.java).
596
597### Defining JSON schema properties
598
599When a JSON schema is derived from your Java classes, all properties represented by `public` fields
600or `public` getter methods are included in the schema by default. Non-`public` fields and getter
601methods are _not_ included by default. You can exclude `public`, or include non-`public` fields or
602getter methods, by using the `@JsonIgnore` or `@JsonProperty` annotations respectively (see
603[Annotating classes and JSON schemas](#annotating-classes-and-json-schemas) for details).
604
605If you do not want to define `public` fields, you can define `private` fields and corresponding
606`public` getter methods. For example, a `private` field `myValue` with a `public` getter method
607`getMyValue()` will result in a `"myValue"` property being included in the JSON schema. If you
608prefer not to use the conventional Java "get" prefix for the name of the getter method, then you
609_must_ annotate the getter method with the `@JsonProperty` annotation and the full method name will
610be used as the property name. You do not have to define any corresponding setter methods if you do
611not need them.
612
613Each of your classes _must_ define at least one property to be included in the JSON schema. A
614validation error will occur if any class contains no fields or getter methods from which schema
615properties can be derived. This may occur if, for example:
616
617- There are no fields or getter methods in the class.
618- All fields and getter methods are `public`, but all are annotated with `@JsonIgnore`.
619- All fields and getter methods are non-`public`, but none are annotated with `@JsonProperty`.
620- A field or getter method is declared with a `Map` type. A `Map` is treated like a separate class
621 with no named properties, so it will result in an empty `"properties"` field in the JSON schema.
622
623### Annotating classes and JSON schemas
624
625You can use annotations to add further information to the JSON schema derived from your Java
626classes, or to control which fields or getter methods will be included in the schema. Details from
627annotations captured in the JSON schema may be used by the AI model to improve its response. The SDK
628supports the use of [Jackson Databind](https://github.com/FasterXML/jackson-databind) annotations.
629
630```java
631import com.fasterxml.jackson.annotation.JsonClassDescription;
632import com.fasterxml.jackson.annotation.JsonIgnore;
633import com.fasterxml.jackson.annotation.JsonPropertyDescription;
634
635class Person {
636 @JsonPropertyDescription("The first name and surname of the person")
637 public String name;
638 public int birthYear;
639 @JsonPropertyDescription("The year the person died, or 'present' if the person is living.")
640 public String deathYear;
641}
642
643@JsonClassDescription("The details of one published book")
644class Book {
645 public String title;
646 public Person author;
647 @JsonPropertyDescription("The year in which the book was first published.")
648 public int publicationYear;
649 @JsonIgnore public String genre;
650}
651
652class BookList {
653 public List<Book> books;
654}
655```
656
657- Use `@JsonClassDescription` to add a detailed description to a class.
658- Use `@JsonPropertyDescription` to add a detailed description to a field or getter method of a
659 class.
660- Use `@JsonIgnore` to exclude a `public` field or getter method of a class from the generated JSON
661 schema.
662- Use `@JsonProperty` to include a non-`public` field or getter method of a class in the generated
663 JSON schema.
664
665If you use `@JsonProperty(required = false)`, the `false` value will be ignored. OpenAI JSON schemas
666must mark all properties as _required_, so the schema generated from your Java classes will respect
667that restriction and ignore any annotation that would violate it.
668
669You can also use [OpenAPI Swagger 2](https://swagger.io/specification/v2/)
670[`@Schema`](https://github.com/swagger-api/swagger-core/wiki/Swagger-2.X---Annotations#schema) and
671[`@ArraySchema`](https://github.com/swagger-api/swagger-core/wiki/Swagger-2.X---Annotations#arrayschema)
672annotations. These allow type-specific constraints to be added to your schema properties. You can
673learn more about the supported constraints in the OpenAI documentation on
674[Supported properties](https://platform.openai.com/docs/guides/structured-outputs#supported-properties).
675
676```java
677import io.swagger.v3.oas.annotations.media.Schema;
678import io.swagger.v3.oas.annotations.media.ArraySchema;
679
680class Article {
681 @ArraySchema(minItems = 1, maxItems = 10)
682 public List<String> authors;
683
684 @Schema(pattern = "^[A-Za-z ]+$")
685 public String title;
686
687 @Schema(format = "date")
688 public String publicationDate;
689
690 @Schema(minimum = "1")
691 public int pageCount;
692}
693```
694
695Local validation will check that you have not used any unsupported constraint keywords. However, the
696values of the constraints are _not_ validated locally. For example, if you use a value for the
697`"format"` constraint of a string property that is not in the list of
698[supported format names](https://platform.openai.com/docs/guides/structured-outputs#supported-properties),
699then local validation will pass, but the AI model may report an error.
700
701If you use both Jackson and Swagger annotations to set the same schema field, the Jackson annotation
702will take precedence. In the following example, the description of `myProperty` will be set to
703"Jackson description"; "Swagger description" will be ignored:
704
705```java
706import com.fasterxml.jackson.annotation.JsonPropertyDescription;
707import io.swagger.v3.oas.annotations.media.Schema;
708
709class MyObject {
710 @Schema(description = "Swagger description")
711 @JsonPropertyDescription("Jackson description")
712 public String myProperty;
713}
714```
715
716## Function calling with JSON schemas
717
718OpenAI [Function Calling](https://platform.openai.com/docs/guides/function-calling?api-mode=chat)
719lets you integrate external functions directly into the language model's responses. Instead of
720producing plain text, the model can output instructions (with parameters) for calling a function
721when appropriate. You define a [JSON schema](https://json-schema.org/overview/what-is-jsonschema)
722for functions, and the model uses it to decide when and how to trigger these calls, enabling more
723interactive, data-driven applications.
724
725A JSON schema describing a function's parameters can be defined via the API by building a
726[`ChatCompletionTool`](openai-java-core/src/main/kotlin/com/openai/models/chat/completions/ChatCompletionTool.kt)
727containing a
728[`FunctionDefinition`](openai-java-core/src/main/kotlin/com/openai/models/FunctionDefinition.kt)
729and then using `addTool` to set it on the input parameters. The response from the AI model may then
730contain requests to call your functions, detailing the functions' names and their parameter values
731as JSON data that conforms to the JSON schema from the function definition. You can then parse the
732parameter values from this JSON, invoke your functions, and pass your functions' results back to the
733AI model. A full, working example of _Function Calling_ using the low-level API can be seen in
734[`FunctionCallingRawExample`](openai-java-example/src/main/java/com/openai/example/FunctionCallingRawExample.java).
735
736However, for greater convenience, the SDK can derive a function and its parameters automatically
737from the structure of an arbitrary Java class: the class's name provides the function name, and the
738class's fields define the function's parameters. When the AI model responds with the parameter
739values in JSON form, you can then easily convert that JSON to an instance of your Java class and
740use the parameter values to invoke your custom function. A full, working example of the use of
741_Function Calling_ with Java classes to define function parameters can be seen in
742[`FunctionCallingExample`](openai-java-example/src/main/java/com/openai/example/FunctionCallingExample.java).
743
744Like for [Structured Outputs](#structured-outputs-with-json-schemas), Java classes can contain
745fields declared to be instances of other classes and can use collections (see
746[Defining JSON schema properties](#defining-json-schema-properties) for more details). Optionally,
747annotations can be used to set the descriptions of the function (class) and its parameters (fields)
748to assist the AI model in understanding the purpose of the function and the possible values of its
749parameters.
750
751```java
752import com.fasterxml.jackson.annotation.JsonClassDescription;
753import com.fasterxml.jackson.annotation.JsonPropertyDescription;
754
755@JsonClassDescription("Gets the quality of the given SDK.")
756static class GetSdkQuality {
757 @JsonPropertyDescription("The name of the SDK.")
758 public String name;
759
760 public SdkQuality execute() {
761 return new SdkQuality(
762 name, name.contains("OpenAI") ? "It's robust and polished!" : "*shrug*");
763 }
764}
765
766static class SdkQuality {
767 public String quality;
768
769 public SdkQuality(String name, String evaluation) {
770 quality = name + ": " + evaluation;
771 }
772}
773
774@JsonClassDescription("Gets the review score (out of 10) for the named SDK.")
775static class GetSdkScore {
776 public String name;
777
778 public int execute() {
779 return name.contains("OpenAI") ? 10 : 3;
780 }
781}
782```
783
784When your functions are defined, add them to the input parameters using `addTool(Class<T>)` and then
785call them if requested to do so in the AI model's response. `Function.argments(Class<T>)` can be
786used to parse a function's parameters in JSON form to an instance of your function-defining class.
787The fields of that instance will be set to the values of the parameters to the function call.
788
789After calling the function, use `ChatCompletionToolMessageParam.Builder.contentAsJson(Object)` to
790pass the function's result back to the AI model. The method will convert the result to JSON form
791for consumption by the model. The `Object` can be any object, including simple `String` instances
792and boxed primitive types.
793
794```java
795import com.openai.client.OpenAIClient;
796import com.openai.client.okhttp.OpenAIOkHttpClient;
797import com.openai.models.ChatModel;
798import com.openai.models.chat.completions.*;
799import java.util.Collection;
800
801OpenAIClient client = OpenAIOkHttpClient.fromEnv();
802
803ChatCompletionCreateParams.Builder createParamsBuilder = ChatCompletionCreateParams.builder()
804 .model(ChatModel.GPT_3_5_TURBO)
805 .maxCompletionTokens(2048)
806 .addTool(GetSdkQuality.class)
807 .addTool(GetSdkScore.class)
808 .addUserMessage("How good are the following SDKs and what do reviewers say: "
809 + "OpenAI Java SDK, Unknown Company SDK.");
810
811client.chat().completions().create(createParamsBuilder.build()).choices().stream()
812 .map(ChatCompletion.Choice::message)
813 // Add each assistant message onto the builder so that we keep track of the
814 // conversation for asking a follow-up question later.
815 .peek(createParamsBuilder::addMessage)
816 .flatMap(message -> {
817 message.content().ifPresent(System.out::println);
818 return message.toolCalls().stream().flatMap(Collection::stream);
819 })
820 .forEach(toolCall -> {
821 Object result = callFunction(toolCall.function());
822 // Add the tool call result to the conversation.
823 createParamsBuilder.addMessage(ChatCompletionToolMessageParam.builder()
824 .toolCallId(toolCall.id())
825 .contentAsJson(result)
826 .build());
827 });
828
829// Ask a follow-up question about the function call result.
830createParamsBuilder.addUserMessage("Why do you say that?");
831client.chat().completions().create(createParamsBuilder.build()).choices().stream()
832 .flatMap(choice -> choice.message().content().stream())
833 .forEach(System.out::println);
834
835static Object callFunction(ChatCompletionMessageToolCall.Function function) {
836 switch (function.name()) {
837 case "GetSdkQuality":
838 return function.arguments(GetSdkQuality.class).execute();
839 case "GetSdkScore":
840 return function.arguments(GetSdkScore.class).execute();
841 default:
842 throw new IllegalArgumentException("Unknown function: " + function.name());
843 }
844}
845```
846
847In the code above, an `execute()` method encapsulates each function's logic. However, there is no
848requirement to follow that pattern. You are free to implement your function's logic in any way that
849best suits your use case. The pattern above is only intended to _suggest_ that a suitable pattern
850may make the process of function calling simpler to understand and implement.
851
852### Usage with the Responses API
853
854_Function Calling_ is also supported for the Responses API. The usage is the same as described
855except where the Responses API differs slightly from the Chat Completions API. Pass the top-level
856class to `addTool(Class<T>)` when building the parameters. In the response, look for
857[`RepoonseOutputItem`](openai-java-core/src/main/kotlin/com/openai/models/responses/ResponseOutputItem.kt)
858instances that are function calls. Parse the parameters to each function call to an instance of the
859class using
860[`ResponseFunctionToolCall.arguments(Class<T>)`](openai-java-core/src/main/kotlin/com/openai/models/responses/ResponseFunctionToolCall.kt).
861Finally, pass the result of each call back to the model.
862
863For a full example of the usage of _Function Calling_ with the Responses API using the low-level
864API to define and parse function parameters, see
865[`ResponsesFunctionCallingRawExample`](openai-java-example/src/main/java/com/openai/example/ResponsesFunctionCallingRawExample.java).
866
867For a full example of the usage of _Function Calling_ with the Responses API using Java classes to
868define and parse function parameters, see
869[`ResponsesFunctionCallingExample`](openai-java-example/src/main/java/com/openai/example/ResponsesFunctionCallingExample.java).
870
871### Local function JSON schema validation
872
873Like for _Structured Outputs_, you can perform local validation to check that the JSON schema
874derived from your function class respects the restrictions imposed by OpenAI on such schemas. Local
875validation is enabled by default, but it can be disabled by adding `JsonSchemaLocalValidation.NO` to
876the call to `addTool`.
877
878```java
879ChatCompletionCreateParams.Builder createParamsBuilder = ChatCompletionCreateParams.builder()
880 .model(ChatModel.GPT_3_5_TURBO)
881 .maxCompletionTokens(2048)
882 .addTool(GetSdkQuality.class, JsonSchemaLocalValidation.NO)
883 .addTool(GetSdkScore.class, JsonSchemaLocalValidation.NO)
884 .addUserMessage("How good are the following SDKs and what do reviewers say: "
885 + "OpenAI Java SDK, Unknown Company SDK.");
886```
887
888See [Local JSON schema validation](#local-json-schema-validation) for more details on local schema
889validation and under what circumstances you might want to disable it.
890
891### Annotating function classes
892
893You can use annotations to add further information about functions to the JSON schemas that are
894derived from your function classes, or to control which fields or getter methods will be used as
895parameters to the function. Details from annotations captured in the JSON schema may be used by the
896AI model to improve its response. The SDK supports the use of
897[Jackson Databind](https://github.com/FasterXML/jackson-databind) annotations.
898
899- Use `@JsonClassDescription` to add a description to a function class detailing when and how to use
900 that function.
901- Use `@JsonTypeName` to set the function name to something other than the simple name of the class,
902 which is used by default.
903- Use `@JsonPropertyDescription` to add a detailed description to function parameter (a field or
904 getter method of a function class).
905- Use `@JsonIgnore` to exclude a `public` field or getter method of a class from the generated JSON
906 schema for a function's parameters.
907- Use `@JsonProperty` to include a non-`public` field or getter method of a class in the generated
908 JSON schema for a function's parameters.
909
910OpenAI provides some
911[Best practices for defining functions](https://platform.openai.com/docs/guides/function-calling#best-practices-for-defining-functions)
912that may help you to understand how to use the above annotations effectively for your functions.
913
914See also [Defining JSON schema properties](#defining-json-schema-properties) for more details on how
915to use fields and getter methods and combine access modifiers and annotations to define the
916parameters of your functions. The same rules apply to function classes and to the structured output
917classes described in that section.
918
919## File uploads
920
921The SDK defines methods that accept files.
922
923To upload a file, pass a [`Path`](https://docs.oracle.com/javase/8/docs/api/java/nio/file/Path.html):
924
925```java
926import com.openai.models.files.FileCreateParams;
927import com.openai.models.files.FileObject;
928import com.openai.models.files.FilePurpose;
929import java.nio.file.Paths;
930
931FileCreateParams params = FileCreateParams.builder()
932 .purpose(FilePurpose.FINE_TUNE)
933 .file(Paths.get("input.jsonl"))
934 .build();
935FileObject fileObject = client.files().create(params);
936```
937
938Or an arbitrary [`InputStream`](https://docs.oracle.com/javase/8/docs/api/java/io/InputStream.html):
939
940```java
941import com.openai.models.files.FileCreateParams;
942import com.openai.models.files.FileObject;
943import com.openai.models.files.FilePurpose;
944import java.net.URL;
945
946FileCreateParams params = FileCreateParams.builder()
947 .purpose(FilePurpose.FINE_TUNE)
948 .file(new URL("https://example.com/input.jsonl").openStream())
949 .build();
950FileObject fileObject = client.files().create(params);
951```
952
953Or a `byte[]` array:
954
955```java
956import com.openai.models.files.FileCreateParams;
957import com.openai.models.files.FileObject;
958import com.openai.models.files.FilePurpose;
959
960FileCreateParams params = FileCreateParams.builder()
961 .purpose(FilePurpose.FINE_TUNE)
962 .file("content".getBytes())
963 .build();
964FileObject fileObject = client.files().create(params);
965```
966
967Note that when passing a non-`Path` its filename is unknown so it will not be included in the request. To manually set a filename, pass a [`MultipartField`](openai-java-core/src/main/kotlin/com/openai/core/Values.kt):
968
969```java
970import com.openai.core.MultipartField;
971import com.openai.models.files.FileCreateParams;
972import com.openai.models.files.FileObject;
973import com.openai.models.files.FilePurpose;
974import java.io.InputStream;
975import java.net.URL;
976
977FileCreateParams params = FileCreateParams.builder()
978 .purpose(FilePurpose.FINE_TUNE)
979 .file(MultipartField.<InputStream>builder()
980 .value(new URL("https://example.com/input.jsonl").openStream())
981 .filename("input.jsonl")
982 .build())
983 .build();
984FileObject fileObject = client.files().create(params);
985```
986
987## Webhook Verification
988
989Verifying webhook signatures is _optional but encouraged_.
990
991For more information about webhooks, see [the API docs](https://platform.openai.com/docs/guides/webhooks).
992
993### Parsing webhook payloads
994
995For most use cases, you will likely want to verify the webhook and parse the payload at the same time. To achieve this, we provide the method `client.webhooks().unwrap()`, which parses a webhook request and verifies that it was sent by OpenAI. This method will throw an exception if the signature is invalid.
996
997Note that the `body` parameter must be the raw JSON string sent from the server (do not parse it first). The `.unwrap()` method will parse this JSON for you into an event object after verifying the webhook was sent from OpenAI.
998
999```java
1000import com.openai.client.OpenAIClient;
1001import com.openai.client.okhttp.OpenAIOkHttpClient;
1002import com.openai.core.http.Headers;
1003import com.openai.models.webhooks.UnwrapWebhookEvent;
1004import java.util.Optional;
1005
1006OpenAIClient client = OpenAIOkHttpClient.fromEnv(); // OPENAI_WEBHOOK_SECRET env var used by default
1007
1008public void handleWebhook(String body, Map<String, String> headers) {
1009 try {
1010 Headers headersList = Headers.builder()
1011 .putAll(headers)
1012 .build();
1013
1014 UnwrapWebhookEvent event = client.webhooks().unwrap(body, headersList, Optional.empty());
1015
1016 if (event.isResponseCompletedWebhookEvent()) {
1017 System.out.println("Response completed: " + event.asResponseCompletedWebhookEvent().data());
1018 } else if (event.isResponseFailed()) {
1019 System.out.println("Response failed: " + event.asResponseFailed().data());
1020 } else {
1021 System.out.println("Unhandled event type: " + event.getClass().getSimpleName());
1022 }
1023 } catch (Exception e) {
1024 System.err.println("Invalid webhook signature: " + e.getMessage());
1025 // Handle invalid signature
1026 }
1027}
1028```
1029
1030### Verifying webhook payloads directly
1031
1032In some cases, you may want to verify the webhook separately from parsing the payload. If you prefer to handle these steps separately, we provide the method `client.webhooks().verifySignature()` to _only verify_ the signature of a webhook request. Like `.unwrap()`, this method will throw an exception if the signature is invalid.
1033
1034Note that the `body` parameter must be the raw JSON string sent from the server (do not parse it first). You will then need to parse the body after verifying the signature.
1035
1036```java
1037import com.fasterxml.jackson.databind.ObjectMapper;
1038import com.openai.client.OpenAIClient;
1039import com.openai.client.okhttp.OpenAIOkHttpClient;
1040import com.openai.core.http.Headers;
1041import com.openai.models.webhooks.WebhookVerificationParams;
1042import java.util.Optional;
1043
1044OpenAIClient client = OpenAIOkHttpClient.fromEnv(); // OPENAI_WEBHOOK_SECRET env var used by default
1045ObjectMapper objectMapper = new ObjectMapper();
1046
1047public void handleWebhook(String body, Map<String, String> headers) {
1048 try {
1049 Headers headersList = Headers.builder()
1050 .putAll(headers)
1051 .build();
1052
1053 client.webhooks().verifySignature(
1054 WebhookVerificationParams.builder()
1055 .payload(body)
1056 .headers(headersList)
1057 .build()
1058 );
1059
1060 // Parse the body after verification
1061 Map<String, Object> event = objectMapper.readValue(body, Map.class);
1062 System.out.println("Verified event: " + event);
1063 } catch (Exception e) {
1064 System.err.println("Invalid webhook signature: " + e.getMessage());
1065 // Handle invalid signature
1066 }
1067}
1068```
1069
1070## Binary responses
1071
1072The SDK defines methods that return binary responses, which are used for API responses that shouldn't necessarily be parsed, like non-JSON data.
1073
1074These methods return [`HttpResponse`](openai-java-core/src/main/kotlin/com/openai/core/http/HttpResponse.kt):
1075
1076```java
1077import com.openai.core.http.HttpResponse;
1078import com.openai.models.files.FileContentParams;
1079
1080HttpResponse response = client.files().content("file_id");
1081```
1082
1083To save the response content to a file, use the [`Files.copy(...)`](https://docs.oracle.com/javase/8/docs/api/java/nio/file/Files.html#copy-java.io.InputStream-java.nio.file.Path-java.nio.file.CopyOption...-) method:
1084
1085```java
1086import com.openai.core.http.HttpResponse;
1087import java.nio.file.Files;
1088import java.nio.file.Paths;
1089import java.nio.file.StandardCopyOption;
1090
1091try (HttpResponse response = client.files().content(params)) {
1092 Files.copy(
1093 response.body(),
1094 Paths.get(path),
1095 StandardCopyOption.REPLACE_EXISTING
1096 );
1097} catch (Exception e) {
1098 System.out.println("Something went wrong!");
1099 throw new RuntimeException(e);
1100}
1101```
1102
1103Or transfer the response content to any [`OutputStream`](https://docs.oracle.com/javase/8/docs/api/java/io/OutputStream.html):
1104
1105```java
1106import com.openai.core.http.HttpResponse;
1107import java.nio.file.Files;
1108import java.nio.file.Paths;
1109
1110try (HttpResponse response = client.files().content(params)) {
1111 response.body().transferTo(Files.newOutputStream(Paths.get(path)));
1112} catch (Exception e) {
1113 System.out.println("Something went wrong!");
1114 throw new RuntimeException(e);
1115}
1116```
1117
1118## Raw responses
1119
1120The SDK defines methods that deserialize responses into instances of Java classes. However, these methods don't provide access to the response headers, status code, or the raw response body.
1121
1122To access this data, prefix any HTTP method call on a client or service with `withRawResponse()`:
1123
1124```java
1125import com.openai.core.http.Headers;
1126import com.openai.core.http.HttpResponseFor;
1127import com.openai.models.ChatModel;
1128import com.openai.models.chat.completions.ChatCompletion;
1129import com.openai.models.chat.completions.ChatCompletionCreateParams;
1130
1131ChatCompletionCreateParams params = ChatCompletionCreateParams.builder()
1132 .addUserMessage("Say this is a test")
1133 .model(ChatModel.GPT_4_1)
1134 .build();
1135HttpResponseFor<ChatCompletion> chatCompletion = client.chat().completions().withRawResponse().create(params);
1136
1137int statusCode = chatCompletion.statusCode();
1138Headers headers = chatCompletion.headers();
1139```
1140
1141You can still deserialize the response into an instance of a Java class if needed:
1142
1143```java
1144import com.openai.models.chat.completions.ChatCompletion;
1145
1146ChatCompletion parsedChatCompletion = chatCompletion.parse();
1147```
1148
1149### Request IDs
1150
1151> For more information on debugging requests, see [the API docs](https://platform.openai.com/docs/api-reference/debugging-requests).
1152
1153When using raw responses, you can access the `x-request-id` response header using the `requestId()` method:
1154
1155```java
1156import com.openai.core.http.HttpResponseFor;
1157import com.openai.models.chat.completions.ChatCompletion;
1158import java.util.Optional;
1159
1160HttpResponseFor<ChatCompletion> chatCompletion = client.chat().completions().withRawResponse().create(params);
1161Optional<String> requestId = chatCompletion.requestId();
1162```
1163
1164This can be used to quickly log failing requests and report them back to OpenAI.
1165
1166## Error handling
1167
1168The SDK throws custom unchecked exception types:
1169
1170- [`OpenAIServiceException`](openai-java-core/src/main/kotlin/com/openai/errors/OpenAIServiceException.kt): Base class for HTTP errors. See this table for which exception subclass is thrown for each HTTP status code:
1171
1172 | Status | Exception |
1173 | ------ | ---------------------------------------------------------------------------------------------------------------------- |
1174 | 400 | [`BadRequestException`](openai-java-core/src/main/kotlin/com/openai/errors/BadRequestException.kt) |
1175 | 401 | [`UnauthorizedException`](openai-java-core/src/main/kotlin/com/openai/errors/UnauthorizedException.kt) |
1176 | 403 | [`PermissionDeniedException`](openai-java-core/src/main/kotlin/com/openai/errors/PermissionDeniedException.kt) |
1177 | 404 | [`NotFoundException`](openai-java-core/src/main/kotlin/com/openai/errors/NotFoundException.kt) |
1178 | 422 | [`UnprocessableEntityException`](openai-java-core/src/main/kotlin/com/openai/errors/UnprocessableEntityException.kt) |
1179 | 429 | [`RateLimitException`](openai-java-core/src/main/kotlin/com/openai/errors/RateLimitException.kt) |
1180 | 5xx | [`InternalServerException`](openai-java-core/src/main/kotlin/com/openai/errors/InternalServerException.kt) |
1181 | others | [`UnexpectedStatusCodeException`](openai-java-core/src/main/kotlin/com/openai/errors/UnexpectedStatusCodeException.kt) |
1182
1183 [`SseException`](openai-java-core/src/main/kotlin/com/openai/errors/SseException.kt) is thrown for errors encountered during [SSE streaming](https://developer.mozilla.org/en-US/docs/Web/API/Server-sent_events) after a successful initial HTTP response.
1184
1185- [`OpenAIIoException`](openai-java-core/src/main/kotlin/com/openai/errors/OpenAIIoException.kt): I/O networking errors.
1186
1187- [`OpenAIInvalidDataException`](openai-java-core/src/main/kotlin/com/openai/errors/OpenAIInvalidDataException.kt): Failure to interpret successfully parsed data. For example, when accessing a property that's supposed to be required, but the API unexpectedly omitted it from the response.
1188
1189- [`OpenAIException`](openai-java-core/src/main/kotlin/com/openai/errors/OpenAIException.kt): Base class for all exceptions. Most errors will result in one of the previously mentioned ones, but completely generic errors may be thrown using the base class.
1190
1191## Pagination
1192
1193The SDK defines methods that return a paginated lists of results. It provides convenient ways to access the results either one page at a time or item-by-item across all pages.
1194
1195### Auto-pagination
1196
1197To iterate through all results across all pages, use the `autoPager()` method, which automatically fetches more pages as needed.
1198
1199When using the synchronous client, the method returns an [`Iterable`](https://docs.oracle.com/javase/8/docs/api/java/lang/Iterable.html)
1200
1201```java
1202import com.openai.models.finetuning.jobs.FineTuningJob;
1203import com.openai.models.finetuning.jobs.JobListPage;
1204
1205JobListPage page = client.fineTuning().jobs().list();
1206
1207// Process as an Iterable
1208for (FineTuningJob job : page.autoPager()) {
1209 System.out.println(job);
1210}
1211
1212// Process as a Stream
1213page.autoPager()
1214 .stream()
1215 .limit(50)
1216 .forEach(job -> System.out.println(job));
1217```
1218
1219When using the asynchronous client, the method returns an [`AsyncStreamResponse`](openai-java-core/src/main/kotlin/com/openai/core/http/AsyncStreamResponse.kt):
1220
1221```java
1222import com.openai.core.http.AsyncStreamResponse;
1223import com.openai.models.finetuning.jobs.FineTuningJob;
1224import com.openai.models.finetuning.jobs.JobListPageAsync;
1225import java.util.Optional;
1226import java.util.concurrent.CompletableFuture;
1227
1228CompletableFuture<JobListPageAsync> pageFuture = client.async().fineTuning().jobs().list();
1229
1230pageFuture.thenRun(page -> page.autoPager().subscribe(job -> {
1231 System.out.println(job);
1232}));
1233
1234// If you need to handle errors or completion of the stream
1235pageFuture.thenRun(page -> page.autoPager().subscribe(new AsyncStreamResponse.Handler<>() {
1236 @Override
1237 public void onNext(FineTuningJob job) {
1238 System.out.println(job);
1239 }
1240
1241 @Override
1242 public void onComplete(Optional<Throwable> error) {
1243 if (error.isPresent()) {
1244 System.out.println("Something went wrong!");
1245 throw new RuntimeException(error.get());
1246 } else {
1247 System.out.println("No more!");
1248 }
1249 }
1250}));
1251
1252// Or use futures
1253pageFuture.thenRun(page -> page.autoPager()
1254 .subscribe(job -> {
1255 System.out.println(job);
1256 })
1257 .onCompleteFuture()
1258 .whenComplete((unused, error) -> {
1259 if (error != null) {
1260 System.out.println("Something went wrong!");
1261 throw new RuntimeException(error);
1262 } else {
1263 System.out.println("No more!");
1264 }
1265 }));
1266```
1267
1268### Manual pagination
1269
1270To access individual page items and manually request the next page, use the `items()`,
1271`hasNextPage()`, and `nextPage()` methods:
1272
1273```java
1274import com.openai.models.finetuning.jobs.FineTuningJob;
1275import com.openai.models.finetuning.jobs.JobListPage;
1276
1277JobListPage page = client.fineTuning().jobs().list();
1278while (true) {
1279 for (FineTuningJob job : page.items()) {
1280 System.out.println(job);
1281 }
1282
1283 if (!page.hasNextPage()) {
1284 break;
1285 }
1286
1287 page = page.nextPage();
1288}
1289```
1290
1291## Logging
1292
1293The SDK uses the standard [OkHttp logging interceptor](https://github.com/square/okhttp/tree/master/okhttp-logging-interceptor).
1294
1295Enable logging by setting the `OPENAI_LOG` environment variable to `info`:
1296
1297```sh
1298$ export OPENAI_LOG=info
1299```
1300
1301Or to `debug` for more verbose logging:
1302
1303```sh
1304$ export OPENAI_LOG=debug
1305```
1306
1307## GraalVM
1308
1309Although the SDK uses reflection, it is still usable in [GraalVM](https://www.graalvm.org) because `openai-java-core` is published with [reachability metadata](https://www.graalvm.org/latest/reference-manual/native-image/metadata/).
1310
1311GraalVM should automatically detect and use the published metadata, but [manual configuration](https://www.graalvm.org/jdk24/reference-manual/native-image/overview/BuildConfiguration/) is also available.
1312
1313## Spring Boot
1314
1315If you're using Spring Boot, then you can use the SDK's [Spring Boot starter](https://docs.spring.io/spring-boot/docs/2.7.18/reference/htmlsingle/#using.build-systems.starters) to simplify configuration and get set up quickly.
1316
1317### Installation
1318
1319<!-- x-release-please-start-version -->
1320
1321#### Gradle
1322
1323```kotlin
1324implementation("com.openai:openai-java-spring-boot-starter:2.18.0")
1325```
1326
1327#### Maven
1328
1329```xml
1330<dependency>
1331 <groupId>com.openai</groupId>
1332 <artifactId>openai-java-spring-boot-starter</artifactId>
1333 <version>2.18.0</version>
1334</dependency>
1335```
1336
1337<!-- x-release-please-end -->
1338
1339### Configuration
1340
1341The [client's environment variable options](#client-configuration) can be configured in [`application.properties` or `application.yml`](https://docs.spring.io/spring-boot/how-to/properties-and-configuration.html).
1342
1343#### `application.properties`
1344
1345```properties
1346openai.base-url=https://api.openai.com/v1
1347openai.api-key=My API Key
1348openai.org-id=My Organization
1349openai.project-id=My Project
1350openai.webhook-secret=My Webhook Secret
1351```
1352
1353#### `application.yml`
1354
1355```yaml
1356openai:
1357 base-url: https://api.openai.com/v1
1358 api-key: My API Key
1359 org-id: My Organization
1360 project-id: My Project
1361 webhook-secret: My Webhook Secret
1362```
1363
1364#### Other configuration
1365
1366Configure any other client option by providing one or more instances of [`OpenAIClientCustomizer`](openai-java-core/src/main/kotlin/com/openai/springboot/OpenAIClientCustomizer.kt). For example, here's how you'd set [`maxRetries`](#retries):
1367
1368```java
1369import com.openai.springboot.OpenAIClientCustomizer;
1370import org.springframework.context.annotation.Bean;
1371import org.springframework.context.annotation.Configuration;
1372
1373@Configuration
1374public class OpenAIConfig {
1375 @Bean
1376 public OpenAIClientCustomizer customizer() {
1377 return builder -> builder.maxRetries(3);
1378 }
1379}
1380```
1381
1382### Usage
1383
1384[Inject](https://docs.spring.io/spring-framework/reference/core/beans/dependencies/factory-collaborators.html) [`OpenAIClient`](openai-java-core/src/main/kotlin/com/openai/client/OpenAIClient.kt) anywhere and start using it!
1385
1386## Jackson
1387
1388The SDK depends on [Jackson](https://github.com/FasterXML/jackson) for JSON serialization/deserialization. It is compatible with version 2.13.4 or higher, but depends on version 2.18.2 by default.
1389
1390The SDK throws an exception if it detects an incompatible Jackson version at runtime (e.g. if the default version was overridden in your Maven or Gradle config).
1391
1392If the SDK threw an exception, but you're _certain_ the version is compatible, then disable the version check using the `checkJacksonVersionCompatibility` on [`OpenAIOkHttpClient`](openai-java-client-okhttp/src/main/kotlin/com/openai/client/okhttp/OpenAIOkHttpClient.kt) or [`OpenAIOkHttpClientAsync`](openai-java-client-okhttp/src/main/kotlin/com/openai/client/okhttp/OpenAIOkHttpClientAsync.kt).
1393
1394> [!CAUTION]
1395> We make no guarantee that the SDK works correctly when the Jackson version check is disabled.
1396
1397## Microsoft Azure
1398
1399To use this library with [Azure OpenAI](https://learn.microsoft.com/azure/ai-services/openai/overview), use the same
1400OpenAI client builder but with the Azure-specific configuration.
1401
1402```java
1403OpenAIClient client = OpenAIOkHttpClient.builder()
1404 // Gets the API key and endpoint from the `AZURE_OPENAI_KEY` and `OPENAI_BASE_URL` environment variables, respectively
1405 .fromEnv()
1406 // Set the Azure Entra ID
1407 .credential(BearerTokenCredential.create(AuthenticationUtil.getBearerTokenSupplier(
1408 new DefaultAzureCredentialBuilder().build(), "https://cognitiveservices.azure.com/.default")))
1409 .build();
1410```
1411
1412See the complete Azure OpenAI example in the [`openai-java-example`](openai-java-example/src/main/java/com/openai/example/AzureEntraIdExample.java) directory. The other examples in the directory also work with Azure as long as the client is configured to use it.
1413
1414## Network options
1415
1416### Retries
1417
1418The SDK automatically retries 2 times by default, with a short exponential backoff.
1419
1420Only the following error types are retried:
1421
1422- Connection errors (for example, due to a network connectivity problem)
1423- 408 Request Timeout
1424- 409 Conflict
1425- 429 Rate Limit
1426- 5xx Internal
1427
1428The API may also explicitly instruct the SDK to retry or not retry a response.
1429
1430To set a custom number of retries, configure the client using the `maxRetries` method:
1431
1432```java
1433import com.openai.client.OpenAIClient;
1434import com.openai.client.okhttp.OpenAIOkHttpClient;
1435
1436OpenAIClient client = OpenAIOkHttpClient.builder()
1437 .fromEnv()
1438 .maxRetries(4)
1439 .build();
1440```
1441
1442### Timeouts
1443
1444Requests time out after 10 minutes by default.
1445
1446To set a custom timeout, configure the method call using the `timeout` method:
1447
1448```java
1449import com.openai.models.chat.completions.ChatCompletion;
1450
1451ChatCompletion chatCompletion = client.chat().completions().create(
1452 params, RequestOptions.builder().timeout(Duration.ofSeconds(30)).build()
1453);
1454```
1455
1456Or configure the default for all method calls at the client level:
1457
1458```java
1459import com.openai.client.OpenAIClient;
1460import com.openai.client.okhttp.OpenAIOkHttpClient;
1461import java.time.Duration;
1462
1463OpenAIClient client = OpenAIOkHttpClient.builder()
1464 .fromEnv()
1465 .timeout(Duration.ofSeconds(30))
1466 .build();
1467```
1468
1469### Proxies
1470
1471To route requests through a proxy, configure the client using the `proxy` method:
1472
1473```java
1474import com.openai.client.OpenAIClient;
1475import com.openai.client.okhttp.OpenAIOkHttpClient;
1476import java.net.InetSocketAddress;
1477import java.net.Proxy;
1478
1479OpenAIClient client = OpenAIOkHttpClient.builder()
1480 .fromEnv()
1481 .proxy(new Proxy(
1482 Proxy.Type.HTTP, new InetSocketAddress(
1483 "https://example.com", 8080
1484 )
1485 ))
1486 .build();
1487```
1488
1489### HTTPS
1490
1491> [!NOTE]
1492> Most applications should not call these methods, and instead use the system defaults. The defaults include
1493> special optimizations that can be lost if the implementations are modified.
1494
1495To configure how HTTPS connections are secured, configure the client using the `sslSocketFactory`, `trustManager`, and `hostnameVerifier` methods:
1496
1497```java
1498import com.openai.client.OpenAIClient;
1499import com.openai.client.okhttp.OpenAIOkHttpClient;
1500
1501OpenAIClient client = OpenAIOkHttpClient.builder()
1502 .fromEnv()
1503 // If `sslSocketFactory` is set, then `trustManager` must be set, and vice versa.
1504 .sslSocketFactory(yourSSLSocketFactory)
1505 .trustManager(yourTrustManager)
1506 .hostnameVerifier(yourHostnameVerifier)
1507 .build();
1508```
1509
1510### Custom HTTP client
1511
1512The SDK consists of three artifacts:
1513
1514- `openai-java-core`
1515 - Contains core SDK logic
1516 - Does not depend on [OkHttp](https://square.github.io/okhttp)
1517 - Exposes [`OpenAIClient`](openai-java-core/src/main/kotlin/com/openai/client/OpenAIClient.kt), [`OpenAIClientAsync`](openai-java-core/src/main/kotlin/com/openai/client/OpenAIClientAsync.kt), [`OpenAIClientImpl`](openai-java-core/src/main/kotlin/com/openai/client/OpenAIClientImpl.kt), and [`OpenAIClientAsyncImpl`](openai-java-core/src/main/kotlin/com/openai/client/OpenAIClientAsyncImpl.kt), all of which can work with any HTTP client
1518- `openai-java-client-okhttp`
1519 - Depends on [OkHttp](https://square.github.io/okhttp)
1520 - Exposes [`OpenAIOkHttpClient`](openai-java-client-okhttp/src/main/kotlin/com/openai/client/okhttp/OpenAIOkHttpClient.kt) and [`OpenAIOkHttpClientAsync`](openai-java-client-okhttp/src/main/kotlin/com/openai/client/okhttp/OpenAIOkHttpClientAsync.kt), which provide a way to construct [`OpenAIClientImpl`](openai-java-core/src/main/kotlin/com/openai/client/OpenAIClientImpl.kt) and [`OpenAIClientAsyncImpl`](openai-java-core/src/main/kotlin/com/openai/client/OpenAIClientAsyncImpl.kt), respectively, using OkHttp
1521- `openai-java`
1522 - Depends on and exposes the APIs of both `openai-java-core` and `openai-java-client-okhttp`
1523 - Does not have its own logic
1524
1525This structure allows replacing the SDK's default HTTP client without pulling in unnecessary dependencies.
1526
1527#### Customized [`OkHttpClient`](https://square.github.io/okhttp/3.x/okhttp/okhttp3/OkHttpClient.html)
1528
1529> [!TIP]
1530> Try the available [network options](#network-options) before replacing the default client.
1531
1532To use a customized `OkHttpClient`:
1533
15341. Replace your [`openai-java` dependency](#installation) with `openai-java-core`
15352. Copy `openai-java-client-okhttp`'s [`OkHttpClient`](openai-java-client-okhttp/src/main/kotlin/com/openai/client/okhttp/OkHttpClient.kt) class into your code and customize it
15363. Construct [`OpenAIClientImpl`](openai-java-core/src/main/kotlin/com/openai/client/OpenAIClientImpl.kt) or [`OpenAIClientAsyncImpl`](openai-java-core/src/main/kotlin/com/openai/client/OpenAIClientAsyncImpl.kt), similarly to [`OpenAIOkHttpClient`](openai-java-client-okhttp/src/main/kotlin/com/openai/client/okhttp/OpenAIOkHttpClient.kt) or [`OpenAIOkHttpClientAsync`](openai-java-client-okhttp/src/main/kotlin/com/openai/client/okhttp/OpenAIOkHttpClientAsync.kt), using your customized client
1537
1538### Completely custom HTTP client
1539
1540To use a completely custom HTTP client:
1541
15421. Replace your [`openai-java` dependency](#installation) with `openai-java-core`
15432. Write a class that implements the [`HttpClient`](openai-java-core/src/main/kotlin/com/openai/core/http/HttpClient.kt) interface
15443. Construct [`OpenAIClientImpl`](openai-java-core/src/main/kotlin/com/openai/client/OpenAIClientImpl.kt) or [`OpenAIClientAsyncImpl`](openai-java-core/src/main/kotlin/com/openai/client/OpenAIClientAsyncImpl.kt), similarly to [`OpenAIOkHttpClient`](openai-java-client-okhttp/src/main/kotlin/com/openai/client/okhttp/OpenAIOkHttpClient.kt) or [`OpenAIOkHttpClientAsync`](openai-java-client-okhttp/src/main/kotlin/com/openai/client/okhttp/OpenAIOkHttpClientAsync.kt), using your new client class
1545
1546## Undocumented API functionality
1547
1548The SDK is typed for convenient usage of the documented API. However, it also supports working with undocumented or not yet supported parts of the API.
1549
1550### Parameters
1551
1552To set undocumented parameters, call the `putAdditionalHeader`, `putAdditionalQueryParam`, or `putAdditionalBodyProperty` methods on any `Params` class:
1553
1554```java
1555import com.openai.core.JsonValue;
1556import com.openai.models.chat.completions.ChatCompletionCreateParams;
1557
1558ChatCompletionCreateParams params = ChatCompletionCreateParams.builder()
1559 .putAdditionalHeader("Secret-Header", "42")
1560 .putAdditionalQueryParam("secret_query_param", "42")
1561 .putAdditionalBodyProperty("secretProperty", JsonValue.from("42"))
1562 .build();
1563```
1564
1565These can be accessed on the built object later using the `_additionalHeaders()`, `_additionalQueryParams()`, and `_additionalBodyProperties()` methods.
1566
1567To set undocumented parameters on _nested_ headers, query params, or body classes, call the `putAdditionalProperty` method on the nested class:
1568
1569```java
1570import com.openai.core.JsonValue;
1571import com.openai.models.chat.completions.ChatCompletionCreateParams;
1572
1573ChatCompletionCreateParams params = ChatCompletionCreateParams.builder()
1574 .responseFormat(ChatCompletionCreateParams.ResponseFormat.builder()
1575 .putAdditionalProperty("secretProperty", JsonValue.from("42"))
1576 .build())
1577 .build();
1578```
1579
1580These properties can be accessed on the nested built object later using the `_additionalProperties()` method.
1581
1582To set a documented parameter or property to an undocumented or not yet supported _value_, pass a [`JsonValue`](openai-java-core/src/main/kotlin/com/openai/core/Values.kt) object to its setter:
1583
1584```java
1585import com.openai.core.JsonValue;
1586import com.openai.models.ChatModel;
1587import com.openai.models.chat.completions.ChatCompletionCreateParams;
1588
1589ChatCompletionCreateParams params = ChatCompletionCreateParams.builder()
1590 .messages(JsonValue.from(42))
1591 .model(ChatModel.GPT_4_1)
1592 .build();
1593```
1594
1595The most straightforward way to create a [`JsonValue`](openai-java-core/src/main/kotlin/com/openai/core/Values.kt) is using its `from(...)` method:
1596
1597```java
1598import com.openai.core.JsonValue;
1599import java.util.List;
1600import java.util.Map;
1601
1602// Create primitive JSON values
1603JsonValue nullValue = JsonValue.from(null);
1604JsonValue booleanValue = JsonValue.from(true);
1605JsonValue numberValue = JsonValue.from(42);
1606JsonValue stringValue = JsonValue.from("Hello World!");
1607
1608// Create a JSON array value equivalent to `["Hello", "World"]`
1609JsonValue arrayValue = JsonValue.from(List.of(
1610 "Hello", "World"
1611));
1612
1613// Create a JSON object value equivalent to `{ "a": 1, "b": 2 }`
1614JsonValue objectValue = JsonValue.from(Map.of(
1615 "a", 1,
1616 "b", 2
1617));
1618
1619// Create an arbitrarily nested JSON equivalent to:
1620// {
1621// "a": [1, 2],
1622// "b": [3, 4]
1623// }
1624JsonValue complexValue = JsonValue.from(Map.of(
1625 "a", List.of(
1626 1, 2
1627 ),
1628 "b", List.of(
1629 3, 4
1630 )
1631));
1632```
1633
1634Normally a `Builder` class's `build` method will throw [`IllegalStateException`](https://docs.oracle.com/javase/8/docs/api/java/lang/IllegalStateException.html) if any required parameter or property is unset.
1635
1636To forcibly omit a required parameter or property, pass [`JsonMissing`](openai-java-core/src/main/kotlin/com/openai/core/Values.kt):
1637
1638```java
1639import com.openai.core.JsonMissing;
1640import com.openai.models.ChatModel;
1641import com.openai.models.chat.completions.ChatCompletionCreateParams;
1642
1643ChatCompletionCreateParams params = ChatCompletionCreateParams.builder()
1644 .model(ChatModel.GPT_4_1)
1645 .messages(JsonMissing.of())
1646 .build();
1647```
1648
1649### Response properties
1650
1651To access undocumented response properties, call the `_additionalProperties()` method:
1652
1653```java
1654import com.openai.core.JsonValue;
1655import java.util.Map;
1656
1657Map<String, JsonValue> additionalProperties = client.chat().completions().create(params)._additionalProperties();
1658JsonValue secretPropertyValue = additionalProperties.get("secretProperty");
1659
1660String result = secretPropertyValue.accept(new JsonValue.Visitor<>() {
1661 @Override
1662 public String visitNull() {
1663 return "It's null!";
1664 }
1665
1666 @Override
1667 public String visitBoolean(boolean value) {
1668 return "It's a boolean!";
1669 }
1670
1671 @Override
1672 public String visitNumber(Number value) {
1673 return "It's a number!";
1674 }
1675
1676 // Other methods include `visitMissing`, `visitString`, `visitArray`, and `visitObject`
1677 // The default implementation of each unimplemented method delegates to `visitDefault`, which throws by default, but can also be overridden
1678});
1679```
1680
1681To access a property's raw JSON value, which may be undocumented, call its `_` prefixed method:
1682
1683```java
1684import com.openai.core.JsonField;
1685import com.openai.models.chat.completions.ChatCompletionMessageParam;
1686import java.util.Optional;
1687
1688JsonField<List<ChatCompletionMessageParam>> messages = client.chat().completions().create(params)._messages();
1689
1690if (messages.isMissing()) {
1691 // The property is absent from the JSON response
1692} else if (messages.isNull()) {
1693 // The property was set to literal null
1694} else {
1695 // Check if value was provided as a string
1696 // Other methods include `asNumber()`, `asBoolean()`, etc.
1697 Optional<String> jsonString = messages.asString();
1698
1699 // Try to deserialize into a custom type
1700 MyClass myObject = messages.asUnknown().orElseThrow().convert(MyClass.class);
1701}
1702```
1703
1704### Response validation
1705
1706In rare cases, the API may return a response that doesn't match the expected type. For example, the SDK may expect a property to contain a `String`, but the API could return something else.
1707
1708By default, the SDK will not throw an exception in this case. It will throw [`OpenAIInvalidDataException`](openai-java-core/src/main/kotlin/com/openai/errors/OpenAIInvalidDataException.kt) only if you directly access the property.
1709
1710If you would prefer to check that the response is completely well-typed upfront, then either call `validate()`:
1711
1712```java
1713import com.openai.models.chat.completions.ChatCompletion;
1714
1715ChatCompletion chatCompletion = client.chat().completions().create(params).validate();
1716```
1717
1718Or configure the method call to validate the response using the `responseValidation` method:
1719
1720```java
1721import com.openai.models.chat.completions.ChatCompletion;
1722
1723ChatCompletion chatCompletion = client.chat().completions().create(
1724 params, RequestOptions.builder().responseValidation(true).build()
1725);
1726```
1727
1728Or configure the default for all method calls at the client level:
1729
1730```java
1731import com.openai.client.OpenAIClient;
1732import com.openai.client.okhttp.OpenAIOkHttpClient;
1733
1734OpenAIClient client = OpenAIOkHttpClient.builder()
1735 .fromEnv()
1736 .responseValidation(true)
1737 .build();
1738```
1739
1740## FAQ
1741
1742### Why don't you use plain `enum` classes?
1743
1744Java `enum` classes are not trivially [forwards compatible](https://www.stainless.com/blog/making-java-enums-forwards-compatible). Using them in the SDK could cause runtime exceptions if the API is updated to respond with a new enum value.
1745
1746### Why do you represent fields using `JsonField<T>` instead of just plain `T`?
1747
1748Using `JsonField<T>` enables a few features:
1749
1750- Allowing usage of [undocumented API functionality](#undocumented-api-functionality)
1751- Lazily [validating the API response against the expected shape](#response-validation)
1752- Representing absent vs explicitly null values
1753
1754### Why don't you use [`data` classes](https://kotlinlang.org/docs/data-classes.html)?
1755
1756It is not [backwards compatible to add new fields to a data class](https://kotlinlang.org/docs/api-guidelines-backward-compatibility.html#avoid-using-data-classes-in-your-api) and we don't want to introduce a breaking change every time we add a field to a class.
1757
1758### Why don't you use checked exceptions?
1759
1760Checked exceptions are widely considered a mistake in the Java programming language. In fact, they were omitted from Kotlin for this reason.
1761
1762Checked exceptions:
1763
1764- Are verbose to handle
1765- Encourage error handling at the wrong level of abstraction, where nothing can be done about the error
1766- Are tedious to propagate due to the [function coloring problem](https://journal.stuffwithstuff.com/2015/02/01/what-color-is-your-function)
1767- Don't play well with lambdas (also due to the function coloring problem)
1768
1769## Semantic versioning
1770
1771This package generally follows [SemVer](https://semver.org/spec/v2.0.0.html) conventions, though certain backwards-incompatible changes may be released as minor versions:
1772
17731. Changes to library internals which are technically public but not intended or documented for external use. _(Please open a GitHub issue to let us know if you are relying on such internals.)_
17742. Changes that we do not expect to impact the vast majority of users in practice.
1775
1776We take backwards-compatibility seriously and work hard to ensure you can rely on a smooth upgrade experience.
1777
1778We are keen for your feedback; please open an [issue](https://www.github.com/openai/openai-java/issues) with questions, bugs, or suggestions.
1779