Skip to content

CubeLoop — a Pythonic, async-native agent harness core designed for high performance, readability, and production-grade persistence.

fix(anthropic): synthesize placeholder for empty assistant content

xf gongac875fdeb992
diff --git a/cubepi/providers/anthropic.py b/cubepi/providers/anthropic.py
index 04e454c..2ac1074 100644
--- a/cubepi/providers/anthropic.py
+++ b/cubepi/providers/anthropic.py
@@ -483,6 +483,16 @@ class AnthropicProvider(BaseProvider):
"input": assistant_block.arguments,
}
)
+ if not assistant_content:
+ # Anthropic rejects messages with no content blocks ("all
+ # messages must have non-empty content"). This happens when a
+ # prior run failed before the model emitted anything and the
+ # empty AssistantMessage was persisted (stop_reason="error").
+ # Synthesize a single text block so replaying the history
+ # doesn't break the next request. (A trailing empty error
+ # assistant is dropped earlier in `_build_api_messages` — the
+ # placeholder only ever appears in non-trailing positions.)
+ assistant_content.append({"type": "text", "text": "[empty response]"})
return {"role": "assistant", "content": assistant_content}
elif isinstance(msg, ToolResultMessage):
diff --git a/tests/providers/test_anthropic.py b/tests/providers/test_anthropic.py
index 16aea57..a8e2c06 100644
--- a/tests/providers/test_anthropic.py
+++ b/tests/providers/test_anthropic.py
@@ -56,6 +56,19 @@ class TestAnthropicMessageConversion:
assert result["content"][0]["type"] == "tool_result"
assert result["content"][0]["tool_use_id"] == "tc-1"
+ def test_convert_assistant_with_empty_content_uses_placeholder(self):
+ # A previously-failed turn is persisted as AssistantMessage(content=[],
+ # stop_reason="error"). Anthropic rejects messages with no content
+ # blocks ("messages.N: all messages must have non-empty content"), so
+ # replaying such a history must synthesize at least one block.
+ msg = AssistantMessage(content=[], stop_reason="error", error_message="boom")
+ result = AnthropicProvider._convert_message(msg)
+ assert result["role"] == "assistant"
+ assert len(result["content"]) >= 1
+ assert all(
+ block.get("text") or block.get("input") for block in result["content"]
+ )
+
class TestAnthropicToolConversion:
def test_convert_tool_definition(self):