@@ -137,6 +137,7 @@ async def openai_chat_sse_to_responses_sse(stream: AsyncIterator[bytes], *, mode
137137 prompt_tokens = 0
138138 completion_tokens = 0
139139 started = False
140+ text_parts : list [str ] = []
140141 async for obj in _iter_sse_json (stream ):
141142 usage = obj .get ("usage" ) or {}
142143 if usage :
@@ -146,6 +147,7 @@ async def openai_chat_sse_to_responses_sse(stream: AsyncIterator[bytes], *, mode
146147 delta = choice .get ("delta" ) or {}
147148 text = delta .get ("content" )
148149 if text :
150+ text_parts .append (str (text ))
149151 if not started :
150152 started = True
151153 yield _sse ("response.created" , {"type" : "response.created" , "response" : _response_stub (response_id , model , "in_progress" )})
@@ -157,10 +159,15 @@ async def openai_chat_sse_to_responses_sse(stream: AsyncIterator[bytes], *, mode
157159 yield _sse ("response.created" , {"type" : "response.created" , "response" : _response_stub (response_id , model , "in_progress" )})
158160 yield _sse ("response.output_item.added" , {"type" : "response.output_item.added" , "output_index" : 0 , "item" : {"id" : item_id , "type" : "message" , "status" : "in_progress" , "role" : "assistant" , "content" : []}})
159161 yield _sse ("response.content_part.added" , {"type" : "response.content_part.added" , "item_id" : item_id , "output_index" : 0 , "content_index" : content_index , "part" : {"type" : "output_text" , "text" : "" , "annotations" : []}})
160- yield _sse ("response.output_text.done" , {"type" : "response.output_text.done" , "item_id" : item_id , "output_index" : 0 , "content_index" : content_index , "text" : "" })
161- yield _sse ("response.content_part.done" , {"type" : "response.content_part.done" , "item_id" : item_id , "output_index" : 0 , "content_index" : content_index , "part" : {"type" : "output_text" , "text" : "" , "annotations" : []}})
162- yield _sse ("response.output_item.done" , {"type" : "response.output_item.done" , "output_index" : 0 , "item" : {"id" : item_id , "type" : "message" , "status" : "completed" , "role" : "assistant" , "content" : []}})
162+ output_text = "" .join (text_parts )
163+ content = {"type" : "output_text" , "text" : output_text , "annotations" : []}
164+ item = {"id" : item_id , "type" : "message" , "status" : "completed" , "role" : "assistant" , "content" : [content ]}
165+ yield _sse ("response.output_text.done" , {"type" : "response.output_text.done" , "item_id" : item_id , "output_index" : 0 , "content_index" : content_index , "text" : output_text })
166+ yield _sse ("response.content_part.done" , {"type" : "response.content_part.done" , "item_id" : item_id , "output_index" : 0 , "content_index" : content_index , "part" : content })
167+ yield _sse ("response.output_item.done" , {"type" : "response.output_item.done" , "output_index" : 0 , "item" : item })
163168 completed = _response_stub (response_id , model , "completed" )
169+ completed ["output" ] = [item ]
170+ completed ["output_text" ] = output_text
164171 completed ["usage" ] = {"input_tokens" : prompt_tokens , "output_tokens" : completion_tokens , "total_tokens" : prompt_tokens + completion_tokens }
165172 yield _sse ("response.completed" , {"type" : "response.completed" , "response" : completed })
166173 yield b"data: [DONE]\n \n "
0 commit comments