Skip to content

Conversation

@GangGreenTemperTatum
Copy link
Contributor

@GangGreenTemperTatum GangGreenTemperTatum commented Jan 21, 2026

[HOOKS] summarize_when_long with no tool call response

Key Changes:

  • aims to fix an issue i experienced when using OpenAI as the driving model and the summarize_when_long hook is invoked, without anything specifically in code to satisfy the OpenAI api (though i couldn't find anything in litellm for this), where the error would look something like:
2026-01-21 14:01:24.666 | WARNING  | hooks.tool_compatibility:tool_compatibility_hook:57 - [ToolCompatibility] Caught litellm tool error on session 01KFGCR4G0PTNR3NPB5GQ2V88E. Allowing agent to continue with alternative approach. Error: litellm.BadRequestError: OpenAIException - An assistant message with 'tool_calls' must be followed by tool messages responding to each 'tool_call_id'. The following tool_call_ids did not have response
  1. Build tool_call_id → index map # O(n)
  2. For each position from desired split backward:
    • Check: any tool response after here references call before here?
    • If no → valid boundary, use it
    • If yes → try previous position
  3. Return first valid position (or 0 if none)

AKA: uses a backward walk algorithm that ensures no tool responses are orphaned from their corresponding tool calls during conversation summarization. This prevents OpenAI API errors while maintaining backward compatibility (defaults to True, can be disabled with preserve_tool_pairs=False).

Changed:

  • adds new function to create the index mapping in summarize code

Generated Summary:

  • Introduced a new function _find_tool_aware_boundary to enhance message summarization by preserving tool call/response pairs.
  • Modified the summarize_when_long function to include a preserve_tool_pairs parameter, defaulting to True, which governs the use of the new boundary finding logic.
  • Updated the summarization boundary finding logic to decide between a tool-aware or legacy approach based on the preserve_tool_pairs flag.
  • Created a new test file test_preserve_tool_pairs.py containing comprehensive tests for the preserve_tool_pairs functionality:
    • Tests include various scenarios such as preserving tool pairs, handling messages without tools, managing multiple tool pairs, and cases where no valid boundary exists.
  • Potential impact includes improved API compliance for strict models by preventing orphaned tool messages during summarization.

This summary was generated with ❤️ by rigging

@dreadnode-renovate-bot dreadnode-renovate-bot bot added the area/tests Changes to test files and testing infrastructure label Jan 21, 2026
@GangGreenTemperTatum
Copy link
Contributor Author

also just replicated this with non-OAI models (claude)

image

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

area/tests Changes to test files and testing infrastructure

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants