rohitg00/ai-engineering-from-scratch · error · ValueError

max_turns must be positive

Error message

max_turns must be positive

What it means

Error "max_turns must be positive" thrown in rohitg00/ai-engineering-from-scratch.

Source

Thrown at certifications/claude/lessons/08-messages-api-and-application-lifecycle/code/main.py:81

    def create(self, messages: list[dict[str, Any]]) -> dict[str, Any]:
        self.requests.append(copy.deepcopy(messages))
        if not self._responses:
            raise ProtocolError("transport has no scripted response left")
        return self._responses.pop(0)


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)})

View on GitHub (pinned to 39ea8a1c6d)

When it happens

Trigger: Thrown at certifications/claude/lessons/08-messages-api-and-application-lifecycle/code/main.py:81 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/d7ce6ccf8ba30d26. Report an issue: GitHub.