Skip to content

Commit 14b5c6e

Browse files
committed
fix(oci): keep fallback stream index in sync
1 parent 80ed0d7 commit 14b5c6e

2 files changed

Lines changed: 21 additions & 1 deletion

File tree

src/cohere/oci_client.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff 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,

tests/test_oci_client.py

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff 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

0 commit comments

Comments
 (0)