Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
62 changes: 57 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -350,6 +350,53 @@ client.chat()
ChatCompletion chatCompletion = chatCompletionAccumulator.chatCompletion();
```

The SDK provides conveniences for streamed responses. A
[`ResponseAccumulator`](openai-java-core/src/main/kotlin/com/openai/helpers/ResponseAccumulator.kt)
can record the stream of response events as they are processed and accumulate a
[`Response`](openai-java-core/src/main/kotlin/com/openai/models/responses/Response.kt)
object similar to that which would have been returned by the non-streaming API.

For a synchronous response add a
[`Stream.peek()`](https://docs.oracle.com/javase/8/docs/api/java/util/stream/Stream.html#peek-java.util.function.Consumer-)
call to the stream pipeline to accumulate each event:

```java
import com.openai.core.http.StreamResponse;
import com.openai.helpers.ResponseAccumulator;
import com.openai.models.responses.Response;
import com.openai.models.responses.ResponseStreamEvent;

ResponseAccumulator responseAccumulator = ResponseAccumulator.create();

try (StreamResponse<ResponseStreamEvent> streamResponse =
client.responses().createStreaming(createParams)) {
streamResponse.stream()
.peek(responseAccumulator::accumulate)
.flatMap(event -> event.outputTextDelta().stream())
.forEach(textEvent -> System.out.print(textEvent.delta()));
}

Response response = responseAccumulator.response();
```

For an asynchronous response, add the `ResponseAccumulator` to the `subscribe()` call:

```java
import com.openai.helpers.ResponseAccumulator;
import com.openai.models.responses.Response;

ResponseAccumulator responseAccumulator = ResponseAccumulator.create();

client.responses()
.createStreaming(createParams)
.subscribe(event -> responseAccumulator.accumulate(event)
.outputTextDelta().ifPresent(textEvent -> System.out.print(textEvent.delta())))
.onCompleteFuture()
.join();

Response response = responseAccumulator.response();
```

## Structured outputs with JSON schemas

Open AI [Structured Outputs](https://platform.openai.com/docs/guides/structured-outputs?api-mode=chat)
Expand Down Expand Up @@ -527,11 +574,16 @@ For a full example of the usage of _Structured Outputs_ with Streaming and the C
see
[`StructuredOutputsStreamingExample`](openai-java-example/src/main/java/com/openai/example/StructuredOutputsStreamingExample.java).

At present, there is no accumulator for streaming responses using the Responses API. It is still
possible to derive a JSON schema from a Java class and create a streaming response for a
[`StructuredResponseCreateParams`](openai-java-core/src/main/kotlin/com/openai/models/responses/StructuredResponseCreateParams.kt)
object, but there is no helper for deserialization of the response to an instance of that Java
class.
With the Responses API, accumulate events while streaming using the
[`ResponseAccumulator`](openai-java-core/src/main/kotlin/com/openai/helpers/ResponseAccumulator.kt).
Once accumulated, use `ResponseAccumulator.response(Class<T>)` to convert the accumulated `Response`
into a
[`StructuredResponse`](openai-java-core/src/main/kotlin/com/openai/models/responses/StructuredResponse.kt).
The [`StructuredResponse`] can then automatically deserialize the JSON strings into instances of
your Java class.

For a full example of the usage of _Structured Outputs_ with Streaming and the Responses API, see
[`ResponsesStructuredOutputsStreamingExample`](openai-java-example/src/main/java/com/openai/example/ResponsesStructuredOutputsStreamingExample.java).

### Defining JSON schema properties

Expand Down
Loading
Loading