@@ -59,7 +59,7 @@ def __stream__(self) -> Iterator[_T]:
59
59
if sse .data .startswith ("[DONE]" ):
60
60
break
61
61
62
- if sse .event is None or sse . event . startswith ( "response." ) or sse . event . startswith ( "transcript." ) :
62
+ if sse .event == "error" :
63
63
data = sse .json ()
64
64
if is_mapping (data ) and data .get ("error" ):
65
65
message = None
@@ -75,12 +75,13 @@ def __stream__(self) -> Iterator[_T]:
75
75
body = data ["error" ],
76
76
)
77
77
78
- yield process_data (data = data , cast_to = cast_to , response = response )
79
78
79
+ if sse .event and sse .event .startswith ('thread.' ):
80
+ # the assistants API uses a different event shape structure
81
+ yield process_data (data = {"data" : sse .json (), "event" : sse .event }, cast_to = cast_to , response = response )
80
82
else :
81
83
data = sse .json ()
82
-
83
- if sse .event == "error" and is_mapping (data ) and data .get ("error" ):
84
+ if is_mapping (data ) and data .get ("error" ):
84
85
message = None
85
86
error = data .get ("error" )
86
87
if is_mapping (error ):
@@ -94,7 +95,7 @@ def __stream__(self) -> Iterator[_T]:
94
95
body = data ["error" ],
95
96
)
96
97
97
- yield process_data (data = { " data" : data , "event" : sse . event } , cast_to = cast_to , response = response )
98
+ yield process_data (data = data , cast_to = cast_to , response = response )
98
99
99
100
# Ensure the entire stream is consumed
100
101
for _sse in iterator :
@@ -161,7 +162,7 @@ async def __stream__(self) -> AsyncIterator[_T]:
161
162
if sse .data .startswith ("[DONE]" ):
162
163
break
163
164
164
- if sse .event is None or sse . event . startswith ( "response." ) or sse . event . startswith ( "transcript." ) :
165
+ if sse .event == "error" :
165
166
data = sse .json ()
166
167
if is_mapping (data ) and data .get ("error" ):
167
168
message = None
@@ -177,12 +178,13 @@ async def __stream__(self) -> AsyncIterator[_T]:
177
178
body = data ["error" ],
178
179
)
179
180
180
- yield process_data (data = data , cast_to = cast_to , response = response )
181
181
182
+ if sse .event and sse .event .startswith ('thread.' ):
183
+ # the assistants API uses a different event shape structure
184
+ yield process_data (data = {"data" : sse .json (), "event" : sse .event }, cast_to = cast_to , response = response )
182
185
else :
183
186
data = sse .json ()
184
-
185
- if sse .event == "error" and is_mapping (data ) and data .get ("error" ):
187
+ if is_mapping (data ) and data .get ("error" ):
186
188
message = None
187
189
error = data .get ("error" )
188
190
if is_mapping (error ):
@@ -196,7 +198,7 @@ async def __stream__(self) -> AsyncIterator[_T]:
196
198
body = data ["error" ],
197
199
)
198
200
199
- yield process_data (data = { " data" : data , "event" : sse . event } , cast_to = cast_to , response = response )
201
+ yield process_data (data = data , cast_to = cast_to , response = response )
200
202
201
203
# Ensure the entire stream is consumed
202
204
async for _sse in iterator :
0 commit comments