Skip to content

[Perf] Fix /audio/speech performance by using shared_sessions - #16739

Merged
AlexsanderHamir merged 13 commits into
mainfrom
litellm_/audio/speech
Dec 1, 2025
Merged

AlexsanderHamir merged 13 commits into
mainfrom
litellm_/audio/speech

Conversation

@AlexsanderHamir

@AlexsanderHamir AlexsanderHamir commented Nov 17, 2025 •

Copy link
Copy Markdown
Contributor

Title

[Perf] Fix /audio/speech performance by using shared_sessions

Relevant issues

Pre-Submission checklist

Please complete all items before asking a LiteLLM maintainer to review your PR

  • I have Added testing in the tests/litellm/ directory, Adding at least 1 test is a hard requirement - see details
  • I have added a screenshot of my new test passing locally
  • My PR passes all unit tests on make test-unit
  • My PR's scope is as isolated as possible, it only solves 1 specific problem

Type

🧹 Refactoring

Changes

  • Change from manual += string concatenation to using a list to accumulate response segments and avoid O(n²) overhead.
    • This optimization doesn't help this specific endpoint because select_data_generator was dead code adding overhead
  • Utilize shared aiohttp sessions in audio speech calls - the router was already adding them to request data, but they weren't being used

Test Results

Screenshot 2025-11-17 at 11 10 21 AM
  • Failures seem unrelated.

Performance Gains

  • Small local tests show a 90% latency improvement with 100 users, which is a very good sign. It’s usually hard to see meaningful performance gains locally, especially with a low number of users.
  • This change makes the /audio/speech endpoint our second fastest endpoint after /realtime.

1KB is too small for audio and is lowering the RPS when testing with medium to large files
Refactor async_data_generator to build streamed text via list accumulation and ''.join() instead of repeated string concatenation. This improves performance for long responses without changing streaming behavior.
Use list accumulation and join in get_response_string to avoid O(n^2) string concatenation and add a brief comment explaining the performance rationale.
The optimizations related to `select_data_generator` had no effect because its output which is the generator wasn't being used.
@vercel

vercel Bot commented Nov 17, 2025 •

Copy link
Copy Markdown

The latest updates on your projects. Learn more about Vercel for GitHub.

Project Deployment Preview Comments Updated (UTC)
litellm Error Error Nov 22, 2025 6:18pm

For anybody that would change this value for whatever reason, the comment makes the tradeoff clear.
Comment thread no_cache_hits.py Outdated

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

can we avoid having test files on the root of the repo? @AlexsanderHamir

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Thank you @krrishdholakia, I forgot to remove it.

@ghost ghost added the changes requested label Nov 18, 2025
@ghost

ghost commented Nov 18, 2025 •

Copy link
Copy Markdown

minor cleanup on test files needed

@ishaan-jaff ishaan-jaff 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.

Comment thread litellm/proxy/proxy_server.py Outdated
# too small: latency is high
# too large: latency is low, but memory usage is high
# 8192 is a good compromise
_generator = await _response.aiter_bytes(chunk_size=8192)

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.

8192 should be in constants

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Thank you, done!

@AlexsanderHamir
AlexsanderHamir merged commit b949ec9 into main Dec 1, 2025
44 of 59 checks passed
@ishaan-berri
ishaan-berri deleted the litellm_/audio/speech branch March 26, 2026 21:50
fzowl pushed a commit to fzowl/litellm that referenced this pull request Jun 24, 2026
[Perf] Fix `/audio/speech` performance by using `shared_sessions`

This branch had an error being deployed

1 failed deployment
Preview — 241d4abd Deployed Nov 22, 2025 by vercel[bot]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants