Skip to content

feat: openai tts support streaming realtime audio - #2086

Merged
creamlike1024 merged 1 commit into
QuantumNous:mainfrom
feitianbubu:pr/openai-tts-stream
Oct 22, 2025
Merged

creamlike1024 merged 1 commit into
QuantumNous:mainfrom
feitianbubu:pr/openai-tts-stream

Conversation

@feitianbubu

@feitianbubu feitianbubu commented Oct 22, 2025 •

Copy link
Copy Markdown
Member

OpenAI TTS支持流式实时音频

Summary by CodeRabbit

  • Bug Fixes
    • Improved handling of streaming text-to-speech responses to ensure reliable, real-time delivery of data chunks as they arrive.

@coderabbitai

coderabbitai Bot commented Oct 22, 2025 •

Copy link
Copy Markdown
Contributor

Walkthrough

Introduces a streaming helper function streamTTSResponse to handle chunked HTTP delivery. Modifies the OpenAI TTS handler to detect streaming responses via ContentLength and conditionally route to the streaming function, preserving non-streaming behavior for regular responses.

Changes

Cohort / File(s) Change Summary
OpenAI TTS Streaming Support
relay/channel/openai/relay-openai.go
Added streamTTSResponse helper function for chunked response delivery. Modified OpenaiTTSHandler to detect streaming responses based on ContentLength and conditionally invoke streaming handler; retains existing behavior for non-streaming responses.

Sequence Diagram

sequenceDiagram
    participant Client
    participant OpenaiTTSHandler
    participant streamTTSResponse
    participant HTTPResponseWriter

    Client->>OpenaiTTSHandler: Request TTS
    OpenaiTTSHandler->>OpenaiTTSHandler: Check ContentLength
    
    alt Streaming Response (ContentLength == 0)
        OpenaiTTSHandler->>streamTTSResponse: Call streamTTSResponse()
        loop For each chunk
            streamTTSResponse->>HTTPResponseWriter: Write chunk
            streamTTSResponse->>HTTPResponseWriter: Flush
        end
        streamTTSResponse->>Client: Stream delivered
    else Non-Streaming Response
        OpenaiTTSHandler->>HTTPResponseWriter: WriteHeader
        OpenaiTTSHandler->>HTTPResponseWriter: Copy body
        OpenaiTTSHandler->>Client: Response sent
    end
Loading

Estimated code review effort

🎯 2 (Simple) | ⏱️ ~10 minutes

Single file with localized changes introducing a new helper function and conditional branching. The modification is straightforward: detecting streaming capability and routing to appropriate handler. Clear separation of concerns between streaming and non-streaming paths minimizes review complexity.

Suggested reviewers

  • creamlike1024

Poem

🐰 Hops through the relay with delight,
Streaming chunks flow smooth and right,
TTS whispers now in pieces small,
Flushed with care, delivered to all! 🎵

Pre-merge checks and finishing touches

❌ Failed checks (1 warning)
Check name Status Explanation Resolution
Docstring Coverage ⚠️ Warning Docstring coverage is 0.00% which is insufficient. The required threshold is 80.00%. You can run @coderabbitai generate docstrings to improve docstring coverage.
✅ Passed checks (2 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title Check ✅ Passed The title "feat: openai tts support streaming realtime audio" directly aligns with the primary change in the changeset. The code modifications introduce a streaming helper function and update the OpenaiTTSHandler to detect and handle streaming responses, which is exactly what the title describes. The title is concise, specific, and uses clear language that accurately conveys the main objective without vague terms or unnecessary details. A teammate reviewing the commit history would immediately understand that this PR adds streaming capability to OpenAI TTS functionality.
✨ Finishing touches
  • 📝 Generate docstrings
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Post copyable unit tests in a comment

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands and usage tips.

@coderabbitai coderabbitai Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actionable comments posted: 0

🧹 Nitpick comments (2)
relay/channel/openai/relay-openai.go (2)

292-292: Remove commented-out debug code.

The commented-out log line should be removed before merging to production.

-		//logger.LogInfo(c, fmt.Sprintf("streamTTSResponse read %d bytes", n))

325-334: Consider checking Transfer-Encoding header for more robust streaming detection.

The OpenAI TTS API uses chunk transfer encoding for streaming, and the standard HTTP way to indicate this is via the Transfer-Encoding: chunked header. While the current logic should work for most cases, consider making it more robust:

-	isStreaming := resp.ContentLength == -1 || resp.Header.Get("Content-Length") == ""
+	isStreaming := resp.ContentLength == -1 || 
+		resp.Header.Get("Content-Length") == "" || 
+		resp.Header.Get("Transfer-Encoding") == "chunked"
📜 Review details

Configuration used: CodeRabbit UI

Review profile: CHILL

Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between 4661399 and afcbff6.

📒 Files selected for processing (1)
  • relay/channel/openai/relay-openai.go (2 hunks)
🧰 Additional context used
🧬 Code graph analysis (1)
relay/channel/openai/relay-openai.go (1)
logger/logger.go (2)
  • LogWarn (61-63)
  • LogError (65-67)
🔇 Additional comments (1)
relay/channel/openai/relay-openai.go (1)

276-307: Solid implementation of streaming TTS support.

The implementation correctly handles both streaming and non-streaming responses with appropriate buffering, flushing, and error handling. The changes maintain backward compatibility and follow the existing patterns in the codebase.

Key strengths:

  • Proper header management and flushing for streaming
  • Graceful fallback when flusher is not available
  • Correct EOF handling (not logged as error)
  • Error logging at appropriate places
  • Reasonable buffer size (4096 bytes) for streaming

Also applies to: 325-334

@creamlike1024
creamlike1024 merged commit e80b442 into QuantumNous:main Oct 22, 2025
ennnnny pushed a commit to ennnnny/new-api that referenced this pull request Mar 17, 2026
…ream

feat: openai tts support streaming realtime audio
salem-2007 added a commit to salem-2007/new-api that referenced this pull request Sep 10, 2026
…ream

feat: openai tts support streaming realtime audio
junjundesk pushed a commit to junjundesk/new-api that referenced this pull request Oct 3, 2026
…ream

feat: openai tts support streaming realtime audio
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants