Skip to content

Commit 5590449

Browse files
committed
Python: Keep interleaved messages in order when keeping a call/result group
Per review: the keep path still appended later results right after the call and removed them from the working slice, so a history such as [call, user, result] came back as [call, result, user]. Append only the current message and let each result be appended when its own index is reached. The skip path still drops the whole group. Extends test_extract_range_preserve_pairs_parallel_calls_filtered_result with a message interleaved between a parallel call and its results. Signed-off-by: Pujitha Paladugu <10557236+pujitha24@users.noreply.github.com>
1 parent d99ee19 commit 5590449

2 files changed

Lines changed: 8 additions & 8 deletions

File tree

‎python/semantic_kernel/contents/history_reducer/chat_history_reducer_utils.py‎

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -245,15 +245,9 @@ def extract_range(
245245
# remove it from the slice so we don't process it again
246246
sliced.remove(paired_idx)
247247
continue
248-
# keep all
248+
# keep all: each paired message is appended when its own index is reached,
249+
# so messages interleaved between a call and its results keep their order
249250
extracted.append(msg)
250-
for paired_idx in paired_indices:
251-
if paired_idx > idx and paired_idx in sliced:
252-
# We'll skip the pair in the normal iteration by removing from slice
253-
# but add it to extracted right now
254-
extracted.append(history[paired_idx])
255-
sliced.remove(paired_idx)
256-
# if paired_idx < idx, it was already processed, so do not add duplicates
257251
i += 1
258252
continue
259253
# If the paired_idx is outside [start, end), there's no conflict

‎python/tests/unit/contents/test_chat_history_reducer_utils.py‎

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -274,3 +274,9 @@ def test_extract_range_preserve_pairs_parallel_calls_filtered_result():
274274
history, start=2, end=len(history), filter_func=lambda m: m is result_paris, preserve_pairs=True
275275
)
276276
assert extracted == [result_tokyo, done]
277+
278+
# A message interleaved between the call and its results keeps its position
279+
note = ChatMessageContent(role=AuthorRole.USER, content="also Tokyo")
280+
history = [call, note, result_paris, result_tokyo]
281+
extracted = extract_range(history, start=0, end=len(history), preserve_pairs=True)
282+
assert extracted == history

0 commit comments

Comments
 (0)