File tree Expand file tree Collapse file tree
Expand file tree Collapse file tree Original file line number Diff line number Diff line change @@ -1102,7 +1102,7 @@ def _process_line(line: str) -> typing.Iterator[bytes]:
11021102 if is_v2 :
11031103 if emitted_start :
11041104 if not emitted_content_end :
1105- yield _emit_v2_event ({"type" : "content-end" , "index" : 0 })
1105+ yield _emit_v2_event ({"type" : "content-end" , "index" : current_content_index })
11061106 message_end_event : typing .Dict [str , typing .Any ] = {
11071107 "type" : "message-end" ,
11081108 "id" : generation_id ,
Original file line number Diff line number Diff line change @@ -952,6 +952,26 @@ def test_stream_wrapper_skips_message_end_for_empty_v2_stream(self):
952952
953953 self .assertEqual (events , [])
954954
955+ def test_stream_wrapper_done_uses_current_content_index_after_transition (self ):
956+ """Test fallback content-end uses the latest content index after type transitions."""
957+ import json
958+ from cohere .oci_client import transform_oci_stream_wrapper
959+
960+ chunks = [
961+ b'data: {"message": {"content": [{"type": "THINKING", "thinking": "Reasoning..."}]}}\n ' ,
962+ b'data: {"message": {"content": [{"type": "TEXT", "text": "Answer"}]}}\n ' ,
963+ b"data: [DONE]\n " ,
964+ ]
965+
966+ events = []
967+ for raw in transform_oci_stream_wrapper (iter (chunks ), "chat" , is_v2 = True ):
968+ line = raw .decode ("utf-8" ).strip ()
969+ if line .startswith ("data: " ):
970+ events .append (json .loads (line [6 :]))
971+
972+ self .assertEqual (events [- 2 ], {"type" : "content-end" , "index" : 1 })
973+ self .assertEqual (events [- 1 ]["type" ], "message-end" )
974+
955975 def test_v1_stream_wrapper_preserves_finish_reason_in_stream_end (self ):
956976 """Test that V1 stream-end uses the OCI finish reason from the final event."""
957977 import json
You can’t perform that action at this time.
0 commit comments