Skip to content

fix(extraction): don't treat prose-with-commas as CSV - #62

Merged
acidkill merged 1 commit into
acidkill:mainfrom
RobertSigmundsson:upstream-pr/csv-prose-guard
Jul 15, 2026
Merged

acidkill merged 1 commit into
acidkill:mainfrom
RobertSigmundsson:upstream-pr/csv-prose-guard

Conversation

@RobertSigmundsson

Copy link
Copy Markdown
Contributor

Hi Toni!

Last of the noise-quality fixes our production usage surfaced — small, but it's a memory-poisoning source that hits anyone feeding free-form text through auto-mode extraction (so, most auto-capture setups).

The problem

_detect_csv_row treats any single line with ≥2 commas and ≥3 fields as a headerless CSV row. The only prose guard rejects the row when all fields are plain text — so ordinary prose that happens to contain one date, price or number in any comma-separated fragment slips through. A line like "Met the team on 2026-01-14, reviewed the roadmap, and signed off the budget" is stored as ContentFormat.CSV_ROW with synthetic col_0/col_1/… fields, each of which becomes an entity-cell entry. Over time these accumulate and surface in recall as col_0 | col_1 | col_2 … garbage — we saw exactly that in our store's recall output.

The fix

One guard on the headerless branch: if any cell is long (> 40 chars) or multi-word (> 6 words), the line is prose, not CSV. Real headerless CSV cells are short, token-like values, so the false-negative risk is negligible — genuine short-cell data rows (dates, numbers, currency) still detect. The existing all-text guard stays; this just closes the "one number rescues the whole prose line" gap in front of it.

Type of Change

  • Bug fix (data quality; non-breaking)

Testing

  • Unit tests added — TestCsvProseGuard (2 cases): a prose line with an embedded date is now rejected, and a genuine short-cell row (2026-01-01,500,USD) still detects.
  • Full unit suite on this branch (rebased on current main): 6,082 passed, 33 skipped in ~35 s (-n0), clean exit. ruff and mypy clean on the touched file.

Checklist

  • Code follows the project's style (ruff/mypy).
  • Self-review performed.
  • Tests added that prove the fix.
  • CHANGELOG.md — proposed entry below; happy to fold it into the branch.

Optional extra hardening (open to discussion)

If you'd rather be stricter, a headerless single-line row could additionally require ≥2 non-text field types, or its confidence (currently 0.7) could be dropped below the encode threshold — I kept the change minimal here, but I'm glad to adjust to whatever bar you prefer.

Proposed CHANGELOG.md entry (Unreleased / Fixed)

_detect_csv_row no longer classifies prose-with-commas as a CSV row. A single comma-separated line was accepted as headerless CSV whenever any one cell held a date/number, so ordinary sentences were stored with synthetic col_N fields that polluted extraction. A prose guard now rejects the row when any cell is long (>40 chars) or multi-word (>6 words); genuine short-cell data rows still detect.

Robert

_detect_csv_row accepts any single line with >=2 commas and >=3 fields as a
headerless CSV row. The only prose guard rejected the row when ALL fields were
plain text — so ordinary prose containing one date, price or number in any
comma-separated fragment (release notes, meeting summaries, "on 2026-07-08 we
rotated the password, and ...") was stored as ContentFormat.CSV_ROW with
synthetic col_0/col_1/... fields. Each becomes a spurious entity-cell entry that
accumulates and pollutes downstream extraction / recall.

Add a prose guard on the headerless branch: if any cell is long (>40 chars) or
multi-word (>6 words) the line is prose, not CSV. Real headerless CSV cells are
short, token-like values, so the false-negative risk is negligible; genuine
short-cell data rows (dates, numbers, currency) still detect.

Tests: +2 (prose-with-a-date is rejected; a genuine short-cell row still
detects).
# `col_N` entity-cell entries that pollute downstream extraction. The
# all-text guard below is not enough on its own: a single date or number
# in one cell used to slip prose through.
if any(len(v) > 40 or len(v.split()) > 6 for v in values):

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The two thresholds are deliberately loose: a genuine headerless CSV cell is a short token (a date, a number, a currency code, an id), so > 40 chars or > 6 words only ever trips on free text. It sits in front of the existing all-text guard rather than replacing it, because the gap was specifically that one numeric/date cell was enough to rescue an entire prose line past the all-text check. Happy to tighten to a ≥2-non-text-field rule instead if you'd prefer a structural test over a length heuristic.

@acidkill acidkill left a comment

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

no issues found. thanks for your contribution

@acidkill
acidkill merged commit 8ddff0a into acidkill:main Jul 15, 2026
7 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants