Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit 8b04453

Browse files
committedMar 21, 2025
fix(package): make sounddevice and numpy optional dependencies
1 parent fb10934 commit 8b04453

File tree

6 files changed

+46
-13
lines changed

6 files changed

+46
-13
lines changed
 

‎pyproject.toml

+1-2
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,6 @@ dependencies = [
1616
"sniffio",
1717
"tqdm > 4",
1818
"jiter>=0.4.0, <1",
19-
"sounddevice>=0.5.1",
20-
"numpy>=2.0.2",
2119
]
2220
requires-python = ">= 3.8"
2321
classifiers = [
@@ -47,6 +45,7 @@ openai = "openai.cli:main"
4745
[project.optional-dependencies]
4846
realtime = ["websockets >= 13, < 15"]
4947
datalib = ["numpy >= 1", "pandas >= 1.2.3", "pandas-stubs >= 1.1.0.11"]
48+
audio = ["sounddevice>=0.5.1", "numpy>=2.0.2"]
5049

5150
[tool.rye]
5251
managed = true

‎src/openai/_extras/__init__.py

+1
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,3 @@
11
from .numpy_proxy import numpy as numpy, has_numpy as has_numpy
22
from .pandas_proxy import pandas as pandas
3+
from .sounddevice_proxy import sounddevice as sounddevice

‎src/openai/_extras/numpy_proxy.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
import numpy as numpy
1111

1212

13-
NUMPY_INSTRUCTIONS = format_instructions(library="numpy", extra="datalib")
13+
NUMPY_INSTRUCTIONS = format_instructions(library="numpy", extra="audio")
1414

1515

1616
class NumpyProxy(LazyProxy[Any]):
+28
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
from __future__ import annotations
2+
3+
from typing import TYPE_CHECKING, Any
4+
from typing_extensions import override
5+
6+
from .._utils import LazyProxy
7+
from ._common import MissingDependencyError, format_instructions
8+
9+
if TYPE_CHECKING:
10+
import sounddevice as sounddevice # type: ignore
11+
12+
13+
SOUNDDEVICE_INSTRUCTIONS = format_instructions(library="sounddevice", extra="audio")
14+
15+
16+
class SounddeviceProxy(LazyProxy[Any]):
17+
@override
18+
def __load__(self) -> Any:
19+
try:
20+
import sounddevice # type: ignore
21+
except ImportError as err:
22+
raise MissingDependencyError(SOUNDDEVICE_INSTRUCTIONS) from err
23+
24+
return sounddevice
25+
26+
27+
if not TYPE_CHECKING:
28+
sounddevice = SounddeviceProxy()

‎src/openai/helpers/local_audio_player.py

+8-5
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,18 @@
11
# mypy: ignore-errors
2+
from __future__ import annotations
3+
24
import queue
35
import asyncio
46
from typing import Any, Union, Callable, AsyncGenerator, cast
5-
6-
import numpy as np
7-
import sounddevice as sd # type: ignore
8-
import numpy.typing as npt
7+
from typing_extensions import TYPE_CHECKING
98

109
from .. import _legacy_response
10+
from .._extras import numpy as np, sounddevice as sd
1111
from .._response import StreamedBinaryAPIResponse, AsyncStreamedBinaryAPIResponse
1212

13+
if TYPE_CHECKING:
14+
import numpy.typing as npt
15+
1316
SAMPLE_RATE = 24000
1417

1518

@@ -62,7 +65,7 @@ async def play(
6265
if input.dtype == np.int16 and self.dtype == np.float32:
6366
audio_content = (input.astype(np.float32) / 32767.0).reshape(-1, self.channels)
6467
elif input.dtype == np.float32:
65-
audio_content = cast(npt.NDArray[np.float32], input)
68+
audio_content = cast('npt.NDArray[np.float32]', input)
6669
else:
6770
raise ValueError(f"Unsupported dtype: {input.dtype}")
6871
else:

‎src/openai/helpers/microphone.py

+7-5
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,18 @@
11
# mypy: ignore-errors
2+
from __future__ import annotations
3+
24
import io
35
import time
46
import wave
57
import asyncio
68
from typing import Any, Type, Union, Generic, TypeVar, Callable, overload
7-
from typing_extensions import Literal
9+
from typing_extensions import TYPE_CHECKING, Literal
810

9-
import numpy as np
10-
import sounddevice as sd # type: ignore
11-
import numpy.typing as npt
11+
from .._types import FileTypes, FileContent
12+
from .._extras import numpy as np, sounddevice as sd
1213

13-
from openai._types import FileTypes, FileContent
14+
if TYPE_CHECKING:
15+
import numpy.typing as npt
1416

1517
SAMPLE_RATE = 24000
1618

0 commit comments

Comments
 (0)
Please sign in to comment.