Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 34 additions & 0 deletions src/openai/lib/streaming/_assistants.py
Original file line number Diff line number Diff line change
Expand Up @@ -977,14 +977,48 @@ def accumulate_event(
return current_message_snapshot, new_content


def _merge_indexed_list(items: list[object]) -> list[object]:
"""Merge list entries that share the same ``index`` value.

Streaming chunks may contain multiple entries with the same logical
``index`` (e.g. a tool-call name and its first argument fragment in one
SSE event). When such a list is stored for the first time it must be
collapsed by logical index so that later chunks merge correctly.
"""
if not items or not is_dict(items[0]) or "index" not in items[0]: # type: ignore[arg-type]
return items

merged: dict[int, object] = {}
order: list[int] = []
for item in items:
if not is_dict(item):
continue
idx = item.get("index") # type: ignore[union-attr]
if not isinstance(idx, int):
continue
if idx in merged:
existing = merged[idx]
if is_dict(existing):
merged[idx] = accumulate_delta(existing, item) # type: ignore[arg-type]
else:
order.append(idx)
merged[idx] = item

return [merged[i] for i in order]


def accumulate_delta(acc: dict[object, object], delta: dict[object, object]) -> dict[object, object]:
for key, delta_value in delta.items():
if key not in acc:
if is_list(delta_value):
delta_value = _merge_indexed_list(delta_value)
acc[key] = delta_value
continue

acc_value = acc[key]
if acc_value is None:
if is_list(delta_value):
delta_value = _merge_indexed_list(delta_value)
acc[key] = delta_value
continue

Expand Down
34 changes: 34 additions & 0 deletions src/openai/lib/streaming/_deltas.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,48 @@
from ..._utils import is_dict, is_list


def _merge_indexed_list(items: list[object]) -> list[object]:
"""Merge list entries that share the same ``index`` value.

Streaming chunks may contain multiple entries with the same logical
``index`` (e.g. a tool-call name and its first argument fragment in one
SSE event). When such a list is stored for the first time it must be
collapsed by logical index so that later chunks merge correctly.
"""
if not items or not is_dict(items[0]) or "index" not in items[0]: # type: ignore[arg-type]
return items

merged: dict[int, object] = {}
order: list[int] = []
for item in items:
if not is_dict(item):
continue
idx = item.get("index") # type: ignore[union-attr]
if not isinstance(idx, int):
continue
Comment on lines +20 to +24
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P2 Badge Do not silently drop malformed indexed list entries

When _merge_indexed_list() is triggered (first item is a dict with index), entries that are not dicts or whose index is missing/non-int are skipped via continue, which silently removes data from the stream snapshot. This differs from the existing list-merge path in accumulate_delta() that explicitly raises on invalid indexed entries, so malformed-but-present payloads now become silent corruption instead of a detectable error. In streams where providers emit mixed or partially malformed tool_calls arrays, this can produce incomplete final messages without surfacing any failure.

Useful? React with 👍 / 👎.

if idx in merged:
existing = merged[idx]
if is_dict(existing):
merged[idx] = accumulate_delta(existing, item) # type: ignore[arg-type]
else:
order.append(idx)
merged[idx] = item

return [merged[i] for i in order]


def accumulate_delta(acc: dict[object, object], delta: dict[object, object]) -> dict[object, object]:
for key, delta_value in delta.items():
if key not in acc:
if is_list(delta_value):
delta_value = _merge_indexed_list(delta_value)
acc[key] = delta_value
continue

acc_value = acc[key]
if acc_value is None:
if is_list(delta_value):
delta_value = _merge_indexed_list(delta_value)
acc[key] = delta_value
continue

Expand Down