Skip to content

Self-evolving Context Database for AI Agents. Unify Agent Memory, Knowledge RAG and Skills.

Fix/moonshot invalid request (#1028)

Linsiyuan9529eb665815c
diff --git a/bot/vikingbot/agent/context.py b/bot/vikingbot/agent/context.py
index e8b220f9f..54a5b47ae 100644
--- a/bot/vikingbot/agent/context.py
+++ b/bot/vikingbot/agent/context.py
@@ -14,6 +14,7 @@ from vikingbot.agent.memory import MemoryStore
from vikingbot.agent.skills import SkillsLoader
from vikingbot.config.schema import SessionKey
from vikingbot.sandbox import SandboxManager
+from vikingbot.utils.helpers import ensure_non_empty_assistant_content
class ContextBuilder:
@@ -341,9 +342,8 @@ Always be helpful, accurate, and concise. When using tools, think step by step:
"""
msg: dict[str, Any] = {"role": "assistant"}
- # Always include content — some providers (e.g. Moonshot) reject
- # assistant messages without it, even when tool_calls are present.
- msg["content"] = content or ""
+ # Moonshot rejects empty/whitespace assistant content (incl. tool-only turns).
+ msg["content"] = ensure_non_empty_assistant_content(content)
if tool_calls:
msg["tool_calls"] = tool_calls
diff --git a/bot/vikingbot/agent/loop.py b/bot/vikingbot/agent/loop.py
index 73c835cd4..da980552e 100644
--- a/bot/vikingbot/agent/loop.py
+++ b/bot/vikingbot/agent/loop.py
@@ -357,7 +357,9 @@ class AgentLoop:
final_content = response.content
break
- if final_content is None:
+ if final_content is None or (
+ isinstance(final_content, str) and not final_content.strip()
+ ):
if iteration >= self.max_iterations:
final_content = f"Reached {self.max_iterations} iterations without completion."
else:
@@ -576,7 +578,9 @@ class AgentLoop:
publish_events=False,
)
- if final_content is None:
+ if final_content is None or (
+ isinstance(final_content, str) and not final_content.strip()
+ ):
final_content = "Background task completed."
# Save to session (mark as system message in history)
diff --git a/bot/vikingbot/agent/subagent.py b/bot/vikingbot/agent/subagent.py
index b397d3a13..81818129d 100644
--- a/bot/vikingbot/agent/subagent.py
+++ b/bot/vikingbot/agent/subagent.py
@@ -14,6 +14,7 @@ from vikingbot.bus.queue import MessageBus
from vikingbot.config.schema import SessionKey
from vikingbot.providers.base import LLMProvider
from vikingbot.sandbox.manager import SandboxManager
+from vikingbot.utils.helpers import ensure_non_empty_assistant_content
class SubagentManager:
@@ -128,7 +129,7 @@ class SubagentManager:
messages.append(
{
"role": "assistant",
- "content": response.content or "",
+ "content": ensure_non_empty_assistant_content(response.content),
"tool_calls": tool_call_dicts,
}
)
diff --git a/bot/vikingbot/session/manager.py b/bot/vikingbot/session/manager.py
index 971f39f42..27c28f413 100644
--- a/bot/vikingbot/session/manager.py
+++ b/bot/vikingbot/session/manager.py
@@ -11,7 +11,7 @@ from loguru import logger
from vikingbot.config.schema import SessionKey
from vikingbot.sandbox.manager import SandboxManager
-from vikingbot.utils.helpers import ensure_dir
+from vikingbot.utils.helpers import ensure_dir, ensure_non_empty_assistant_content
@dataclass
@@ -56,7 +56,14 @@ class Session:
)
# Convert to LLM format (just role and content)
- return [{"role": m["role"], "content": m["content"]} for m in recent]
+ out: list[dict[str, Any]] = []
+ for m in recent:
+ role = m["role"]
+ content: Any = m.get("content", "")
+ if role == "assistant" and isinstance(content, str):
+ content = ensure_non_empty_assistant_content(content)
+ out.append({"role": role, "content": content})
+ return out
def clear(self) -> None:
"""Clear all messages in the session."""
diff --git a/bot/vikingbot/utils/helpers.py b/bot/vikingbot/utils/helpers.py
index 081ac0cf6..17fb7681c 100644
--- a/bot/vikingbot/utils/helpers.py
+++ b/bot/vikingbot/utils/helpers.py
@@ -49,6 +49,22 @@ def get_history_path() -> Path:
return ensure_dir(get_bot_data_path() / "history")
+# Moonshot and similar APIs reject assistant turns with empty or whitespace-only content
+# (common on tool-only model replies where `content` is null or "").
+_ASSISTANT_NON_EMPTY_PLACEHOLDER = " "
+
+
+def ensure_non_empty_assistant_content(content: str | None) -> str:
+ """
+ Normalize text for an OpenAI-style `assistant` message `content` field.
+
+ Some providers return 400 if content is missing, null, or empty after stripping.
+ """
+ if content is None or (isinstance(content, str) and not content.strip()):
+ return _ASSISTANT_NON_EMPTY_PLACEHOLDER
+ return content
+
+
def get_bridge_path() -> Path:
"""Get the bridge directory."""
return ensure_dir(get_bot_data_path() / "bridge")