langchain-ai/langgraph · error · TypeError

Tool returned unexpected type

Error message

Tool {name} returned unexpected type: {type}

What it means

Return-value normalization guard in ToolNode: the tool's raw return value is not a Command, ToolMessage, or list (e.g. it returned an arbitrary object type). The validator cannot coerce the value into anything the graph can consume.

Solutions

  1. Return a string, ToolMessage, Command, or a JSON-serializable dict from the tool
  2. Wrap custom objects yourself: return `ToolMessage(content=obj.model_dump(), ...)`
  3. Ensure async tools return awaitable results, not coroutines leaked into the validator
Defensive patterns

Strategy: type-guard

When it happens

Trigger: Thrown at libs/prebuilt/langgraph/prebuilt/tool_node.py:1453 when the library encounters an invalid state.

Common situations: See trigger scenarios.


AI-assisted analysis of langchain-ai/langgraph@38031739e5 (2026-08-26). Data as JSON: /api/errors/7fe166d0c76a27fc. Report an issue: GitHub.

Appendix: source

Thrown at libs/prebuilt/langgraph/prebuilt/tool_node.py:1453

        tool_call: ToolCall,
        input_type: Literal["list", "dict", "tool_calls"],
    ) -> ToolMessage | Command | list[Command | ToolMessage]:
        """Validate and normalize a tool's raw return value."""
        if isinstance(response, Command):
            return self._validate_tool_command(response, tool_call, input_type)
        if isinstance(response, ToolMessage):
            response.content = cast("str | list", msg_content_output(response.content))
            return response
        if isinstance(response, list):
            if all(isinstance(r, (Command, ToolMessage)) for r in response):
                return self._validate_tool_command_list(response, tool_call, input_type)
            msg = (
                f"Tool {tool_call['name']} returned a list with invalid element "
                "types: expected all Command or ToolMessage"
            )
            raise TypeError(msg)
        msg = f"Tool {tool_call['name']} returned unexpected type: {type(response)}"
        raise TypeError(msg)

    def _validate_tool_command_list(
        self,
        response: list[Command | ToolMessage],
        tool_call: ToolCall,
        input_type: Literal["list", "dict", "tool_calls"],
    ) -> list[Command | ToolMessage]:
        """Validate a list of Command/ToolMessage returned by a single tool call.

        Requires exactly one terminating ToolMessage (matching the outer tool_call_id)
        across the list — either as a top-level element or nested in a
        Command.update["messages"].
        """
        expected_id = tool_call["id"]

        terminator_count = 0
        for item in response:
            if isinstance(item, ToolMessage):

View on GitHub (pinned to 38031739e5)