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

unsupported stop_reason: {stop_reason!r}

Error message

unsupported stop_reason: {stop_reason!r}

What it means

Error "unsupported stop_reason: {stop_reason!r}" thrown in rohitg00/ai-engineering-from-scratch.

Source

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

    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"]
            if not tool_results:
                raise ProtocolError("stop_reason tool_use had no tool_use block")
            messages.append({"role": "user", "content": tool_results})

        raise ProtocolError(f"maximum turn count {self.max_turns} exceeded")

    def _execute_tool(self, block: dict[str, Any]) -> dict[str, Any]:
        tool_id = block.get("id")
        name = block.get("name")
        arguments = block.get("input")
        if not isinstance(tool_id, str) or not tool_id:
            raise ProtocolError("tool_use requires a non-empty id")
        if not isinstance(name, str) or not isinstance(arguments, dict):
            raise ProtocolError("tool_use requires name and object input")

        handler = self.tools.get(name)

View on GitHub (pinned to 39ea8a1c6d)

When it happens

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