Skip to content
This repository was archived by the owner on Aug 21, 2026. It is now read-only.

fix: trim oldest research turns on token limit during compression - #332

Closed
bigfish-emily wants to merge 1 commit into
langchain-ai:mainfrom
bigfish-emily:fix/compress-research-truncation
Closed

bigfish-emily wants to merge 1 commit into
langchain-ai:mainfrom
bigfish-emily:fix/compress-research-truncation

Conversation

@bigfish-emily

Copy link
Copy Markdown

Why

Fixes #252.

When compress_research hits a token-limit error, the retry path is meant to
free context by removing the older research history so the most recent
findings are preserved. The old remove_up_to_last_ai_message helper did the
opposite: it dropped the last AI message and everything after it, which
keeps the oldest (and largest) tool outputs, so repeated retries rarely freed
meaningful space. It also removed the compress_research_simple_human_message
instruction that was appended just before the retry loop, so subsequent
attempts ran without the compression-mode instruction.

Additionally, the token-limit detector was handed research_model even though
the failing call uses compression_model; when the two models come from
different providers, provider-specific error matching could miss the limit
error entirely.

What Changed

  • Added remove_oldest_research_turn in src/open_deep_research/utils.py,
    which removes the oldest complete research turn (the first AI message plus
    its immediately following ToolMessage results) while preserving the leading
    HumanMessage topic, the newest findings, and the trailing compression
    instruction. Message alternation stays valid so the retried model call is
    well-formed.
  • Updated compress_research to use the new helper and to pass
    configurable.compression_model to is_token_limit_exceeded.
  • Added tests/test_utils.py covering turn removal, single-AI transcripts,
    no-AI transcripts, structural validity, and repeated truncation.

Verification

The new unit tests were executed against the helper implementation; all five
pass:

PASS test_keeps_tool_results_paired_with_their_ai_message
PASS test_removes_oldest_research_turn
PASS test_removes_turn_without_tool_results
PASS test_repeated_calls_remove_turns_one_at_a_time
PASS test_returns_original_list_without_ai_messages

python -m py_compile also succeeds on all modified files.

@bigfish-emily

Copy link
Copy Markdown
Author

Closing as a duplicate: #255 (opened 2026-03-13) already fixes the token-limit truncation direction and #230 (opened 2025-12-10) already fixes the compression-model token check. This PR re-implemented both; the tested version remains in the branch if maintainers prefer it.

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

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Bug: compress_research removes recent messages instead of older ones on token limit

1 participant