Repository navigation
fix(tts): stream HTTP audio incrementally instead of buffering the full response #168
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -140,22 +140,27 @@ def stream( | |
|
|
||
| payload = request.model_dump(exclude_none=True) | ||
|
|
||
| # Make request with streaming | ||
| # Send the request and return once headers arrive; the body is read | ||
| # lazily so chunks reach the caller as the server produces them. | ||
| response = self._client.request( | ||
| "POST", | ||
| "/v1/tts", | ||
| headers={"Content-Type": "application/msgpack", "model": model}, | ||
| content=ormsgpack.packb(payload), | ||
| request_options=request_options, | ||
| stream=True, | ||
| ) | ||
|
|
||
| # Create generator and wrap with AudioStream | ||
| def _stream(): | ||
| for chunk in response.iter_bytes(): | ||
| if chunk: | ||
| yield chunk | ||
| try: | ||
| for chunk in response.iter_bytes(): | ||
| if chunk: | ||
| yield chunk | ||
| finally: | ||
| response.close() | ||
|
Comment on lines
+160
to
+161
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
If Useful? React with 👍 / 👎.
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Fixed in ef23521. The wrapper now holds the response, so Tests:
coderabbitai[bot] marked this conversation as resolved.
Resolved
|
||
|
|
||
| return AudioStream(_stream()) | ||
| return AudioStream(_stream(), on_close=response.close) | ||
|
|
||
| def convert( | ||
| self, | ||
|
|
@@ -450,22 +455,27 @@ async def stream( | |
|
|
||
| payload = request.model_dump(exclude_none=True) | ||
|
|
||
| # Make request with streaming | ||
| # Send the request and return once headers arrive; the body is read | ||
| # lazily so chunks reach the caller as the server produces them. | ||
| response = await self._client.request( | ||
| "POST", | ||
| "/v1/tts", | ||
| headers={"Content-Type": "application/msgpack", "model": model}, | ||
| content=ormsgpack.packb(payload), | ||
| request_options=request_options, | ||
| stream=True, | ||
| ) | ||
|
|
||
| # Create async generator and wrap with AsyncAudioStream | ||
| async def _stream(): | ||
| async for chunk in response.aiter_bytes(): | ||
| if chunk: | ||
| yield chunk | ||
|
|
||
| return AsyncAudioStream(_stream()) | ||
| try: | ||
| async for chunk in response.aiter_bytes(): | ||
| if chunk: | ||
| yield chunk | ||
| finally: | ||
| await response.aclose() | ||
|
|
||
| return AsyncAudioStream(_stream(), on_close=response.aclose) | ||
|
|
||
| async def convert( | ||
| self, | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Fixed in ef23521.
AudioStream/AsyncAudioStreamnow own the response:close()/aclose()release it, both work as context managers, andcollect()closes when done. Early exit is documented aswith client.tts.stream(...) as stream: ... break.Tests:
test_with_block_closes_after_early_break,test_async_with_closes_after_early_break(fail before this commit, pass after).