Unity-Technologies/ml-agents · error · UnityActionException

The behavior {name} needs a continuous input of dimension {_

Error message

The behavior {name} needs a continuous input of dimension {_expected_shape} for (<number of agents>, <action size>) but received input of dimension {actions.continuous.shape}

What it means

BaseEnv._validate_action checks that ActionArgs.continuous has shape (n_agents, continuous_size) matching the BehaviorSpec declared for the behavior. A mismatch raises UnityActionException. This ensures the array you send via set_actions matches what the Unity environment expects for every deciding agent.

Source

Thrown at ml-agents-envs/mlagents_envs/base_env.py:421

                        self.discrete_branches[i],  # type: ignore
                        size=(n_agents),
                        dtype=np.int32,
                    )
                    for i in range(self.discrete_size)
                ]
            )
        return ActionTuple(continuous=_continuous, discrete=_discrete)

    def _validate_action(
        self, actions: ActionTuple, n_agents: int, name: str
    ) -> ActionTuple:
        """
        Validates that action has the correct action dim
        for the correct number of agents and ensures the type.
        """
        _expected_shape = (n_agents, self.continuous_size)
        if actions.continuous.shape != _expected_shape:
            raise UnityActionException(
                f"The behavior {name} needs a continuous input of dimension "
                f"{_expected_shape} for (<number of agents>, <action size>) but "
                f"received input of dimension {actions.continuous.shape}"
            )
        _expected_shape = (n_agents, self.discrete_size)
        if actions.discrete.shape != _expected_shape:
            raise UnityActionException(
                f"The behavior {name} needs a discrete input of dimension "
                f"{_expected_shape} for (<number of agents>, <action size>) but "
                f"received input of dimension {actions.discrete.shape}"
            )
        return actions

    @staticmethod
    def create_continuous(continuous_size: int) -> "ActionSpec":
        """
        Creates an ActionSpec that is homogenously continuous
        """

View on GitHub (pinned to 3ecb446f75)

Solutions

  1. Read the expected dims from behavior_spec: spec.action_spec.continuous_size and len(decision_steps) for n_agents, then reshape with np.reshape / np.atleast_2d.
  2. Slice policy outputs to exactly the deciding agents: use decision_steps.agent_id length for the batch dimension.
  3. Upgrade/align policy and env configs — mismatched action spaces between trainer config and Unity behavior parameters cause persistent shape errors.

Example fix

# before
env.set_actions(name, policy.decide(all_agent_obs))  # wrong row count
# after
import numpy as np
cont = policy.decide(decision_steps.obs)
cont = np.asarray(cont, dtype=np.float32).reshape(len(decision_steps), spec.action_spec.continuous_size)
env.set_actions(name, ActionArgs(continuous=cont))
Defensive patterns

Strategy: validation

Validate before calling

import numpy as np
n = len(decision_steps)
cs = behavior_spec.action_spec.continuous_size
cont = np.asarray(actions.continuous, dtype=np.float32)
assert cont.shape == (n, cs), f"need (n_agents, {cs}), got {cont.shape}"

Try / catch

try:
    env.set_actions(name, ActionArgs(continuous=cont))
except UnityActionException as e:
    print(e)  # inspect expected vs received shapes and reshape
    raise

Prevention

When it happens

Trigger: Calling env.set_actions(behavior_name, action) where actions.continuous.shape != (number of deciding agents, spec.continuous_action_size) — e.g. one row per known agent instead of only deciding agents, transposed shape, or wrong action dimension.

Common situations: Using policy output sized for all agents in the scene while some were done this step; hardcoding action size instead of reading BehaviorSpec; numpy arrays with an extra/missing dimension (shape (n,) instead of (n, k)).

Related errors


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