Skip to content
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

fix: extract request-id from response headers #479

Open
wants to merge 5 commits into
base: main
Choose a base branch
from
Open
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
60 changes: 42 additions & 18 deletions src/elevenlabs/text_to_speech/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -61,9 +61,9 @@ def convert(
BodyTextToSpeechV1TextToSpeechVoiceIdPostApplyTextNormalization
] = OMIT,
request_options: typing.Optional[RequestOptions] = None,
) -> typing.Iterator[bytes]:
) -> tuple[str, typing.Iterator[bytes]]:
"""
Converts text into speech using a voice of your choice and returns audio.
Converts text into speech using a voice of your choice and returns the request ID and audio stream.

Parameters
----------
Expand Down Expand Up @@ -126,9 +126,11 @@ def convert(
Request-specific configuration. You can pass in configuration such as `chunk_size`, and more to customize the request and response.

Yields
------
typing.Iterator[bytes]
Successful Response
-------
tuple[str, typing.Iterator[bytes]]
A tuple containing:
- request_id: The ID of the request
- audio_stream: Iterator of audio bytes chunks

Examples
--------
Expand Down Expand Up @@ -180,10 +182,20 @@ def convert(
) as _response:
try:
if 200 <= _response.status_code < 300:
_chunk_size = request_options.get("chunk_size", 1024) if request_options is not None else 1024
for _chunk in _response.iter_bytes(chunk_size=_chunk_size):
yield _chunk
return
request_id = _response.headers.get('request-id')
if not request_id:
raise ApiError(
Copy link
Collaborator

Choose a reason for hiding this comment

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

How likely is this to happen?

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

impossible because we set it if not provided by the user

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

i can just leave it as optional alternatively ?

status_code=_response.status_code,
body="Missing request-id in response headers."
)

def audio_iterator():
_chunk_size = request_options.get("chunk_size", 1024) if request_options is not None else 1024
for _chunk in _response.iter_bytes(chunk_size=_chunk_size):
yield _chunk

return request_id, audio_iterator()

_response.read()
if _response.status_code == 422:
raise UnprocessableEntityError(
Expand Down Expand Up @@ -732,9 +744,9 @@ async def convert(
BodyTextToSpeechV1TextToSpeechVoiceIdPostApplyTextNormalization
] = OMIT,
request_options: typing.Optional[RequestOptions] = None,
) -> typing.AsyncIterator[bytes]:
) -> tuple[str, typing.AsyncIterator[bytes]]:
"""
Converts text into speech using a voice of your choice and returns audio.
Converts text into speech using a voice of your choice and returns the request ID and audio stream.

Parameters
----------
Expand Down Expand Up @@ -798,9 +810,11 @@ async def convert(

Yields
------
typing.AsyncIterator[bytes]
Successful Response

tuple[str, typing.AsyncIterator[bytes]]
A tuple containing:
- request_id: The ID of the request
- audio_stream: Iterator of audio bytes chunks

Examples
--------
import asyncio
Expand Down Expand Up @@ -859,10 +873,20 @@ async def main() -> None:
) as _response:
try:
if 200 <= _response.status_code < 300:
_chunk_size = request_options.get("chunk_size", 1024) if request_options is not None else 1024
async for _chunk in _response.aiter_bytes(chunk_size=_chunk_size):
yield _chunk
return
request_id = _response.headers.get('request-id')
if not request_id:
raise ApiError(
status_code=_response.status_code,
body="Missing request-id in response headers."
)

async def audio_iterator():
_chunk_size = request_options.get("chunk_size", 1024) if request_options is not None else 1024
async for _chunk in _response.aiter_bytes(chunk_size=_chunk_size):
yield _chunk

return request_id, audio_iterator()

await _response.aread()
if _response.status_code == 422:
raise UnprocessableEntityError(
Expand Down
Loading