{"record":{"id":"796b920578433775","repo":"cocoindex-io/cocoindex","slug":"expected-tuple-of-length-expected-len-loc-got","errorCode":null,"errorMessage":"expected tuple of length {expected_len}{loc}, got length {len(value)}","messagePattern":"expected tuple of length (.+?)(.+?), got length (.+?)","errorType":"exception","errorClass":"TypeError","httpStatus":null,"severity":"error","filePath":"python/cocoindex/_internal/datatype.py","lineNumber":364,"sourceCode":"                    loc = f\" at {path}\" if path else \"\"\n                    raise TypeError(f\"expected tuple{loc}, got {type(value).__name__}\")\n                for i, elem in enumerate(value):\n                    elem_fn(elem, f\"{path}[{i}]\")\n\n            return check_var_tuple\n\n        if args:\n            # Fixed-length: tuple[X, Y, ...]\n            elem_fns = [_build_check_fn(a) for a in args]\n            expected_len = len(args)\n\n            def check_fixed_tuple(value: Any, path: str) -> None:\n                if not isinstance(value, tuple):\n                    loc = f\" at {path}\" if path else \"\"\n                    raise TypeError(f\"expected {tp}{loc}, got {type(value).__name__}\")\n                if len(value) != expected_len:\n                    loc = f\" at {path}\" if path else \"\"\n                    raise TypeError(\n                        f\"expected tuple of length {expected_len}{loc}, \"\n                        f\"got length {len(value)}\"\n                    )\n                for i, (elem, fn) in enumerate(zip(value, elem_fns)):\n                    fn(elem, f\"{path}[{i}]\")\n\n            return check_fixed_tuple\n\n        # Bare tuple (no args) — just check isinstance\n        def check_bare_tuple(value: Any, path: str) -> None:\n            if not isinstance(value, tuple):\n                loc = f\" at {path}\" if path else \"\"\n                raise TypeError(f\"expected tuple{loc}, got {type(value).__name__}\")\n\n        return check_bare_tuple\n\n    # Simple concrete type: str, int, bytes, uuid.UUID, etc.\n    if isinstance(tp, type):","sourceCodeStart":346,"sourceCodeEnd":382,"githubUrl":"https://github.com/cocoindex-io/cocoindex/blob/e84aa99b3292c5270a4b313b2a7137ad9ce8ab3b/python/cocoindex/_internal/datatype.py#L346-L382","documentation":"For fixed-length tuple types, after confirming the value is a tuple, check_fixed_tuple verifies the arity matches the number of type arguments. This TypeError is raised when a tuple of the wrong length is supplied, e.g. a 3-element tuple for tuple[int, str]. The length must match exactly because each position has its own element checker.","triggerScenarios":"Passing a tuple whose len() differs from the declared arity — e.g. (1, 'a', True) where tuple[int, str] is declared, or an empty tuple () for any non-empty fixed tuple type, via a @coco.fn argument, target-state field, or TypeChecker.check().","commonSituations":"Appending/removing a field from the declared type without updating producers; spreading optional values conditionally; building the tuple dynamically with variable item counts.","solutions":["Fix the producer so it emits exactly the declared number of elements","If arity is genuinely variable, switch the declared type to a dataclass, NamedTuple, or tuple[X, ...] as appropriate","Log/print the value before the call to see which element count is being produced"],"exampleFix":"// before\nreturn (user_id, name, email)  # declared tuple[int, str]\n// after\nreturn (user_id, name)","handlingStrategy":"validation","validationCode":"if not (isinstance(value, tuple) and len(value) == expected_len):\n    raise ValueError(f\"need tuple of length {expected_len}, got {value!r}\")","typeGuard":"def has_arity(value: object, n: int) -> TypeGuard[tuple]:\n    return isinstance(value, tuple) and len(value) == n","tryCatchPattern":"try:\n    check(value)\nexcept TypeError as e:\n    if \"expected tuple of length\" in str(e):\n        log.error(\"arity mismatch: %s\", e)\n        raise","preventionTips":["Keep producers and declared tuple types in sync — update both when fields change","Build fixed-arity values via a small factory function that asserts length once","Add a unit test that checks representative values against the declared type"],"tags":["python","type-checking","tuple","arity"],"backgroundTag":"type-mismatch","analyzedSha":"e84aa99b3292c5270a4b313b2a7137ad9ce8ab3b","analyzedAt":"2026-09-08T15:59:19.997Z","contentChangedAt":"2026-09-08T15:59:19.997Z","schemaVersion":2},"datasetVersion":"2026-09-14T05:17:10.506Z"}