{"record":{"id":"67cbb912044e3f25","repo":"microsoft/qlib","slug":"subspace-of-index-i-validation-error","errorCode":null,"errorMessage":"Subspace of index {i} validation error.","messagePattern":"Subspace of index (.+?) validation error\\.","errorType":"validation","errorClass":"GymSpaceValidationError","httpStatus":null,"severity":"error","filePath":"qlib/rl/interpreter.py","lineNumber":127,"sourceCode":"            raise GymSpaceValidationError(\"Sample must be a dict with same length as space.\", space, x)\n        for k, subspace in space.spaces.items():\n            if k not in x:\n                raise GymSpaceValidationError(f\"Key {k} not found in sample.\", space, x)\n            try:\n                _gym_space_contains(subspace, x[k])\n            except GymSpaceValidationError as e:\n                raise GymSpaceValidationError(f\"Subspace of key {k} validation error.\", space, x) from e\n\n    elif isinstance(space, spaces.Tuple):\n        if isinstance(x, (list, np.ndarray)):\n            x = tuple(x)  # Promote list and ndarray to tuple for contains check\n        if not isinstance(x, tuple) or len(x) != len(space):\n            raise GymSpaceValidationError(\"Sample must be a tuple with same length as space.\", space, x)\n        for i, (subspace, part) in enumerate(zip(space, x)):\n            try:\n                _gym_space_contains(subspace, part)\n            except GymSpaceValidationError as e:\n                raise GymSpaceValidationError(f\"Subspace of index {i} validation error.\", space, x) from e\n\n    else:\n        if not space.contains(x):\n            raise GymSpaceValidationError(\"Validation error reported by gym.\", space, x)\n\n\nclass GymSpaceValidationError(Exception):\n    def __init__(self, message: str, space: gym.Space, x: Any) -> None:\n        self.message = message\n        self.space = space\n        self.x = x\n\n    def __str__(self) -> str:\n        return f\"{self.message}\\n  Space: {self.space}\\n  Sample: {self.x}\"\n","sourceCodeStart":109,"sourceCodeEnd":142,"githubUrl":"https://github.com/microsoft/qlib/blob/79633dd9506ea689e5400dea0197717b5b3d74b7/qlib/rl/interpreter.py#L109-L142","documentation":"Wrapping error from qlib's recursive gym space validator for `spaces.Tuple` (qlib/rl/interpreter.py:127). Element `i` of the sample failed its nested validation and the outer level re-raises with the failing index. The actual cause (bounds/shape/key failure) is preserved in `__cause__`.","triggerScenarios":"A Tuple space where element `i` violates its subspace: e.g. `spaces.Tuple([Box(0, 1), Box(0, 10)])` with sample `(0.5, 42.0)` — index 1 is out of bounds and this error wraps it.","commonSituations":"Multi-component action interpreters (e.g. split order into several execution volumes) where one component exceeds its declared range; heterogeneous observations assembled per-step where one field occasionally breaks bounds (NaN in Box, unnormalized price).","solutions":["Read `e.__cause__` (or full `str(e)`) to identify the leaf failure and map index `i` back to the interpreter field it corresponds to.","Clip/normalize that component before returning it from the interpreter (e.g. `np.clip(vol, lo, hi)`).","Fix the subspace definition if the declared bounds are too tight for legitimate values."],"exampleFix":"// before\nvols = self.twins_adjust * total  # can exceed per-tick bound\n// after\nvols = np.clip(self.twins_adjust * total, self._min_vol, self._max_vol)","handlingStrategy":"try-catch","validationCode":"for i, (sub, part) in enumerate(zip(space, x)):\n    assert sub.contains(part), f\"element {i}={part!r} not in subspace {sub}\"","typeGuard":"def tuple_elements_valid(space: gym.spaces.Tuple, x: tuple) -> bool:\n    return len(x) == len(space) and all(s.contains(p) for s, p in zip(space, x))","tryCatchPattern":"try:\n    _gym_space_contains(space, x)\nexcept GymSpaceValidationError as e:\n    idx = parse_index(e.message)  # from 'Subspace of index {i} ...'\n    log.error(\"component %d failed: %s\", idx, e.__cause__)\n    raise","preventionTips":["Clip each action component to its subspace bounds in the interpreter.","Keep component order stable between space definition and action construction.","Write per-component unit tests so you know which index maps to which semantic field."],"tags":["gym","rl","interpreter","space-validation","tuple","nested"],"backgroundTag":null,"analyzedSha":"79633dd9506ea689e5400dea0197717b5b3d74b7","analyzedAt":"2026-08-15T07:01:27.511Z","schemaVersion":2},"datasetVersion":"2026-08-15T17:31:12.345Z"}