FazBrowse GitHub Viewer
|
Trending
|
URL:
|
Home
Tools:
[Download Repo ZIP]
[View Raw Code]
[Original HTTPS Page]
openai-python/tests/test_streaming.py at main · consumer47/openai-python · GitHub
consumer47
openai-python
Repository navigation
Code
Pull requests
Actions
Projects
Security and quality
Insights
Expand file tree
Breadcrumbs
openai-python
/
tests
/
test_streaming.py
Copy path
More file actions
More file actions
Latest commit
History
History
History
248 lines (182 loc) · 7.78 KB
Breadcrumbs
openai-python
/
tests
/
test_streaming.py
Copy path
File metadata and controls
248 lines (182 loc) · 7.78 KB
Raw
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
from
__future__
import
annotations
from
typing
import
Iterator
,
AsyncIterator
import
httpx
import
pytest
from
openai
import
OpenAI
,
AsyncOpenAI
from
openai
.
_streaming
import
Stream
,
AsyncStream
,
ServerSentEvent
@
pytest
.
mark
.
asyncio
@
pytest
.
mark
.
parametrize
(
"sync"
, [
True
,
False
],
ids
=
[
"sync"
,
"async"
])
async
def
test_basic
(
sync
:
bool
,
client
:
OpenAI
,
async_client
:
AsyncOpenAI
)
->
None
:
def
body
()
->
Iterator
[
bytes
]:
yield
b"event: completion
\n
"
yield
b'data: {"foo":true}
\n
'
yield
b"
\n
"
iterator
=
make_event_iterator
(
content
=
body
(),
sync
=
sync
,
client
=
client
,
async_client
=
async_client
)
sse
=
await
iter_next
(
iterator
)
assert
sse
.
event
==
"completion"
assert
sse
.
json
()
==
{
"foo"
:
True
}
await
assert_empty_iter
(
iterator
)
@
pytest
.
mark
.
asyncio
@
pytest
.
mark
.
parametrize
(
"sync"
, [
True
,
False
],
ids
=
[
"sync"
,
"async"
])
async
def
test_data_missing_event
(
sync
:
bool
,
client
:
OpenAI
,
async_client
:
AsyncOpenAI
)
->
None
:
def
body
()
->
Iterator
[
bytes
]:
yield
b'data: {"foo":true}
\n
'
yield
b"
\n
"
iterator
=
make_event_iterator
(
content
=
body
(),
sync
=
sync
,
client
=
client
,
async_client
=
async_client
)
sse
=
await
iter_next
(
iterator
)
assert
sse
.
event
is
None
assert
sse
.
json
()
==
{
"foo"
:
True
}
await
assert_empty_iter
(
iterator
)
@
pytest
.
mark
.
asyncio
@
pytest
.
mark
.
parametrize
(
"sync"
, [
True
,
False
],
ids
=
[
"sync"
,
"async"
])
async
def
test_event_missing_data
(
sync
:
bool
,
client
:
OpenAI
,
async_client
:
AsyncOpenAI
)
->
None
:
def
body
()
->
Iterator
[
bytes
]:
yield
b"event: ping
\n
"
yield
b"
\n
"
iterator
=
make_event_iterator
(
content
=
body
(),
sync
=
sync
,
client
=
client
,
async_client
=
async_client
)
sse
=
await
iter_next
(
iterator
)
assert
sse
.
event
==
"ping"
assert
sse
.
data
==
""
await
assert_empty_iter
(
iterator
)
@
pytest
.
mark
.
asyncio
@
pytest
.
mark
.
parametrize
(
"sync"
, [
True
,
False
],
ids
=
[
"sync"
,
"async"
])
async
def
test_multiple_events
(
sync
:
bool
,
client
:
OpenAI
,
async_client
:
AsyncOpenAI
)
->
None
:
def
body
()
->
Iterator
[
bytes
]:
yield
b"event: ping
\n
"
yield
b"
\n
"
yield
b"event: completion
\n
"
yield
b"
\n
"
iterator
=
make_event_iterator
(
content
=
body
(),
sync
=
sync
,
client
=
client
,
async_client
=
async_client
)
sse
=
await
iter_next
(
iterator
)
assert
sse
.
event
==
"ping"
assert
sse
.
data
==
""
sse
=
await
iter_next
(
iterator
)
assert
sse
.
event
==
"completion"
assert
sse
.
data
==
""
await
assert_empty_iter
(
iterator
)
@
pytest
.
mark
.
asyncio
@
pytest
.
mark
.
parametrize
(
"sync"
, [
True
,
False
],
ids
=
[
"sync"
,
"async"
])
async
def
test_multiple_events_with_data
(
sync
:
bool
,
client
:
OpenAI
,
async_client
:
AsyncOpenAI
)
->
None
:
def
body
()
->
Iterator
[
bytes
]:
yield
b"event: ping
\n
"
yield
b'data: {"foo":true}
\n
'
yield
b"
\n
"
yield
b"event: completion
\n
"
yield
b'data: {"bar":false}
\n
'
yield
b"
\n
"
iterator
=
make_event_iterator
(
content
=
body
(),
sync
=
sync
,
client
=
client
,
async_client
=
async_client
)
sse
=
await
iter_next
(
iterator
)
assert
sse
.
event
==
"ping"
assert
sse
.
json
()
==
{
"foo"
:
True
}
sse
=
await
iter_next
(
iterator
)
assert
sse
.
event
==
"completion"
assert
sse
.
json
()
==
{
"bar"
:
False
}
await
assert_empty_iter
(
iterator
)
@
pytest
.
mark
.
asyncio
@
pytest
.
mark
.
parametrize
(
"sync"
, [
True
,
False
],
ids
=
[
"sync"
,
"async"
])
async
def
test_multiple_data_lines_with_empty_line
(
sync
:
bool
,
client
:
OpenAI
,
async_client
:
AsyncOpenAI
)
->
None
:
def
body
()
->
Iterator
[
bytes
]:
yield
b"event: ping
\n
"
yield
b"data: {
\n
"
yield
b'data: "foo":
\n
'
yield
b"data:
\n
"
yield
b"data:
\n
"
yield
b"data: true}
\n
"
yield
b"
\n
\n
"
iterator
=
make_event_iterator
(
content
=
body
(),
sync
=
sync
,
client
=
client
,
async_client
=
async_client
)
sse
=
await
iter_next
(
iterator
)
assert
sse
.
event
==
"ping"
assert
sse
.
json
()
==
{
"foo"
:
True
}
assert
sse
.
data
==
'{
\n
"foo":
\n
\n
\n
true}'
await
assert_empty_iter
(
iterator
)
@
pytest
.
mark
.
asyncio
@
pytest
.
mark
.
parametrize
(
"sync"
, [
True
,
False
],
ids
=
[
"sync"
,
"async"
])
async
def
test_data_json_escaped_double_new_line
(
sync
:
bool
,
client
:
OpenAI
,
async_client
:
AsyncOpenAI
)
->
None
:
def
body
()
->
Iterator
[
bytes
]:
yield
b"event: ping
\n
"
yield
b'data: {"foo": "my long
\\
n
\\
ncontent"}'
yield
b"
\n
\n
"
iterator
=
make_event_iterator
(
content
=
body
(),
sync
=
sync
,
client
=
client
,
async_client
=
async_client
)
sse
=
await
iter_next
(
iterator
)
assert
sse
.
event
==
"ping"
assert
sse
.
json
()
==
{
"foo"
:
"my long
\n
\n
content"
}
await
assert_empty_iter
(
iterator
)
@
pytest
.
mark
.
asyncio
@
pytest
.
mark
.
parametrize
(
"sync"
, [
True
,
False
],
ids
=
[
"sync"
,
"async"
])
async
def
test_multiple_data_lines
(
sync
:
bool
,
client
:
OpenAI
,
async_client
:
AsyncOpenAI
)
->
None
:
def
body
()
->
Iterator
[
bytes
]:
yield
b"event: ping
\n
"
yield
b"data: {
\n
"
yield
b'data: "foo":
\n
'
yield
b"data: true}
\n
"
yield
b"
\n
\n
"
iterator
=
make_event_iterator
(
content
=
body
(),
sync
=
sync
,
client
=
client
,
async_client
=
async_client
)
sse
=
await
iter_next
(
iterator
)
assert
sse
.
event
==
"ping"
assert
sse
.
json
()
==
{
"foo"
:
True
}
await
assert_empty_iter
(
iterator
)
@
pytest
.
mark
.
parametrize
(
"sync"
, [
True
,
False
],
ids
=
[
"sync"
,
"async"
])
async
def
test_special_new_line_character
(
sync
:
bool
,
client
:
OpenAI
,
async_client
:
AsyncOpenAI
,
)
->
None
:
def
body
()
->
Iterator
[
bytes
]:
yield
b'data: {"content":" culpa"}
\n
'
yield
b"
\n
"
yield
b'data: {"content":"
\xe2
\x80
\xa8
"}
\n
'
yield
b"
\n
"
yield
b'data: {"content":"foo"}
\n
'
yield
b"
\n
"
iterator
=
make_event_iterator
(
content
=
body
(),
sync
=
sync
,
client
=
client
,
async_client
=
async_client
)
sse
=
await
iter_next
(
iterator
)
assert
sse
.
event
is
None
assert
sse
.
json
()
==
{
"content"
:
" culpa"
}
sse
=
await
iter_next
(
iterator
)
assert
sse
.
event
is
None
assert
sse
.
json
()
==
{
"content"
:
" "
}
sse
=
await
iter_next
(
iterator
)
assert
sse
.
event
is
None
assert
sse
.
json
()
==
{
"content"
:
"foo"
}
await
assert_empty_iter
(
iterator
)
@
pytest
.
mark
.
parametrize
(
"sync"
, [
True
,
False
],
ids
=
[
"sync"
,
"async"
])
async
def
test_multi_byte_character_multiple_chunks
(
sync
:
bool
,
client
:
OpenAI
,
async_client
:
AsyncOpenAI
,
)
->
None
:
def
body
()
->
Iterator
[
bytes
]:
yield
b'data: {"content":"'
# bytes taken from the string 'известни' and arbitrarily split
# so that some multi-byte characters span multiple chunks
yield
b"
\xd0
"
yield
b"
\xb8
\xd0
\xb7
\xd0
"
yield
b"
\xb2
\xd0
\xb5
\xd1
\x81
\xd1
\x82
\xd0
\xbd
\xd0
\xb8
"
yield
b'"}
\n
'
yield
b"
\n
"
iterator
=
make_event_iterator
(
content
=
body
(),
sync
=
sync
,
client
=
client
,
async_client
=
async_client
)
sse
=
await
iter_next
(
iterator
)
assert
sse
.
event
is
None
assert
sse
.
json
()
==
{
"content"
:
"известни"
}
async
def
to_aiter
(
iter
:
Iterator
[
bytes
])
->
AsyncIterator
[
bytes
]:
for
chunk
in
iter
:
yield
chunk
async
def
iter_next
(
iter
:
Iterator
[
ServerSentEvent
]
|
AsyncIterator
[
ServerSentEvent
])
->
ServerSentEvent
:
if
isinstance
(
iter
,
AsyncIterator
):
return
await
iter
.
__anext__
()
return
next
(
iter
)
async
def
assert_empty_iter
(
iter
:
Iterator
[
ServerSentEvent
]
|
AsyncIterator
[
ServerSentEvent
])
->
None
:
with
pytest
.
raises
((
StopAsyncIteration
,
RuntimeError
)):
await
iter_next
(
iter
)
def
make_event_iterator
(
content
:
Iterator
[
bytes
],
*
,
sync
:
bool
,
client
:
OpenAI
,
async_client
:
AsyncOpenAI
,
)
->
Iterator
[
ServerSentEvent
]
|
AsyncIterator
[
ServerSentEvent
]:
if
sync
:
return
Stream
(
cast_to
=
object
,
client
=
client
,
response
=
httpx
.
Response
(
200
,
content
=
content
)).
_iter_events
()
return
AsyncStream
(
cast_to
=
object
,
client
=
async_client
,
response
=
httpx
.
Response
(
200
,
content
=
to_aiter
(
content
))
).
_iter_events
()
Back
|
FazBrowse Home
|
New Git URL