From 0036116e0ba94b2b7a1889a570d0a345ddc538a3 Mon Sep 17 00:00:00 2001 From: Re-bin Date: Sat, 28 Feb 2026 07:35:07 +0000 Subject: [PATCH] fix: filter empty assistant messages in _save_turn instead of patching at send time --- nanobot/agent/loop.py | 2 ++ nanobot/providers/base.py | 8 -------- 2 files changed, 2 insertions(+), 8 deletions(-) diff --git a/nanobot/agent/loop.py b/nanobot/agent/loop.py index 6cd8e56..9bca0a2 100644 --- a/nanobot/agent/loop.py +++ b/nanobot/agent/loop.py @@ -451,6 +451,8 @@ class AgentLoop: for m in messages[skip:]: entry = {k: v for k, v in m.items() if k != "reasoning_content"} role, content = entry.get("role"), entry.get("content") + if role == "assistant" and not content and not entry.get("tool_calls"): + continue # skip empty assistant messages — they poison session context if role == "tool" and isinstance(content, str) and len(content) > self._TOOL_RESULT_MAX_CHARS: entry["content"] = content[:self._TOOL_RESULT_MAX_CHARS] + "\n... (truncated)" elif role == "user": diff --git a/nanobot/providers/base.py b/nanobot/providers/base.py index f52a951..eb1599a 100644 --- a/nanobot/providers/base.py +++ b/nanobot/providers/base.py @@ -51,14 +51,6 @@ class LLMProvider(ABC): for msg in messages: content = msg.get("content") - # None content on a plain assistant message (no tool_calls) crashes - # providers with "invalid message content type: ". - if content is None and msg.get("role") == "assistant" and not msg.get("tool_calls"): - clean = dict(msg) - clean["content"] = "(empty)" - result.append(clean) - continue - if isinstance(content, str) and not content: clean = dict(msg) clean["content"] = None if (msg.get("role") == "assistant" and msg.get("tool_calls")) else "(empty)"