From 2fc1029073e48a3241aa38a8d5c6666766ade069 Mon Sep 17 00:00:00 2001 From: suxinshuo Date: Wed, 15 Apr 2026 20:21:38 +0800 Subject: [PATCH] fix: To prevent tool call message pairs(assistant + tool) from being broken down. --- backend/app/api/websocket.py | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/backend/app/api/websocket.py b/backend/app/api/websocket.py index 11c98843d..a6698c5f0 100644 --- a/backend/app/api/websocket.py +++ b/backend/app/api/websocket.py @@ -515,10 +515,15 @@ async def _call_with_failover(): async def _on_failover(reason: str): await websocket.send_json({"type": "info", "content": f"Primary model error, {reason}"}) + # To prevent tool call message pairs(assistant + tool) from being broken down. + _truncated = conversation[-ctx_size:] + while _truncated and _truncated[0].get("role") == "tool": + _truncated.pop(0) + return await call_llm_with_failover( primary_model=llm_model, fallback_model=fallback_llm_model, - messages=conversation[-ctx_size:], + messages=_truncated, agent_name=agent_name, role_description=role_description, agent_id=agent_id,