Skip to content

Commit f8c44d5

Browse files
(fix): projects.stream_audio returns a byte stream
1 parent 0710d29 commit f8c44d5

File tree

3 files changed

+42
-34
lines changed

3 files changed

+42
-34
lines changed

pyproject.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
[tool.poetry]
22
name = "elevenlabs"
3-
version = "v1.2.0"
3+
version = "v1.2.1"
44
description = ""
55
readme = "README.md"
66
authors = []

src/elevenlabs/core/client_wrapper.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ def get_headers(self) -> typing.Dict[str, str]:
1717
headers: typing.Dict[str, str] = {
1818
"X-Fern-Language": "Python",
1919
"X-Fern-SDK-Name": "elevenlabs",
20-
"X-Fern-SDK-Version": "v1.2.0",
20+
"X-Fern-SDK-Version": "v1.2.1",
2121
}
2222
if self._api_key is not None:
2323
headers["xi-api-key"] = self._api_key

src/elevenlabs/projects/client.py

Lines changed: 40 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -439,7 +439,7 @@ def stream_audio(
439439
*,
440440
convert_to_mpeg: typing.Optional[bool] = OMIT,
441441
request_options: typing.Optional[RequestOptions] = None,
442-
) -> None:
442+
) -> typing.Iterator[bytes]:
443443
"""
444444
Stream the audio from a project snapshot.
445445
@@ -458,14 +458,15 @@ def stream_audio(
458458
api_key="YOUR_API_KEY",
459459
)
460460
client.projects.stream_audio(
461-
project_id="project_id",
462-
project_snapshot_id="project_snapshot_id",
461+
project_id="string",
462+
project_snapshot_id="string",
463+
convert_to_mpeg=True,
463464
)
464465
"""
465466
_request: typing.Dict[str, typing.Any] = {}
466467
if convert_to_mpeg is not OMIT:
467468
_request["convert_to_mpeg"] = convert_to_mpeg
468-
_response = self._client_wrapper.httpx_client.request(
469+
with self._client_wrapper.httpx_client.stream(
469470
method="POST",
470471
url=urllib.parse.urljoin(
471472
f"{self._client_wrapper.get_base_url()}/",
@@ -493,18 +494,21 @@ def stream_audio(
493494
else self._client_wrapper.get_timeout(),
494495
retries=0,
495496
max_retries=request_options.get("max_retries") if request_options is not None else 0, # type: ignore
496-
)
497-
if 200 <= _response.status_code < 300:
498-
return
499-
if _response.status_code == 422:
500-
raise UnprocessableEntityError(
501-
typing.cast(HttpValidationError, construct_type(type_=HttpValidationError, object_=_response.json())) # type: ignore
502-
)
503-
try:
504-
_response_json = _response.json()
505-
except JSONDecodeError:
506-
raise ApiError(status_code=_response.status_code, body=_response.text)
507-
raise ApiError(status_code=_response.status_code, body=_response_json)
497+
) as _response:
498+
if 200 <= _response.status_code < 300:
499+
for _chunk in _response.iter_bytes():
500+
yield _chunk
501+
return
502+
_response.read()
503+
if _response.status_code == 422:
504+
raise UnprocessableEntityError(
505+
typing.cast(HttpValidationError, construct_type(type_=HttpValidationError, object_=_response.json())) # type: ignore
506+
)
507+
try:
508+
_response_json = _response.json()
509+
except JSONDecodeError:
510+
raise ApiError(status_code=_response.status_code, body=_response.text)
511+
raise ApiError(status_code=_response.status_code, body=_response_json)
508512

509513
def stream_archive(
510514
self, project_id: str, project_snapshot_id: str, *, request_options: typing.Optional[RequestOptions] = None
@@ -1058,7 +1062,7 @@ async def stream_audio(
10581062
*,
10591063
convert_to_mpeg: typing.Optional[bool] = OMIT,
10601064
request_options: typing.Optional[RequestOptions] = None,
1061-
) -> None:
1065+
) -> typing.AsyncIterator[bytes]:
10621066
"""
10631067
Stream the audio from a project snapshot.
10641068
@@ -1077,14 +1081,15 @@ async def stream_audio(
10771081
api_key="YOUR_API_KEY",
10781082
)
10791083
await client.projects.stream_audio(
1080-
project_id="project_id",
1081-
project_snapshot_id="project_snapshot_id",
1084+
project_id="string",
1085+
project_snapshot_id="string",
1086+
convert_to_mpeg=True,
10821087
)
10831088
"""
10841089
_request: typing.Dict[str, typing.Any] = {}
10851090
if convert_to_mpeg is not OMIT:
10861091
_request["convert_to_mpeg"] = convert_to_mpeg
1087-
_response = await self._client_wrapper.httpx_client.request(
1092+
async with self._client_wrapper.httpx_client.stream(
10881093
method="POST",
10891094
url=urllib.parse.urljoin(
10901095
f"{self._client_wrapper.get_base_url()}/",
@@ -1112,18 +1117,21 @@ async def stream_audio(
11121117
else self._client_wrapper.get_timeout(),
11131118
retries=0,
11141119
max_retries=request_options.get("max_retries") if request_options is not None else 0, # type: ignore
1115-
)
1116-
if 200 <= _response.status_code < 300:
1117-
return
1118-
if _response.status_code == 422:
1119-
raise UnprocessableEntityError(
1120-
typing.cast(HttpValidationError, construct_type(type_=HttpValidationError, object_=_response.json())) # type: ignore
1121-
)
1122-
try:
1123-
_response_json = _response.json()
1124-
except JSONDecodeError:
1125-
raise ApiError(status_code=_response.status_code, body=_response.text)
1126-
raise ApiError(status_code=_response.status_code, body=_response_json)
1120+
) as _response:
1121+
if 200 <= _response.status_code < 300:
1122+
async for _chunk in _response.aiter_bytes():
1123+
yield _chunk
1124+
return
1125+
await _response.aread()
1126+
if _response.status_code == 422:
1127+
raise UnprocessableEntityError(
1128+
typing.cast(HttpValidationError, construct_type(type_=HttpValidationError, object_=_response.json())) # type: ignore
1129+
)
1130+
try:
1131+
_response_json = _response.json()
1132+
except JSONDecodeError:
1133+
raise ApiError(status_code=_response.status_code, body=_response.text)
1134+
raise ApiError(status_code=_response.status_code, body=_response_json)
11271135

11281136
async def stream_archive(
11291137
self, project_id: str, project_snapshot_id: str, *, request_options: typing.Optional[RequestOptions] = None

0 commit comments

Comments
 (0)