Skip to content

fix: strip orphaned tool_result messages after conversation truncation#416

Merged
yaojin3616 merged 1 commit intodataelement:mainfrom
suxinshuo:hotifx/tool_result_400
Apr 16, 2026
Merged

fix: strip orphaned tool_result messages after conversation truncation#416
yaojin3616 merged 1 commit intodataelement:mainfrom
suxinshuo:hotifx/tool_result_400

Conversation

@suxinshuo
Copy link
Copy Markdown
Contributor

@suxinshuo suxinshuo commented Apr 15, 2026

Summary

  • Fix HTTP 400 errors caused by orphaned tool_result blocks in the conversation history sent to the Anthropic API.
  • After truncating the conversation to ctx_size, strip any leading role="tool" messages whose corresponding assistant (tool_use) message was cut off by the truncation boundary.

Closes #415

Root Cause

Each tool_call DB record expands into 2 messages (assistant + tool) during history reconstruction, so conversation[-ctx_size:] can split a pair — keeping the tool result but dropping the tool use. The Anthropic API requires every tool_result to have a matching tool_use in the preceding assistant message.

Test plan

  • Reproduce with a session containing ~50 tool-call rounds and context_window_size = 100
  • Verify the LLM call succeeds after the fix
  • Verify normal sessions without tool calls are unaffected

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

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[BUG] HTTP 400: orphaned tool_result in conversation history due to dual truncation

2 participants