{"record":{"id":"40ca824169dcc084","repo":"jax-ml/jax","slug":"string-directive-must-be-placed-at-the-beginning","errorCode":null,"errorMessage":"string directive must be placed at the beginning","messagePattern":"string directive must be placed at the beginning","errorType":"validation","errorClass":"ValueError","httpStatus":null,"severity":"error","filePath":"jax/_src/numpy/index_tricks.py","lineNumber":187,"sourceCode":"        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\n      if trans1d != -1 and ndmin - item_ndim > 0:\n        shape_obj = tuple(range(ndmin))\n        # Calculate number of left shifts, with overflow protection by mod\n        num_lshifts = ndmin - abs(ndmin + trans1d + 1) % ndmin\n        shape_obj = tuple(shape_obj[num_lshifts:] + shape_obj[:num_lshifts])\n\n        newobj = transpose(newobj, shape_obj)\n\n      output.append(newobj)\n\n    res = concatenate(tuple(output), axis=axis)\n","sourceCodeStart":169,"sourceCodeEnd":205,"githubUrl":"https://github.com/jax-ml/jax/blob/1e1c6a8fc06dfcd1247076ec5cae4640cea5d7bb/jax/_src/numpy/index_tricks.py#L169-L205","documentation":"Raised by jnp.r_ / jnp.c_ when a string appears anywhere in the indexing key other than the first position. Only the leading element of the bracket expression may be a directive string; subsequent string items are rejected.","triggerScenarios":"jnp.r_[a, '0,2'] or jnp.r_['0,2', a, 'r'] — a string placed after the first key element.","commonSituations":"Reordering bracket arguments while refactoring; copy-paste errors merging two r_ expressions; assuming directives can be attached per-operand.","solutions":["Move the directive string to the very first position inside the brackets","Remove stray string entries from the key","Build the layout with explicit concatenate/stack/expand_dims calls"],"exampleFix":"# before\nout = jnp.r_[a, b, '0,2']\n# after\nout = jnp.r_['0,2', a, b]","handlingStrategy":"validation","validationCode":"assert not any(isinstance(k, str) for k in list(key_tup)[1:]), 'directive must be first'","typeGuard":"def directive_first(key) -> bool:\n    items = key if isinstance(key, tuple) else (key,)\n    return not any(isinstance(k, str) for k in items[1:])","tryCatchPattern":null,"preventionTips":["Place directive strings only in the first bracket slot","Review r_/c_ expressions after reordering arguments","Use explicit stack/concatenate calls for complex layouts"],"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"}