DEV Community

Ship Stack
Ship Stack

Posted on Originally published at youtu.be

An AI Agent Wrote a CSV Parser in Node. I Tested It With Real Inputs

I asked an AI agent to write a CSV parser in Node.js and then I tested it for real, first the version everyone writes by instinct, then the version that actually survives. Everything below was run on my machine. The full run is on video: https://youtu.be/zdCNzOnxUv4

The naive version

Every one of us has written this:

const rows = text.split('\n').map(line => line.split(','));
Enter fullscreen mode Exit fullscreen mode

Split on newline, split on comma, done. I ran it against 5 test cases drawn from real CSV files: quoted fields containing commas, escaped quotes inside a field, a newline inside a quoted field, and Windows line endings (CRLF).

It passed 1 of 5.

Quoted commas break the split because the comma inside quotes is not a delimiter. Escaped quotes ("she said ""hi""") come out mangled. A newline inside a quoted field creates phantom rows. CRLF leaves a stray \r glued to every last column. Only the plainest comma-separated file survives.

The state machine version

The agent then produced a 17-line state machine parser. The idea is small: walk the text one character at a time and track whether you are inside quotes. A comma only ends a field when you are outside quotes; a quote only closes when it is not followed by another quote; anything else, including newlines, is just data.

I ran it against the full suite: all 5 cases from before plus extra edge cases, 8 tests in total. It passed 8 of 8, including correctly raising an error on an unterminated quote, which is the case most hand-rolled parsers silently swallow.

The difference between 1 of 5 and 8 of 8 is not cleverness. It is that CSV is a format with quoting rules, and quoting rules need state. split(',') has no state, so it cannot tell a delimiter from data.

What I did not test

Very large files, encodings other than UTF-8, and full RFC 4180 coverage are still open. For production, a battle-tested library like csv-parse or papaparse is still the right call. But as a demonstration of why the naive split fails, the test run is the whole point.

Watch the run

The code, the terminal output, and every pass/fail are in the video: https://youtu.be/zdCNzOnxUv4

Subscribe to Ship Stack AI on YouTube and tell me in the comments what to build and break next.

Top comments (0)