diff --git a/src/openai/lib/streaming/_deltas.py b/src/openai/lib/streaming/_deltas.py index a5e1317612..2efdda31ce 100644 --- a/src/openai/lib/streaming/_deltas.py +++ b/src/openai/lib/streaming/_deltas.py @@ -6,8 +6,13 @@ 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: - acc[key] = delta_value - continue + # Seed indexed-dict lists with [] so duplicate-index entries in the + # first chunk are merged by the list path below instead of stored raw. + if is_list(delta_value) and delta_value and is_dict(delta_value[0]) and "index" in delta_value[0]: + acc[key] = [] + else: + acc[key] = delta_value + continue acc_value = acc[key] if acc_value is None: @@ -33,7 +38,7 @@ def accumulate_delta(acc: dict[object, object], delta: dict[object, object]) -> elif is_list(acc_value) and is_list(delta_value): # for lists of non-dictionary items we'll only ever get new entries # in the array, existing entries will never be changed - if all(isinstance(x, (str, int, float)) for x in acc_value): + if acc_value and all(isinstance(x, (str, int, float)) for x in acc_value): acc_value.extend(delta_value) continue