{"record":{"id":"90b7630ad7514cbe","repo":"jax-ml/jax","slug":"rule-must-be-a-str-but-got-type-rule","errorCode":null,"errorMessage":"rule must be a str, but got {type(rule)}","messagePattern":"rule must be a str, but got (.+?)","errorType":"validation","errorClass":"TypeError","httpStatus":null,"severity":"error","filePath":"jax/_src/custom_partitioning_sharding_rule.py","lineNumber":336,"sourceCode":"                             **factor_sizes: int) -> SdyShardingRule:\n  \"\"\"Constructs a SdyShardingRule object from the Einsum notation like string.\n\n  This is done by verifying that the input Einsum notation like string and\n  with optional special factors and factor sizes represents a valid sharding\n  rule and converting it to an internal representation.\n\n  Args:\n    rule: The Einsum notation like string for an operation.\n    reduction_factors: A tuple of factors that are reduction factors.\n    need_replication_factors: A tuple of factors that are need_replication factors.\n    permutation_factors: A tuple of factors that are permutation factors.\n    **factor_sizes: The optional factor sizes.\n\n  Raises:\n    ValueError: If there is any problem with the rule or factor_sizes.\n  \"\"\"\n  if not isinstance(rule, str):\n    raise TypeError(f\"rule must be a str, but got {type(rule)}\")\n  if not all(isinstance(size, int) for size in factor_sizes.values()):\n    raise TypeError(\n        f\"factor_sizes must be a dict of str to int, but got {factor_sizes}\")\n\n  # Replace ... with a single char to simplify parsing.\n  if BATCHING in rule:\n    raise ValueError(f\"Unknown character '{BATCHING}'\")\n  if \".\" in rule:\n    rule = rule.replace(\"...\", BATCHING)\n    if \".\" in rule:\n      raise ValueError(\"Character '.' must be used inside ellipsis '...'\")\n\n  try:\n    operands, results = rule.split(\"->\")\n  except ValueError as e:\n    raise ValueError(f\"There is no -> in rule: '{rule}'\") from e\n\n  operand_mappings = _parse_values(operands)","sourceCodeStart":318,"sourceCodeEnd":354,"githubUrl":"https://github.com/jax-ml/jax/blob/1e1c6a8fc06dfcd1247076ec5cae4640cea5d7bb/jax/_src/custom_partitioning_sharding_rule.py#L318-L354","documentation":"str_to_sdy_sharding_rule (and therefore def_partition) requires the rule to be a Python str describing operands->results. Any other type (bytes, list, SdyShardingRule object) raises TypeError immediately.","triggerScenarios":"Passing rule=b'i,j->j', rule=['i','j'], or an already-parsed SdyShardingRule to def_partition.","commonSituations":"Loading rules from config/protobuf and forgetting to decode; refactoring from parsed objects back to strings.","solutions":["Convert the value to str before calling, e.g. rule.decode() for bytes or '->'.join(parts) for structured forms","If you already hold an SdyShardingRule, pass it where the rule object is accepted rather than the str API"],"exampleFix":"# before\nrule = b'i,j->j'\n# after\nrule = 'i,j->j'","handlingStrategy":"type-guard","validationCode":"assert isinstance(rule, str), f'rule must be str, got {type(rule)}'","typeGuard":"def is_rule_str(rule) -> bool:\n    return isinstance(rule, str)","tryCatchPattern":null,"preventionTips":["Decode bytes rules at load boundaries","Keep rule strings in typed config fields"],"tags":["jax","sharding","type-error","rule-syntax"],"backgroundTag":"wrong-argument-type","analyzedSha":"1e1c6a8fc06dfcd1247076ec5cae4640cea5d7bb","analyzedAt":"2026-08-27T09:53:25.647Z","schemaVersion":2},"datasetVersion":"2026-08-27T13:17:12.746Z"}