{"record":{"id":"9585e70c4bdd5c1e","repo":"Unity-Technologies/ml-agents","slug":"agent-id-agent-id-is-not-present-in-the-decision","errorCode":null,"errorMessage":"agent_id {agent_id} is not present in the DecisionSteps","messagePattern":"agent_id (.+?) is not present in the DecisionSteps","errorType":"exception","errorClass":"KeyError","httpStatus":null,"severity":"error","filePath":"ml-agents-envs/mlagents_envs/base_env.py","lineNumber":118,"sourceCode":"        this DecisionSteps.\n        \"\"\"\n        if self._agent_id_to_index is None:\n            self._agent_id_to_index = {}\n            for a_idx, a_id in enumerate(self.agent_id):\n                self._agent_id_to_index[a_id] = a_idx\n        return self._agent_id_to_index\n\n    def __len__(self) -> int:\n        return len(self.agent_id)\n\n    def __getitem__(self, agent_id: AgentId) -> DecisionStep:\n        \"\"\"\n        returns the DecisionStep for a specific agent.\n        :param agent_id: The id of the agent\n        :returns: The DecisionStep\n        \"\"\"\n        if agent_id not in self.agent_id_to_index:\n            raise KeyError(f\"agent_id {agent_id} is not present in the DecisionSteps\")\n        agent_index = self._agent_id_to_index[agent_id]  # type: ignore\n        agent_obs = []\n        for batched_obs in self.obs:\n            agent_obs.append(batched_obs[agent_index])\n        agent_mask = None\n        if self.action_mask is not None:\n            agent_mask = []\n            for mask in self.action_mask:\n                agent_mask.append(mask[agent_index])\n        group_id = self.group_id[agent_index]\n        return DecisionStep(\n            obs=agent_obs,\n            reward=self.reward[agent_index],\n            agent_id=agent_id,\n            action_mask=agent_mask,\n            group_id=group_id,\n            group_reward=self.group_reward[agent_index],\n        )","sourceCodeStart":100,"sourceCodeEnd":136,"githubUrl":"https://github.com/Unity-Technologies/ml-agents/blob/3ecb446f75d1e7400eb404c562dc005d3164cffc/ml-agents-envs/mlagents_envs/base_env.py#L100-L136","documentation":"DecisionSteps.__getitem__ raises KeyError when the requested agent_id is not among the agents that produced a decision this step. agent_id_to_index only contains ids present in the current batch, so any id not stepping/deciding now is invalid. This is a plain Python KeyError (the message is the formatted string).","triggerScenarios":"Indexing d = DecisionSteps() or the DecisionSteps from env.get_steps() with an agent id that is done/absent this step: d[agent_id] where agent_id not in d.agent_id_to_index — commonly an agent that terminated (moved to TerminalSteps) or hasn't been spawned yet.","commonSituations":"Looping over a cached list of all agent ids while some have finished their episode (they're in TerminalSteps, not DecisionSteps); querying before the first env step; agent ids reset between episodes with new ids for new spawns.","solutions":["Check membership first: only index d[agent_id] if agent_id in d.agent_id_to_index.","Iterate over d.agent_id instead of a stale external id list so you only touch present agents.","Handle agents found in TerminalSteps separately — get them from the TerminalSteps object returned by env.get_steps()."],"exampleFix":"# before\nstep = decision_steps[agent_id]  # KeyError when agent is done\n# after\nif agent_id in decision_steps:\n    step = decision_steps[agent_id]\nelse:\n    step = terminal_steps[agent_id]  # agent finished this step","handlingStrategy":"type-guard","validationCode":"if agent_id not in decision_steps.agent_id_to_index:\n    # agent is done or absent this step — handle via TerminalSteps","typeGuard":"def in_decision(ds, agent_id: int) -> bool:\n    return agent_id in ds.agent_id_to_index","tryCatchPattern":"try:\n    step = decision_steps[agent_id]\nexcept KeyError:\n    step = None  # agent not deciding this step","preventionTips":["Check `agent_id in decision_steps` before indexing","Iterate decision_steps.agent_id, not stale id lists","Route finished agents to TerminalSteps handling"],"tags":["python","ml-agents","keyerror","decision-steps","agent-lifecycle"],"backgroundTag":"key-not-found","analyzedSha":"3ecb446f75d1e7400eb404c562dc005d3164cffc","analyzedAt":"2026-09-02T16:33:12.832Z","contentChangedAt":null,"schemaVersion":2},"datasetVersion":"2026-09-09T21:17:11.164Z"}