Unity-Technologies/ml-agents · error · RuntimeError

The {source} provided had Infinite values.

Error message

The {source} provided had Infinite values.

What it means

RuntimeError raised by _raise_on_nan_and_inf when the mean of the data received from Unity is infinite, indicating the environment produced values outside float32 range (e.g. ±inf from an unstable simulation or overflow). It fails fast to protect training from diverging gradients.

Source

Thrown at ml-agents-envs/mlagents_envs/rpc_utils.py:288

def _raise_on_nan_and_inf(data: np.array, source: str) -> np.array:
    # Check for NaNs or Infinite values in the observation or reward data.
    # If there's a NaN in the observations, the np.mean() result will be NaN
    # If there's an Infinite value (either sign) then the result will be Inf
    # See https://stackoverflow.com/questions/6736590/fast-check-for-nan-in-numpy for background
    # Note that a very large values (larger than sqrt(float_max)) will result in an Inf value here
    # Raise a Runtime error in the case that NaNs or Infinite values make it into the data.
    if data.size == 0:
        return data

    d = np.mean(data)
    has_nan = np.isnan(d)
    has_inf = not np.isfinite(d)

    if has_nan:
        raise RuntimeError(f"The {source} provided had NaN values.")
    if has_inf:
        raise RuntimeError(f"The {source} provided had Infinite values.")


@timed
def _process_rank_one_or_two_observation(
    obs_index: int,
    observation_spec: ObservationSpec,
    agent_info_list: Collection[AgentInfoProto],
) -> np.ndarray:
    if len(agent_info_list) == 0:
        return np.zeros((0,) + observation_spec.shape, dtype=np.float32)
    try:
        np_obs = np.array(
            [
                agent_obs.observations[obs_index].float_data.data
                for agent_obs in agent_info_list
            ],
            dtype=np.float32,
        ).reshape((len(agent_info_list),) + observation_spec.shape)

View on GitHub (pinned to 3ecb446f75)

Solutions

  1. Fix Unity-side physics instability (caps on velocities/forces, smaller fixed timestep, continuous collision detection) to stop inf values at the source.
  2. Clamp or normalize rewards and observations in Unity before sending.
  3. Check the trainer's normalizer settings and reward scale so intermediate values stay in float range.
  4. Restart the environment (env.reset() or relaunch) if it's a one-off divergence and monitor when it recurs.

Example fix

// before
# Unity: reward = huge_unbounded_value  -> inf on the wire
// after
# Unity C#: reward = Mathf.Clamp(rawReward, -100f, 100f);
Defensive patterns

Strategy: try-catch

Validate before calling

import numpy as np

def obs_finite(arr: np.ndarray) -> bool:
    return np.isfinite(arr).all()

Type guard

import numpy as np

def is_finite(arr: np.ndarray) -> bool:
    return bool(np.isfinite(arr).all())

Try / catch

from mlagents_envs.exception import UnityException
import numpy as np

try:
    env.step()
except RuntimeError as e:
    if "had Infinite values" in str(e):
        decision_steps, terminal_steps = env.reset()  # restart episode

Prevention

When it happens

Trigger: steps_from_proto or _process_rank_one_or_two_observation receiving obs/reward data with inf — physics explosions, huge reward scaling, overflow in Unity-side computations, or a bad normalization statistic.

Common situations: Rigidbodies tunneling/exploding in Unity producing inf transforms; unbounded custom reward functions; float overflow in sensors; running an old environment build against newer trainer expectations.

Related errors


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