rohitg00/ai-engineering-from-scratch · error · ValueError
user_text must not be empty
Error message
user_text must not be empty
What it means
Input guard at the top of MessageLifecycle.run: the user turn is blank (empty or whitespace-only), which would send a content-free message to the API. The loop refuses to start rather than burn a request on an empty prompt.
Solutions
- Pass a non-empty user message
- Trim and check input at the application boundary before invoking the lifecycle
- Return an application-level validation error to the caller instead of letting a raw exception escape
Defensive patterns
Strategy: validation
When it happens
Trigger: Thrown at certifications/claude/lessons/08-messages-api-and-application-lifecycle/code/main.py:88 when the library encounters an invalid state.
Common situations: See trigger scenarios.
AI-assisted analysis of rohitg00/ai-engineering-from-scratch@39ea8a1c6d (2026-08-26).
Data as JSON: /api/errors/0710a1c36500a0ba.
Report an issue: GitHub.
Appendix: source
Thrown at certifications/claude/lessons/08-messages-api-and-application-lifecycle/code/main.py:88
class MessageLifecycle:
"""Own conversation state and advance it until the model ends the turn."""
def __init__(
self,
transport: ScriptedTransport,
tools: dict[str, Callable[[dict[str, Any]], Any]] | None = None,
max_turns: int = 8,
) -> None:
if max_turns < 1:
raise ValueError("max_turns must be positive")
self.transport = transport
self.tools = tools or {}
self.max_turns = max_turns
def run(self, user_text: str) -> RunResult:
if not user_text.strip():
raise ValueError("user_text must not be empty")
messages: list[dict[str, Any]] = [
{"role": "user", "content": [{"type": "text", "text": user_text}]}
]
for turn in range(1, self.max_turns + 1):
response = self.transport.create(messages)
blocks = _validated_blocks(response)
stop_reason = response.get("stop_reason")
# The assistant tool_use block must be retained before tool_result.
messages.append({"role": "assistant", "content": copy.deepcopy(blocks)})
if stop_reason == "end_turn":
return RunResult(_text_from_blocks(blocks), messages, turn)
if stop_reason != "tool_use":
raise ProtocolError(f"unsupported stop_reason: {stop_reason!r}")
tool_results = [self._execute_tool(block) for block in blocks if block["type"] == "tool_use"]View on GitHub (pinned to 39ea8a1c6d)