{"record":{"id":"7232ade594219871","repo":"Unity-Technologies/ml-agents","slug":"the-source-provided-had-infinite-values","errorCode":null,"errorMessage":"The {source} provided had Infinite values.","messagePattern":"The (.+?) provided had Infinite values\\.","errorType":"exception","errorClass":"RuntimeError","httpStatus":null,"severity":"error","filePath":"ml-agents-envs/mlagents_envs/rpc_utils.py","lineNumber":288,"sourceCode":"\ndef _raise_on_nan_and_inf(data: np.array, source: str) -> np.array:\n    # Check for NaNs or Infinite values in the observation or reward data.\n    # If there's a NaN in the observations, the np.mean() result will be NaN\n    # If there's an Infinite value (either sign) then the result will be Inf\n    # See https://stackoverflow.com/questions/6736590/fast-check-for-nan-in-numpy for background\n    # Note that a very large values (larger than sqrt(float_max)) will result in an Inf value here\n    # Raise a Runtime error in the case that NaNs or Infinite values make it into the data.\n    if data.size == 0:\n        return data\n\n    d = np.mean(data)\n    has_nan = np.isnan(d)\n    has_inf = not np.isfinite(d)\n\n    if has_nan:\n        raise RuntimeError(f\"The {source} provided had NaN values.\")\n    if has_inf:\n        raise RuntimeError(f\"The {source} provided had Infinite values.\")\n\n\n@timed\ndef _process_rank_one_or_two_observation(\n    obs_index: int,\n    observation_spec: ObservationSpec,\n    agent_info_list: Collection[AgentInfoProto],\n) -> np.ndarray:\n    if len(agent_info_list) == 0:\n        return np.zeros((0,) + observation_spec.shape, dtype=np.float32)\n    try:\n        np_obs = np.array(\n            [\n                agent_obs.observations[obs_index].float_data.data\n                for agent_obs in agent_info_list\n            ],\n            dtype=np.float32,\n        ).reshape((len(agent_info_list),) + observation_spec.shape)","sourceCodeStart":270,"sourceCodeEnd":306,"githubUrl":"https://github.com/Unity-Technologies/ml-agents/blob/3ecb446f75d1e7400eb404c562dc005d3164cffc/ml-agents-envs/mlagents_envs/rpc_utils.py#L270-L306","documentation":"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.","triggerScenarios":"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.","commonSituations":"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.","solutions":["Fix Unity-side physics instability (caps on velocities/forces, smaller fixed timestep, continuous collision detection) to stop inf values at the source.","Clamp or normalize rewards and observations in Unity before sending.","Check the trainer's normalizer settings and reward scale so intermediate values stay in float range.","Restart the environment (env.reset() or relaunch) if it's a one-off divergence and monitor when it recurs."],"exampleFix":"// before\n# Unity: reward = huge_unbounded_value  -> inf on the wire\n// after\n# Unity C#: reward = Mathf.Clamp(rawReward, -100f, 100f);","handlingStrategy":"try-catch","validationCode":"import numpy as np\n\ndef obs_finite(arr: np.ndarray) -> bool:\n    return np.isfinite(arr).all()","typeGuard":"import numpy as np\n\ndef is_finite(arr: np.ndarray) -> bool:\n    return bool(np.isfinite(arr).all())","tryCatchPattern":"from mlagents_envs.exception import UnityException\nimport numpy as np\n\ntry:\n    env.step()\nexcept RuntimeError as e:\n    if \"had Infinite values\" in str(e):\n        decision_steps, terminal_steps = env.reset()  # restart episode","preventionTips":["Cap velocities/forces in Unity physics; enable continuous collision detection.","Clamp rewards Unity-side to a sane range.","Avoid unbounded custom reward/sensor computations.","Reset the episode on inf and monitor frequency to catch divergence early."],"tags":["python","unity-ml-agents","infinity","numerical"],"backgroundTag":"nan-in-observation-data","analyzedSha":"3ecb446f75d1e7400eb404c562dc005d3164cffc","analyzedAt":"2026-09-02T16:33:12.832Z","contentChangedAt":null,"schemaVersion":2},"datasetVersion":"2026-09-09T21:17:11.164Z"}