Commit f034ed1
Python: fix: ChatHistoryTruncationReducer orphans TOOL role messages (#13608)
## Summary
Fixes #12708
`ChatHistoryTruncationReducer.reduce()` can orphan `TOOL` role messages
by truncating the preceding `assistant` message that contains the
`tool_calls`, causing OpenAI to reject the history with:
```
messages with role 'tool' must be a response to a preceding message with 'tool_calls'
```
## Root Cause
`locate_safe_reduction_index()` uses
`contains_function_call_or_result()` to detect tool-related messages
during its backward scan. This function only checks `msg.items` for
`FunctionCallContent`/`FunctionResultContent` instances.
However, `TOOL` role messages can contain only text content (no
`FunctionResultContent` in `items`), which causes the backward scan to
treat them as regular messages. The truncation point then lands between
the `tool_calls` assistant message and its `TOOL` responses, orphaning
the tool results.
## Fix
Added `AuthorRole.TOOL` check to `contains_function_call_or_result()`:
```python
if msg.role == AuthorRole.TOOL:
return True
```
This ensures **any** `TOOL` role message is recognized as part of a tool
call/result pair, regardless of whether it has `FunctionResultContent`
in its `items`.
## Test
Added
`test_locate_safe_reduction_index_tool_role_without_function_result_content`
that verifies TOOL role messages with only text content are not
separated from their tool call.
---------
Co-authored-by: giulio-leone <giulio.leone@users.noreply.github.com>
Co-authored-by: Evan Mattson <35585003+moonbox3@users.noreply.github.com>
Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>1 parent 6ed2bb2 commit f034ed1
2 files changed
Lines changed: 44 additions & 1 deletion
File tree
- python
- semantic_kernel/contents/history_reducer
- tests/unit/contents
Lines changed: 8 additions & 1 deletion
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
237 | 237 | | |
238 | 238 | | |
239 | 239 | | |
240 | | - | |
| 240 | + | |
| 241 | + | |
| 242 | + | |
| 243 | + | |
| 244 | + | |
| 245 | + | |
| 246 | + | |
| 247 | + | |
241 | 248 | | |
Lines changed: 36 additions & 0 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
194 | 194 | | |
195 | 195 | | |
196 | 196 | | |
| 197 | + | |
| 198 | + | |
| 199 | + | |
| 200 | + | |
| 201 | + | |
| 202 | + | |
| 203 | + | |
| 204 | + | |
| 205 | + | |
| 206 | + | |
| 207 | + | |
| 208 | + | |
| 209 | + | |
| 210 | + | |
| 211 | + | |
| 212 | + | |
| 213 | + | |
| 214 | + | |
| 215 | + | |
| 216 | + | |
| 217 | + | |
| 218 | + | |
| 219 | + | |
| 220 | + | |
| 221 | + | |
| 222 | + | |
| 223 | + | |
| 224 | + | |
| 225 | + | |
| 226 | + | |
| 227 | + | |
| 228 | + | |
| 229 | + | |
| 230 | + | |
| 231 | + | |
| 232 | + | |
0 commit comments