Unity-Technologies/ml-agents · error · IndexError

agent_id {} is did not request a decision at the previous st

Error message

agent_id {} is did not request a decision at the previous step

What it means

IndexError raised by UnityEnvironment.set_action_for_agent when the requested agent_id is not found in the last recorded DecisionSteps for that behavior (self._env_state[behavior_name][0]). np.where over the agent_id array produced no match, so the agent did not request a decision in the previous step, and per-agent action setting is impossible.

Source

Thrown at ml-agents-envs/mlagents_envs/environment.py:392

        self._env_actions[behavior_name] = action

    def set_action_for_agent(
        self, behavior_name: BehaviorName, agent_id: AgentId, action: ActionTuple
    ) -> None:
        self._assert_behavior_exists(behavior_name)
        if behavior_name not in self._env_state:
            return
        action_spec = self._env_specs[behavior_name].action_spec
        action = action_spec._validate_action(action, 1, behavior_name)
        if behavior_name not in self._env_actions:
            num_agents = len(self._env_state[behavior_name][0])
            self._env_actions[behavior_name] = action_spec.empty_action(num_agents)
        try:
            index = np.where(self._env_state[behavior_name][0].agent_id == agent_id)[0][
                0
            ]
        except IndexError as ie:
            raise IndexError(
                "agent_id {} is did not request a decision at the previous step".format(
                    agent_id
                )
            ) from ie
        if action_spec.continuous_size > 0:
            self._env_actions[behavior_name].continuous[index] = action.continuous[0, :]
        if action_spec.discrete_size > 0:
            self._env_actions[behavior_name].discrete[index] = action.discrete[0, :]

    def get_steps(
        self, behavior_name: BehaviorName
    ) -> Tuple[DecisionSteps, TerminalSteps]:
        self._assert_behavior_exists(behavior_name)
        return self._env_state[behavior_name]

    def _poll_process(self) -> None:
        """
        Check the status of the subprocess. If it has exited, raise a UnityEnvironmentException

View on GitHub (pinned to 3ecb446f75)

Solutions

  1. Read the current DecisionSteps each step and only set actions for ids present in decision_steps.agent_id.
  2. Use set_actions(behavior_name, action) for all agents at once instead of per-agent calls.
  3. Verify agent_id belongs to the given behavior_name, not another group.
  4. Re-check after env.reset(); agent ids reset between episodes.

Example fix

// before
env.set_action_for_agent('Walker', 7, action)  # agent 7 didn't request a decision -> IndexError

// after
decision_steps, _ = env.get_steps('Walker')
if 7 in decision_steps.agent_id:
    env.set_action_for_agent('Walker', 7, action)
Defensive patterns

Strategy: validation

Validate before calling

decision_steps, terminal_steps = env.get_steps(behavior_name)
if agent_id not in decision_steps.agent_id:
    print(f'Skipping agent {agent_id}: did not request a decision this step')
else:
    env.set_action_for_agent(behavior_name, agent_id, action)

Try / catch

try:
    env.set_action_for_agent(behavior_name, agent_id, action)
except IndexError as e:
    if 'did not request a decision' in str(e):
        decision_steps, _ = env.get_steps(behavior_name)
        print(f'agent_id {agent_id} not in {list(decision_steps.agent_id)}')
    else:
        raise

Prevention

When it happens

Trigger: env.set_action_for_agent(behavior_name, agent_id, action) with an agent_id that was absent from the previous env.get_steps(behavior_name) DecisionSteps tuple, or an agent that used ActionBuffers instead, or a stale id from an earlier step after the agent was removed/terminated.

Common situations: Caching agent_ids across steps while agents come and go (agents only appear in DecisionSteps when they request decisions); using ids from terminated/Episode-Ended agents; off-by-one or wrong behavior_name lookups.

Related errors


AI-assisted analysis of Unity-Technologies/ml-agents@3ecb446f75 (2026-09-02). Data as JSON: /api/errors/00e9dfcbe5066e78. Report an issue: GitHub.