python/cpython · error · ValueError
The second value in the tuple must be composed of integers i
Error message
The second value in the tuple must be composed of integers in the range 0 through 9.
What it means
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.
Source
Thrown at Lib/_pydecimal.py:579
raise ValueError("Invalid sign. The first value in the tuple "
"should be an integer; either 0 for a "
"positive number or 1 for a negative number.")
self._sign = value[0]
if value[2] == 'F':
# infinity: value[1] is ignored
self._int = '0'
self._exp = value[2]
self._is_special = True
else:
# process and validate the digits in value[1]
digits = []
for digit in value[1]:
if isinstance(digit, int) and 0 <= digit <= 9:
# skip leading zeros
if digits or digit != 0:
digits.append(digit)
else:
raise ValueError("The second value in the tuple must "
"be composed of integers in the range "
"0 through 9.")
if value[2] in ('n', 'N'):
# NaN: digits form the diagnostic
self._int = ''.join(map(str, digits))
self._exp = value[2]
self._is_special = True
elif isinstance(value[2], int):
# finite number: digits give the coefficient
self._int = ''.join(map(str, digits or [0]))
self._exp = value[2]
self._is_special = False
else:
raise ValueError("The third value in the tuple must "
"be an integer, or one of the "
"strings 'F', 'n', 'N'.")
return self
View on GitHub (pinned to bc6749cc3b)
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')
Example fix
// before
d = Decimal((0, tuple('142'), 0)) # strings -> ValueError
// after
d = Decimal((0, tuple(int(c) for c in '142'), 0)) # Decimal('142') Defensive patterns
Strategy: type-guard
Validate before calling
def valid_digits(digits) -> bool:
return all(isinstance(d, int) and 0 <= d <= 9 for d in digits)
# digits = tuple(int(c) for c in digit_str) if digit_str.isascii() and digit_str.isdigit() else digits Type guard
def is_digit_tuple(digits) -> bool:
return hasattr(digits, '__iter__') and all(
isinstance(d, int) and not isinstance(d, bool) and 0 <= d <= 9
for d in digits
) Try / catch
try:
d = Decimal((sign, digits, exp))
except ValueError as e:
if 'second value' in str(e):
digits = tuple(int(c) for c in ''.join(map(str, digits)))
d = Decimal((sign, digits, exp)) Prevention
- 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
When it happens
Trigger: 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.
Common situations: 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.
Related errors
- Invalid tuple size in creation of Decimal from list or tuple
- Invalid sign. The first value in the tuple should be an int
- The third value in the tuple must be an integer, or one of t
- argument must be a multiple of 32, with a maximum of {IEEE_C
- skip_file_prefixes must be a tuple of strs.
AI-assisted analysis of python/cpython@bc6749cc3b (2026-08-14).
Data as JSON: /api/errors/6fb5486227033e52.
Report an issue: GitHub.