{"record":{"id":"bf75e662b7b3db01","repo":"jax-ml/jax","slug":"could-not-understand-directive-directive-r","errorCode":null,"errorMessage":"could not understand directive {directive!r}","messagePattern":"could not understand directive (.+?)","errorType":"validation","errorClass":"ValueError","httpStatus":null,"severity":"error","filePath":"jax/_src/numpy/index_tricks.py","lineNumber":175,"sourceCode":"    if isinstance(directive, str):\n      key_tup = key_tup[1:]\n      # check two special cases: matrix directives\n      if directive == \"r\":\n        params[-1] = 0\n      elif directive == \"c\":\n        params[-1] = 1\n      else:\n        vec: list[Any] = directive.split(\",\")\n        k = len(vec)\n        if k < 4:\n          vec += params[k:]\n        else:\n          # ignore everything after the first three comma-separated ints\n          vec = vec[:3] + [params[-1]]\n        try:\n          params = list(map(int, vec))\n        except ValueError as err:\n          raise ValueError(\n            f\"could not understand directive {directive!r}\"\n          ) from err\n\n    axis, ndmin, trans1d, matrix = params\n\n    output = []\n    for item in key_tup:\n      if isinstance(item, slice):\n        newobj = _make_1d_grid_from_slice(item, op_name=self.op_name)\n        item_ndim = 0\n      elif isinstance(item, str):\n        raise ValueError(\"string directive must be placed at the beginning\")\n      else:\n        newobj = array(item, copy=False)\n        item_ndim = newobj.ndim\n\n      newobj = array(newobj, copy=False, ndmin=ndmin)\n","sourceCodeStart":157,"sourceCodeEnd":193,"githubUrl":"https://github.com/jax-ml/jax/blob/1e1c6a8fc06dfcd1247076ec5cae4640cea5d7bb/jax/_src/numpy/index_tricks.py#L157-L193","documentation":"Raised by jax.numpy.r_ / jnp.r_ (and c_) when the bracket directive string cannot be parsed. Directives like '0,2' set (axis, ndmin) or '2,3,1' sets (axis, ndmin, trans1d); the comma-separated parts must all be integers. If int() conversion of any part fails, this ValueError is raised.","triggerScenarios":"jnp.r_['0,2,axis'] or jnp.r_['1.5,2', ...] — non-integer tokens in the directive; also typos like '0;2'. Only 'r' and 'c' are special-cased.","commonSituations":"Copy-pasting numpy r_ examples with wrong directive syntax; forgetting the quote style; expecting slice syntax like '-1:1:5j' inside the directive string instead of as the indexed item.","solutions":["Use only comma-separated integers in the directive, e.g. jnp.r_['0,2,-1', a, b]","Use the 'r'/'c' shorthand strings for simple row/column stacking","For complex layout needs, call jnp.stack/jnp.concatenate/jnp.expand_dims explicitly"],"exampleFix":"# before\nout = jnp.r_['0,2,outer', a, b]\n# after\nout = jnp.r_['0,2', a, b]  # integers only","handlingStrategy":"validation","validationCode":"def valid_directive(d: str) -> bool:\n    if d in ('r', 'c'):\n        return True\n    parts = d.split(',')\n    return all(p.strip().lstrip('-').isdigit() for p in parts)","typeGuard":null,"tryCatchPattern":"try:\n    out = jnp.r_['0,2', a, b]\nexcept ValueError as e:\n    if 'could not understand directive' in str(e):\n        out = jnp.concatenate([jnp.expand_dims(x, 0) for x in (a, b)], axis=0)\n    else:\n        raise","preventionTips":["Keep directives to 1-3 comma-separated integers","Prefer explicit jnp.concatenate/stack for anything beyond simple stacking","Test directive strings in a helper that validates before use"],"tags":["jax","r-c-index-tricks","directive","numpy-compat"],"backgroundTag":"invalid-directive-syntax","analyzedSha":"1e1c6a8fc06dfcd1247076ec5cae4640cea5d7bb","analyzedAt":"2026-08-27T09:53:25.647Z","schemaVersion":2},"datasetVersion":"2026-08-27T13:17:12.746Z"}