Fastest end-to-end CSV ingestion for Ruby (with C acceleration). SmarterCSV auto-detects formats, applies smart defaults, and returns Rails-ready hashes for sea
Fastest end-to-end CSV ingestion for Ruby (with C acceleration). SmarterCSV auto-detects formats, applies smart defaults, and returns Rails-ready hashes for sea
[!TIP] Upgrading from an older version? Use the SmarterCSV Upgrade Wizard to walk through what (if anything) you need to change for your specific version. Most steps do not require any changes.
SmarterCSV is a high-performance CSV ingestion and generation for Ruby, focused on fast end-to-end CSV ingestion of real-world data — no silent failures, no surprises, not just tokenization.
⭐ If SmarterCSV saved you hours of import time, please star the repo, and consider sponsoring this project.
Ruby's built-in CSV library has 10 documented failure modes that can silently corrupt or lose data — duplicate headers, blank header cells, extra columns, BOMs, whitespace, encoding issues, and more — all without raising an exception. SmarterCSV handles 8 our of 10 by default, and the remaining 2 with a single option each.
See Ruby CSV Pitfalls for 10 ways
CSV.readsilently corrupts or loses data, and how SmarterCSV handles them.
[!TIP] No silent precision loss (new in 1.18.0). For scientific data, GPS/geo coordinates, and financial figures — which routinely carry 16+ significant digits — Ruby's standard CSV converts with
Float(), so a value like1234567890.123456789is silently rounded to1234567890.1234567. SmarterCSV's defaultdecimal_precision: :autoreturns aBigDecimalfor values beyond 16 significant digits (andFloatotherwise) — full precision, no data loss. Floats are decoded with the Eisel-Lemire algorithm: correctly rounded, bit-for-bit identical toString#to_f.
Beyond raw speed, SmarterCSV is designed to provide a significantly more convenient and developer-friendly interface than traditional CSV libraries. Instead of returning raw arrays that require substantial post-processing, SmarterCSV produces Rails-ready hashes for each row, making the data immediately usable with ActiveRecord, Sidekiq pipelines, parallel processing, and JSON-based workflows such as S3.
In a Rails app, warnings auto-route through Rails.logger and instrumentation hooks compose with ActiveSupport::Notifications — no setup required. Outside Rails, warnings fall back to $stderr and the same APIs work without any framework dependency.
The library includes intelligent defaults, automatic detection of column and row separators, and flexible header/value transformations. These features eliminate much of the boilerplate typically required when working with CSV data and help keep ingestion code concise and maintainable.
For large files, SmarterCSV supports both chunked processing (arrays of hashes) and streaming via Enumerable APIs, enabling efficient batch jobs and low-memory pipelines.
As of 1.17.0, SmarterCSV also accepts non-seekable streaming inputs — pipes, STDIN, Zlib::GzipReader, and HTTP responses — with no need to materialize the file on disk first.
The C acceleration further optimizes the full ingestion path — including parsing, hash construction, and conversions — so performance gains reflect real-world workloads, not just tokenizer benchmarks.
The interface is intentionally designed to robustly handle messy real-world CSV while keeping application code clean. Developers can easily map headers, skip unwanted rows, quarantine problematic data, and transform values on the fly without building custom post-processing pipelines. See Real-World CSV Files for a comprehensive guide to production CSV patterns.
When exporting data, SmarterCSV converts arrays of hashes back into properly formatted CSV, maintaining the same focus on convenience and correctness.
User Testimonial:
"Best gem for CSV for us yet. […] taking an import process from 7+ hours to about 3 minutes. […] SmarterCSV was a big part and helped clean up our code A LOT."
SmarterCSV is designed for real-world CSV processing, returning fully usable hashes with symbol keys and type conversions — not raw arrays that require additional post-processing.
Beware of benchmarks that only measure raw CSV parsing. Such comparisons measure tokenization alone, while real-world usage requires hash construction, key normalization, type conversion, and edge-case handling. Omitting this work understates the actual cost of CSV ingestion.
For a fair comparison, CSV.table is the closest Ruby CSV equivalent to SmarterCSV.
| Comparison (SmarterCSV 1.17.0, C-accelerated) | Range |
|---|---|
| vs SmarterCSV 1.15.2 (with C acceleration) | up to 2.8× faster |
| vs SmarterCSV 1.14.4 (with C acceleration) | 9×–82× faster |
| vs SmarterCSV 1.14.4 (Ruby path) | 2.4×–19.8× faster |
| vs CSV.read (arrays of arrays) | 1.3×–7.9× faster |
| vs CSV.table (arrays of hashes) | 4.9×–132× faster |
| vs ZSV 1.3.0 (arrays of hashes, equiv. output) | 1.1×–6.6× faster † |
† SmarterCSV faster on 15 of 16 files. ZSV raw arrays (no hashes, no conversions) are 2×–14× faster — but that omits the post-processing work needed to produce usable output. ZSV row carried over from the 1.16.0 benchmark; not re-measured for 1.17.0.
Benchmarks: 19 CSV files (20k–240k rows), Ruby 3.4.7, Apple M4.
⁉️ Why these numbers look a touch lower than 1.16.0 charts? TL;DR: because we use different statistic methods.
Earlier versions of these benchmarks reported the best-of-N sample (the absolute
min/ fastest run) for each measurement. A single lucky run — empty caches lining up, no scheduler interrupts — could shave up to ~10% off and become the headline number. I think that would be misleading. Because of that, we've switched to the 10th-percentile (p10) of multiple runs of 40 samples, which discards roughly the four luckiest runs and reports a time much closer to what you'll actually observe in production. On noisier fixturesp10is ~5–10% abovemin; on quiet ones it's within 1%. The relative ordering between versions and adapters is unchanged; the absolute speedup figures are simply more honest.
See SmarterCSV 1.15.2: Faster Than Raw CSV Arrays and PR #319 for more details.
It's a one-line change:
# Before
rows = CSV.table('data.csv').map(&:to_h)
# After — up to 132× faster, same symbol keys
rows = SmarterCSV.process('data.csv')SmarterCSV.parse(string) works like CSV.parse(string, headers: true, header_converters: :symbol) — with numeric conversion included by default:
data = SmarterCSV.parse(csv_string)Numeric conversion is also more accurate: where Ruby's :numeric/:float converters round high-precision decimals through Float(), SmarterCSV's default decimal_precision: :auto returns a BigDecimal past 16 significant digits, so no precision is lost (pass decimal_precision: :float for like-for-like Float output).
SmarterCSV is designed for robustness — real-world CSV data often has inconsistent formatting, extra whitespace, and varied column separators. Its intelligent defaults automatically clean and normalize data, returning high-quality hashes ready for direct use with ActiveRecord, Sidekiq, or any data pipeline — no post-processing required. See Parsing CSV Files in Ruby with SmarterCSV for more background.
$ cat spec/fixtures/sample.csv
First Name , Last Name , Emoji , Posts
José ,Corüazón, ❤️, 12
Jürgen, Müller ,,3
Michael, May ,, 7
$ irb
>> require 'smarter_csv'
=> true
>> data = SmarterCSV.process('spec/fixtures/sample.csv')
=> [{:first_name=>"José", :last_name=>"Corüazón", :emoji=>"❤️", :posts=>12},
{:first_name=>"Jürgen", :last_name=>"Müller", :emoji=>"", :posts=>3},
{:first_name=>"Michael", :last_name=>"May", :emoji=>"", :posts=>7}]Notice how SmarterCSV automatically (all defaults):
downcase_header: true, strings_as_keys: falsestrip_whitespace: trueconvert_values_to_numeric: trueremove_empty_values: trueOnce the header line is read, SmarterCSV normalizes it through these steps:
comment_regexp → strip_chars_from_headers → split on col_sep → strip quote_char
→ strip_whitespace → [gsub spaces/dashes→_ → downcase_header]
→ disambiguate_headers → symbolize → key_mappinguser_provided_headers bypasses all of the above. Each step is individually configurable. See Header Transformations for the full step-by-step table and options.
After each row is parsed, SmarterCSV applies a transformation pipeline to field values:
strip_whitespace → nil_values_matching → remove_empty_values → remove_zero_values
→ convert_values_to_numeric → value_converters → remove_empty_hashesEach step is individually configurable. See Data Transformations and Value Converters for details.
Per-column lambdas convert raw strings into typed values — dates, currency, booleans:
require 'date'
data = SmarterCSV.process('orders.csv',
value_converters: {
dob: ->(v) { v && Date.strptime(v, '%m/%d/%Y') },
price: ->(v) { v&.delete('$,')&.to_f },
active: ->(v) { v&.match?(/\Atrue\z/i) },
})See Value Converters.
Processing large CSV files in chunks minimizes memory usage and enables powerful workflows:
chunk_index parameter enables progress reportingThe block receives a chunk (array of hashes) and an optional chunk_index (0-based sequence number):
# Database bulk import
SmarterCSV.process(filename, chunk_size: 100) do |chunk, chunk_index|
puts "Processing chunk #{chunk_index}..."
MyModel.insert_all(chunk) # chunk is an array of hashes
end
# Parallel processing with Sidekiq
SmarterCSV.process(filename, chunk_size: 100) do |chunk|
Sidekiq::Client.push_bulk('class' => MyWorker, 'args' => chunk) # each chunk processed in parallel
endSee Batch Processing for chunk sizing, each_chunk, and parallel-worker patterns.
Reader#each is the modern, idiomatic way to process rows — Reader includes Enumerable, so all standard Ruby methods work:
reader = SmarterCSV::Reader.new('data.csv', options)
reader.each { |hash| MyModel.upsert(hash) }
# Enumerable methods
active = reader.select { |h| h[:status] == 'active' }
names = reader.map { |h| h[:name] }
# Lazy — stop early without reading the whole file
first_ten = reader.lazy.select { |h| h[:active] }.first(10)No open issues yet, or sync has not completed.