langchain-ai/langgraph · error · KeyError

{state_field}

Error message

{state_field}

What it means

Suffix appended to the 'Invalid input to ToolNode' error listing the state field names the tool's InjectedState annotations require. It tells you exactly which fields (beyond the messages key) the graph state dict must contain for argument injection to succeed.

Solutions

  1. Supply a state dict containing the listed fields when invoking ToolNode
  2. Make sure the tool's InjectedState annotations reference keys that actually exist in your StateGraph schema
  3. Mark optional fields with InjectedState(..., optional=True) so missing keys don't fail injection
Defensive patterns

Strategy: validation

When it happens

Trigger: Thrown at libs/prebuilt/langgraph/prebuilt/tool_node.py:1397 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/4873124edfea3c6c. Report an issue: GitHub.

Appendix: source

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

                        f"Invalid input to ToolNode. Tool {tool_call['name']} requires "
                        f"graph state dict as input."
                    )
                    if any(state_field for state_field in injected.state.values()):
                        required_fields_str = ", ".join(f for f in required_fields if f)
                        err_msg += (
                            f" State should contain fields {required_fields_str}."
                        )
                    raise ValueError(err_msg)

            # Extract state values
            if isinstance(state, dict):
                for tool_arg, state_field in injected.state.items():
                    if not state_field:
                        injected_args[tool_arg] = state
                    elif state_field in state:
                        injected_args[tool_arg] = state[state_field]
                    elif tool_arg not in injected._optional_state_args:
                        raise KeyError(state_field)
            else:
                for tool_arg, state_field in injected.state.items():
                    if not state_field:
                        injected_args[tool_arg] = state
                    elif hasattr(state, state_field):
                        injected_args[tool_arg] = getattr(state, state_field)
                    elif tool_arg not in injected._optional_state_args:
                        raise AttributeError(state_field)

        # Inject store
        if injected.store:
            if tool_runtime.store is None:
                msg = (
                    "Cannot inject store into tools with InjectedStore annotations - "
                    "please compile your graph with a store."
                )
                raise ValueError(msg)
            injected_args[injected.store] = tool_runtime.store

View on GitHub (pinned to 38031739e5)