openai / openai/openai-java

API ergonomics enhancement: expose types as enums

Open
#545 0 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

enhancement sdk
Dominant language
Kotlin
Stars
1.5k
Forks
264
Avg merge
9h 46m
Merged PRs (30d)
96

Description

Right now the way the Java SDK works is you have to do things like:

if (event.isOutputTextDelta()) {
  // do stuff with event.asOutputTextDelta()
} else if (event.isCompleted()) {
  // do stuff with event.asCompleted()
} // etc

Since Java 17, the enhanced switch statement makes it really convenient to use enums with switches and it be nice if we could do something like:

switch (event.type()) {
  case OUTPUT_TEXT_DELTA -> // do stuff with event.asOutputTextDelta()
  case COMPLETED -> // do stuff with event.asCompleted()
}

And since Java 21, you can also do switch pattern matching. Unfortunately, this doesn't seem to work because the style the OpenAI SDK uses does not have classes like ResponseStreamEvent as a base for things like ResponseTextDeltaEvent or ResponseCompletedEvent. If it did, we could do:

switch (event) {
  case ResponseTextDeltaEvent e -> // do stuff with e
  case ResponseCompletedEvent e -> // do stuff with e
}

From a dev ergonomics standpoint I'd be nice if support was added for one or both of these :) Thank you for considering it!

PS: the workaround is easy enough, we just maintain something like this:

public enum ResponseStreamEventType {
    OUTPUT_TEXT_DELTA,
    COMPLETED,
    ERROR,
    UNKNOWN;
    
    // TODO: Add the 40+ additional event types such as...
    // SESSION_CREATED, SESSION_UPDATED, CONVERSATION_ITEM_CREATED, CONVERSATION_ITEM_INPUT_AUDIO_TRANSCRIPTION_COMPLETED,
    // CONVERSATION_ITEM_INPUT_AUDIO_TRANSCRIPTION_FAILED, CONVERSATION_ITEM_TRUNCATED, CONVERSATION_ITEM_DELETED,
    // INPUT_AUDIO_BUFFER_COMMITTED, INPUT_AUDIO_BUFFER_CLEARED, INPUT_AUDIO_BUFFER_SPEECH_STARTED, INPUT_AUDIO_BUFFER_SPEECH_STOPPED,
    // RESPONSE_CREATED, RESPONSE_DONE, RESPONSE_OUTPUT_ITEM_ADDED, RESPONSE_OUTPUT_ITEM_DONE, RESPONSE_CONTENT_PART_ADDED,
    // RESPONSE_CONTENT_PART_DONE, RESPONSE_TEXT_DELTA, RESPONSE_TEXT_DONE, RESPONSE_AUDIO_TRANSCRIPT_DELTA,
    // RESPONSE_AUDIO_TRANSCRIPT_DONE, RESPONSE_AUDIO_DELTA, RESPONSE_AUDIO_DONE, RESPONSE_FUNCTION_CALL_ARGUMENTS_DELTA,
    // RESPONSE_FUNCTION_CALL_ARGUMENTS_DONE, RATE_LIMITS_UPDATED, etc.
    // See: https://platform.openai.com/docs/api-reference/responses-streaming

    public static ResponseStreamEventType classify(ResponseStreamEvent event) {
        if (event.isOutputTextDelta()) {
            return OUTPUT_TEXT_DELTA;
        } else if (event.isCompleted()) {
            return COMPLETED;
        } else if (event.isError()) {
            return ERROR;
        } else {
            return UNKNOWN;
        }
    }
}

And then we can switch on the enum that classify(event) returns.

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start by reviewing the Java SDK's ResponseStreamEvent API and the existing isOutputTextDelta(), isCompleted(), as..., and event-type patterns described in the issue. Compare the enum-based event.type() option with the proposed event-class hierarchy, including the listed streaming event variants. Done means a decided, consistent ergonomics improvement supporting the intended switch usage.

Written by the indexing model from the issue text.

Assessment

Tech stack
java
Domain
api, developer-experience
Issue type
Feature
Difficulty
5/5
Estimated time
Over a week
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.