openai / openai/openai-java

How to use Speech.stream_format=sse

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

Nobody has claimed this yet.

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

Description

Hi there, how to save the response data to a audio file when setting stream_format=sse, my code as below, the audio file can't be played.

And what's the meaning of stream_format=sse, does it like the steaming response with chat completion api ?

    static void speech() throws IOException {
        SpeechCreateParams createParams = SpeechCreateParams.builder()
            .model(SpeechModel.GPT_4O_MINI_TTS)
            .input("金十数据12月31日讯,商务部新闻发言人就荷兰经济大臣卡雷曼斯接受采访答记者问表示,中方已多次强调,荷方对安世半导体企业内部事务的不当行政干预,已导致全球半导体供应链危机,荷方必须对此承担全部责任。")
            .responseFormat(SpeechCreateParams.ResponseFormat.WAV)
            .streamFormat(SpeechCreateParams.StreamFormat.SSE)
            .voice(SpeechCreateParams.Voice.ALLOY)
            .build();

        InputStream inputStream = buildClient().audio()
            .withRawResponse()
            .speech()
            .create(createParams)
            .body();

        Path tempDir = Path.of("temp");
        Files.createDirectories(tempDir);
        Path outputFile = tempDir.resolve(System.currentTimeMillis() + ".wav");
        Files.copy(inputStream, outputFile, StandardCopyOption.REPLACE_EXISTING);
    }

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 with SpeechCreateParams.StreamFormat.SSE and the withRawResponse().speech().create() entry point shown in the report, then compare the returned stream with the Files.copy call. Clarify how SSE responses should be handled for audio output and what stream_format means; done means the usage and expected result are documented.

Written by the indexing model from the issue text.

Assessment

Tech stack
java
Domain
api
Issue type
Documentation
Difficulty
3/5
Estimated time
1-2 days
Activity status
Stale
Clarity
Needs clarification
Newbie friendliness
25/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.