{"record":{"id":"6fb5486227033e52","repo":"python/cpython","slug":"the-second-value-in-the-tuple-must-be-composed-of","errorCode":null,"errorMessage":"The second value in the tuple must be composed of integers in the range 0 through 9.","messagePattern":"The second value in the tuple must be composed of integers in the range 0 through 9\\.","errorType":"exception","errorClass":"ValueError","httpStatus":null,"severity":"error","filePath":"Lib/_pydecimal.py","lineNumber":579,"sourceCode":"                raise ValueError(\"Invalid sign.  The first value in the tuple \"\n                                 \"should be an integer; either 0 for a \"\n                                 \"positive number or 1 for a negative number.\")\n            self._sign = value[0]\n            if value[2] == 'F':\n                # infinity: value[1] is ignored\n                self._int = '0'\n                self._exp = value[2]\n                self._is_special = True\n            else:\n                # process and validate the digits in value[1]\n                digits = []\n                for digit in value[1]:\n                    if isinstance(digit, int) and 0 <= digit <= 9:\n                        # skip leading zeros\n                        if digits or digit != 0:\n                            digits.append(digit)\n                    else:\n                        raise ValueError(\"The second value in the tuple must \"\n                                         \"be composed of integers in the range \"\n                                         \"0 through 9.\")\n                if value[2] in ('n', 'N'):\n                    # NaN: digits form the diagnostic\n                    self._int = ''.join(map(str, digits))\n                    self._exp = value[2]\n                    self._is_special = True\n                elif isinstance(value[2], int):\n                    # finite number: digits give the coefficient\n                    self._int = ''.join(map(str, digits or [0]))\n                    self._exp = value[2]\n                    self._is_special = False\n                else:\n                    raise ValueError(\"The third value in the tuple must \"\n                                     \"be an integer, or one of the \"\n                                     \"strings 'F', 'n', 'N'.\")\n            return self\n","sourceCodeStart":561,"sourceCodeEnd":597,"githubUrl":"https://github.com/python/cpython/blob/bc6749cc3b5ae4a5e88a6cc2d5b3bebbe354eae6/Lib/_pydecimal.py#L561-L597","documentation":"In Decimal's tuple/list constructor, the second element must be an iterable of Python ints each in 0..9 — these are the coefficient digits. Anything else (an int outside 0-9, a string digit like '5', a None) raises this ValueError. Leading zeros are skipped, but every element must still be a valid digit.","triggerScenarios":"Decimal((0, (1, 25), 0)) (25 out of range); Decimal((0, '142', 0)) (string characters, not ints); Decimal((0, (1, None), 0)); Decimal((0, (), 'F')) is fine (empty digits with 'F'), but Decimal((0, (-1,), 0)) fails.","commonSituations":"Converting a digit string to a digits tuple without mapping to int: tuple('142') gives ('1','4','2'); deserialized triples where digits were JSON-encoded as strings; passing a single int instead of an iterable of digits.","solutions":["Map string digits to ints: digits = tuple(int(c) for c in '142')","Validate each digit with isinstance(d, int) and 0 <= d <= 9 before constructing","If you have the number as text, skip the tuple entirely: Decimal('1.42')"],"exampleFix":"// before\nd = Decimal((0, tuple('142'), 0))  # strings -> ValueError\n\n// after\nd = Decimal((0, tuple(int(c) for c in '142'), 0))  # Decimal('142')","handlingStrategy":"type-guard","validationCode":"def valid_digits(digits) -> bool:\n    return all(isinstance(d, int) and 0 <= d <= 9 for d in digits)\n\n# digits = tuple(int(c) for c in digit_str) if digit_str.isascii() and digit_str.isdigit() else digits","typeGuard":"def is_digit_tuple(digits) -> bool:\n    return hasattr(digits, '__iter__') and all(\n        isinstance(d, int) and not isinstance(d, bool) and 0 <= d <= 9\n        for d in digits\n    )","tryCatchPattern":"try:\n    d = Decimal((sign, digits, exp))\nexcept ValueError as e:\n    if 'second value' in str(e):\n        digits = tuple(int(c) for c in ''.join(map(str, digits)))\n        d = Decimal((sign, digits, exp))","preventionTips":["Never pass tuple('142') — string chars are not ints; always map through int()","Validate digit tuples after JSON round-trips, where ints may become strings","Skip the tuple path entirely when you already have the number as a string"],"tags":["decimal","constructor","tuple","digits","validation","valueerror"],"backgroundTag":null,"analyzedSha":"bc6749cc3b5ae4a5e88a6cc2d5b3bebbe354eae6","analyzedAt":"2026-08-14T22:01:13.976Z","schemaVersion":2},"datasetVersion":"2026-08-15T22:17:37.221Z"}