File tree Expand file tree Collapse file tree
Expand file tree Collapse file tree Original file line number Diff line number Diff line change @@ -60,7 +60,7 @@ async def stream_chat_completion_chunks(
6060
6161 # Handle pre-computed output: emit value as a single content chunk
6262 if output .is_computed ():
63- if output .value :
63+ if output .value is not None :
6464 chunk = ChatCompletionChunk (
6565 id = completion_id ,
6666 model = model ,
Original file line number Diff line number Diff line change @@ -132,6 +132,38 @@ async def mock_astream():
132132 assert parsed [- 1 ]["choices" ][0 ]["finish_reason" ] == "stop"
133133 assert parsed [- 1 ]["usage" ]["total_tokens" ] == 8
134134
135+ @pytest .mark .asyncio
136+ async def test_stream_chat_completion_chunks_preserves_empty_precomputed_chunk (
137+ self ,
138+ ):
139+ """Test helper emits an explicit empty content chunk for precomputed output."""
140+ output = ModelOutputThunk ("" )
141+ output .usage = {"prompt_tokens" : 1 , "completion_tokens" : 0 , "total_tokens" : 1 }
142+
143+ events = []
144+ async for event in stream_chat_completion_chunks (
145+ output = output ,
146+ completion_id = "chatcmpl-test123" ,
147+ model = "test-model" ,
148+ created = 123 ,
149+ stream_options = StreamOptions (include_usage = True ),
150+ ):
151+ events .append (event )
152+
153+ assert events [- 1 ] == "data: [DONE]\n \n "
154+
155+ parsed = [
156+ json .loads (event [6 :].strip ())
157+ for event in events
158+ if event .startswith ("data: " ) and event != "data: [DONE]\n \n "
159+ ]
160+
161+ assert len (parsed ) == 3
162+ assert parsed [0 ]["choices" ][0 ]["delta" ]["role" ] == "assistant"
163+ assert parsed [1 ]["choices" ][0 ]["delta" ]["content" ] == ""
164+ assert parsed [2 ]["choices" ][0 ]["finish_reason" ] == "stop"
165+ assert parsed [2 ]["usage" ]["total_tokens" ] == 1
166+
135167 @pytest .mark .asyncio
136168 async def test_stream_chat_completion_chunks_emits_error_event (self ):
137169 """Test helper emits an error payload and [DONE] when streaming fails."""
You can’t perform that action at this time.
0 commit comments