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

stop_reason tool_use had no tool_use block

Error message

stop_reason tool_use had no tool_use block

What it means

Error "stop_reason tool_use had no tool_use block" thrown in rohitg00/ai-engineering-from-scratch.

Source

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

            {"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)
        if handler is None:
            return {
                "type": "tool_result",
                "tool_use_id": tool_id,

View on GitHub (pinned to 39ea8a1c6d)

When it happens

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