ValueError: invalid literal for int() with base 10: ''

Python · updated 2026-11-03

Raised by int() when the string isn't a plain base-10 integer. The value after the colon, in quotes, is exactly what int() received, which is usually the fastest clue.

Traceback (most recent call last):
  File "/app/import_orders.py", line 31, in <module>
    quantity = int(row["quantity"])
               ^^^^^^^^^^^^^^^^^^^^
ValueError: invalid literal for int() with base 10: ''

Quick fix

Look at the quoted value in the message. '' means an empty field, '3.5' means a decimal, '1,200' means a thousands separator, '12\n' or ' 12 ' means whitespace was kept from the source. Clean or validate the value before converting, and decide what an empty field should mean.

  1. Read the quoted value in the error message: that is what int() received.
  2. Strip whitespace and separators first: int(value.strip().replace(",", "")).
  3. For decimals, convert with int(float(value)) only if dropping the fraction is correct, or use Decimal.
  4. Treat empty strings explicitly: decide whether they mean 0, None or a skipped row.
  5. Wrap conversions of user or file input in try/except ValueError and log the bad value with its row.
python
def to_int(value: str | None, default: int | None = None) -> int | None:
    if value is None or not value.strip():
        return default
    try:
        return int(value.strip().replace(",", ""))
    except ValueError:
        log.warning("not an integer: %r", value)
        return default

quantity = to_int(row["quantity"], default=0)

Why it happens

An empty field from a CSV, form or query string

csv.DictReader returns '' for empty cells and form fields arrive as '' when left blank. int('') raises this error, which is why the message so often ends with two quotes.

A decimal number

int('3.5') fails even though int(3.5) works: int() only parses whole-number strings. Prices, weights and averages are the usual examples.

Formatting from the source

Thousands separators ('1,200'), currency symbols ('$40'), units ('12kg') and trailing newlines from file reads all make the string something int() won't parse.

The wrong column or key

A header that shifted, a renamed JSON key or a column read by position can feed a name or date into int(), so the quoted value is text that was never meant to be a number.

Other fixes

  • Use str.isdigit() only for simple non-negative values: it rejects '-5' and accepts some Unicode digits int() might not expect.
  • With pandas, pd.to_numeric(df[col], errors="coerce") turns bad values into NaN so you can inspect them in one pass.
  • Validate request input with pydantic or similar so bad numbers are rejected with a clear 422 instead of a 500.

Same error, other wording

  • ValueError: invalid literal for int() with base 10: '3.5'
  • ValueError: invalid literal for int() with base 10: 'abc'
  • ValueError: could not convert string to float: ''

Let your coding agent catch the next one

Runheed records this error in production with the stack trace, the breadcrumbs before it and the agent session that shipped the code, and serves it to Claude Code, Cursor or any MCP client. With it connected, you can ask:

> Find the ValueError: invalid literal for int() errors in production, show me the bad values and which input they came from, and add a safe conversion that handles empty and formatted values at each spot.

Plans from $29/moSee what your agent readsSee the setupCancel any time from the billing portal

Related errors

References: Python docs: int(), Python docs: built-in exceptions (ValueError)