csv.field_size_limit

A safety net against runaway fields (e.g. an unclosed quote swallowing the rest of a file). The limit is module-wide, counts characters, and applies to reading only.

csv functionPython 2.5+Live demo
Common call
csv.field_size_limit(10_000_000)
Returns
the previous limit, 131072 at first
Replaces
-
Watch out
global for the whole process; sys.maxsize overflows on Windows before 3.13.1
csv.field_size_limit([new_limit])
→ int

Demo

Live evaluation
Set a limit, read one line, put the old limit back. A field longer than the limit raises csv.Error.
Try:
Inputs
limitintmax characters per field
linestrone CSV line
Code
import csv
old = csv.field_size_limit(5)
try:
    rows = list(csv.reader(['abcde,xy']))
finally:
    csv.field_size_limit(old)
(old, rows)
Result
(131072, [['abcde', 'xy']])

The limit counts the characters of the field value: "abcde" fits a limit of 5, "abcdef" raises _csv.Error: field larger than field limit (5). The surrounding quotes of "abc" are not part of the value, so a limit of 3 is enough. With 0 no field may have even one character, but empty fields are still fine. The snippet restores the old limit in finally because the setting is shared by the whole program.

Parameters

NameTypeRequiredDescription
new_limitintnoThe new maximum number of characters per field. Omit it to only read the current value.

Return value

int — The limit that was in force before the call.

Common patterns

Allow very large fields
Up to Python 3.13.0 the limit was a C long — 32 bits on Windows — so sys.maxsize raised OverflowError there; halving until it fits works everywhere.
import csv, sys
limit = sys.maxsize
while True:
    try:
        csv.field_size_limit(limit)
        break
    except OverflowError:
        limit //= 2
Raise it temporarily
Restore the previous value when done.
import csv
old = csv.field_size_limit(10_000_000)
try:
    rows = list(csv.reader(f))
finally:
    csv.field_size_limit(old)

Examples

1. Read the current limit
import csv csv.field_size_limit()
Returns
131072
2. A field over the limit
import csv next(csv.reader(['x' * 131073]))
Returns
_csv.Error: field larger than field limit (131072)
3. Exactly the limit is fine
import csv len(next(csv.reader(['x' * 131072]))[0])
Returns
131072
4. Setting returns the old value
import csv old = csv.field_size_limit(500) try: now = csv.field_size_limit() finally: csv.field_size_limit(old) (old, now)
Returns
(131072, 500)
5. Only ints
import csv csv.field_size_limit('1000')
Returns
TypeError: limit must be an integer
6. Writing is not limited
import csv, io buf = io.StringIO(newline='') csv.writer(buf).writerow(['x' * 200000]) len(buf.getvalue())
Returns
200002

Pitfalls

1. An unclosed quote hits the limit
Without a closing quote the rest of the input becomes one field. The limit error is often a symptom of broken quoting, not of a legitimately huge field.
raise the limit
import csv
lines = ['a,"b'] + ['x' * 100] * 2000
len(next(csv.reader(lines)))
_csv.Error: field larger than field limit (131072)
strict=True
import csv
lines = ['a,"b'] + ['x' * 100] * 10
try:
    next(csv.reader(lines, strict=True))
except csv.Error as e:
    msg = str(e)
msg
'unexpected end of data'

When to use

Use it
  • Files with legitimately huge fields (embedded documents, base64 blobs)
Reach for something else
  • Hiding broken quoting — check the data first (strict=True helps)

Notes

CPython impl
Modules/_csv.c — field_limit in the module state (128 * 1024); parse_add_char raises "field larger than field limit (%zd)" before appending the character that would exceed it
Range
Stored as a C Py_ssize_t since 3.13.1 (a C long before, 32-bit on Windows); beyond it: OverflowError. A non-int is TypeError: limit must be an integer
Scope
Module-wide: affects every reader in the process, including other threads

FAQ

First check for an unclosed quote. If the fields really are that large, raise the limit before reading: csv.field_size_limit(10_000_000) (or the largest value your platform accepts).