1{
2 lib,
3 buildPythonPackage,
4 fetchFromGitHub,
5 pythonAtLeast,
6 pythonOlder,
7
8 # build-system
9 hatchling,
10 hatch-fancy-pypi-readme,
11
12 # dependencies
13 anyio,
14 distro,
15 httpx,
16 jiter,
17 pydantic,
18 sniffio,
19 tqdm,
20 typing-extensions,
21
22 # optional-dependencies (datalib)
23 numpy,
24 pandas,
25 pandas-stubs,
26
27 # optional-dependencies (realtime)
28 websockets,
29
30 # optional-dependencies (voice-helpers)
31 sounddevice,
32
33 # check deps
34 pytestCheckHook,
35 dirty-equals,
36 inline-snapshot,
37 nest-asyncio,
38 pytest-asyncio,
39 pytest-mock,
40 respx,
41
42 # optional-dependencies toggle
43 withRealtime ? true,
44 withVoiceHelpers ? true,
45}:
46
47buildPythonPackage rec {
48 pname = "openai";
49 version = "1.78.1";
50 pyproject = true;
51
52 disabled = pythonOlder "3.8";
53
54 src = fetchFromGitHub {
55 owner = "openai";
56 repo = "openai-python";
57 tag = "v${version}";
58 hash = "sha256-rdK4usuFVhPpDL3jnSkapJfDfLOI5EH8zeS0a7xoy/g=";
59 };
60
61 postPatch = ''substituteInPlace pyproject.toml --replace-fail "hatchling==1.26.3" "hatchling"'';
62
63 build-system = [
64 hatchling
65 hatch-fancy-pypi-readme
66 ];
67
68 dependencies =
69 [
70 anyio
71 distro
72 httpx
73 jiter
74 pydantic
75 sniffio
76 tqdm
77 typing-extensions
78 ]
79 ++ lib.optionals withRealtime optional-dependencies.realtime
80 ++ lib.optionals withVoiceHelpers optional-dependencies.voice-helpers;
81
82 optional-dependencies = {
83 datalib = [
84 numpy
85 pandas
86 pandas-stubs
87 ];
88 realtime = [
89 websockets
90 ];
91 voice-helpers = [
92 numpy
93 sounddevice
94 ];
95 };
96
97 pythonImportsCheck = [ "openai" ];
98
99 nativeCheckInputs = [
100 pytestCheckHook
101 dirty-equals
102 inline-snapshot
103 nest-asyncio
104 pytest-asyncio
105 pytest-mock
106 respx
107 ];
108
109 pytestFlagsArray = [
110 "-W"
111 "ignore::DeprecationWarning"
112 # snapshot mismatches
113 "--inline-snapshot=update"
114 ];
115
116 disabledTests =
117 [
118 # Tests make network requests
119 "test_copy_build_request"
120 "test_basic_attribute_access_works"
121 ]
122 ++ lib.optionals (pythonAtLeast "3.13") [
123 # RuntimeWarning: coroutine method 'aclose' of 'AsyncStream._iter_events' was never awaited
124 "test_multi_byte_character_multiple_chunks"
125 ];
126
127 disabledTestPaths = [
128 # Test makes network requests
129 "tests/api_resources"
130 ];
131
132 meta = with lib; {
133 description = "Python client library for the OpenAI API";
134 homepage = "https://github.com/openai/openai-python";
135 changelog = "https://github.com/openai/openai-python/blob/v${version}/CHANGELOG.md";
136 license = licenses.mit;
137 maintainers = with maintainers; [ malo ];
138 mainProgram = "openai";
139 };
140}