@@ -135,12 +135,12 @@ async def create_async_generator(
135135 if "usage" in data :
136136 yield Usage (** data ["usage" ])
137137 if "choices" in data :
138- choice = data ["choices" ][ 0 ]
139- if "content" in choice ["message" ] and choice ["message" ]["content" ]:
138+ choice = next ( iter ( data ["choices" ]), None )
139+ if choice and "content" in choice ["message" ] and choice ["message" ]["content" ]:
140140 yield choice ["message" ]["content" ].strip ()
141141 if "tool_calls" in choice ["message" ]:
142142 yield ToolCalls (choice ["message" ]["tool_calls" ])
143- if "finish_reason" in choice and choice ["finish_reason" ] is not None :
143+ if choice and "finish_reason" in choice and choice ["finish_reason" ] is not None :
144144 yield FinishReason (choice ["finish_reason" ])
145145 return
146146 elif content_type .startswith ("text/event-stream" ):
@@ -153,8 +153,8 @@ async def create_async_generator(
153153 if not model_returned and model :
154154 yield ProviderInfo (** cls .get_dict (), model = model )
155155 model_returned = True
156- choice = data ["choices" ][ 0 ]
157- if "content" in choice ["delta" ] and choice ["delta" ]["content" ]:
156+ choice = next ( iter ( data ["choices" ]), None )
157+ if choice and "content" in choice ["delta" ] and choice ["delta" ]["content" ]:
158158 delta = choice ["delta" ]["content" ]
159159 if first :
160160 delta = delta .lstrip ()
@@ -163,7 +163,7 @@ async def create_async_generator(
163163 yield delta
164164 if "usage" in data and data ["usage" ]:
165165 yield Usage (** data ["usage" ])
166- if "finish_reason" in choice and choice ["finish_reason" ] is not None :
166+ if choice and "finish_reason" in choice and choice ["finish_reason" ] is not None :
167167 yield FinishReason (choice ["finish_reason" ])
168168 break
169169 else :
0 commit comments