Skip to content

Require exactly two cells for a CSV QA pair - #19008

Merged
yuzhichang merged 3 commits into
infiniflow:mainfrom
devYRPauli:fix/qa-csv-strict-pair
Sep 3, 2026
Merged

yuzhichang merged 3 commits into
infiniflow:mainfrom
devYRPauli:fix/qa-csv-strict-pair

Conversation

@devYRPauli

@devYRPauli devYRPauli commented Aug 30, 2026 •

Copy link
Copy Markdown
Contributor

Summary

Closes part of #19006. Follows the review on #17902.

Problem

extractQATable drops empty cells, then pairs texts[0] with texts[1]:

if t != "" {
    texts = append(texts, t)
}
...
if len(texts) >= 2 {
    pairs = append(pairs, qaPair{Question: texts[0], Answer: texts[1]})
}

A CSV row question,,extra renders as <td>question</td><td></td><td>extra</td>, because office_table_render.go:238 writes one cell per field and keeps the empty ones. The pair becomes question and extra. The empty middle column is skipped without notice.

Python does not apply one rule to every format:

source Python rule
.csv rag/app/qa.py:365 the row must hold exactly two fields
.xls, .xlsx rag/app/qa.py:53 skip empty cells, take the first two values

Which formats reach this function

csv_parser.go and xls_parser.go emit OutputFormat: "html", so they reach extractQATable. xlsx_parser.go no longer does. #18800 changed its return from "html" to "json" on 2026-08-27, so an .xlsx file now reaches extractQAJSON.

This change is therefore CSV-only in effect, plus .xls, which keeps its current behaviour. It is a no-op for .xlsx.

Change

extractQATable takes a strictPairs flag. When it is set, a row makes a pair only if it holds exactly two cells. The call site sets it from the source document name, the same signal Python dispatches on.

.xls behaviour does not change.

Scope

This stops the incorrect pairing on the HTML table path. Two things stay out:

Tests

Three tests in qa_test.go, all driving the component through Invoke:

  • TestQAChunker_CSVStrictPairRejectsThreeCells - a .CSV source with question, empty, extra produces no chunk. The name is uppercase on purpose, to cover the case-insensitive match.
  • TestQAChunker_CSVStrictPairAcceptsTwoCells - a normal two-column CSV row still pairs.
  • TestQAChunker_NonCSVHTMLThreeCellsKeepsFirstTwo - the same three-cell row on an .xls source still pairs question with extra, so the non-CSV path is unchanged.

Results:

CGO_ENABLED=0 go test ./internal/ingestion/component/chunker/
ok  	ragflow/internal/ingestion/component/chunker	1.124s

CGO_ENABLED=0 go vet ./internal/ingestion/component/chunker/
(no output)

Reverting only the source guard and keeping the tests fails one test, and only the intended one:

--- FAIL: TestQAChunker_CSVStrictPairRejectsThreeCells
    qa_test.go:147: expected 0 chunks, got 1
--- PASS: TestQAChunker_CSVStrictPairAcceptsTwoCells
--- PASS: TestQAChunker_NonCSVHTMLThreeCellsKeepsFirstTwo

Note on the local build

go build ./internal/... needs CGO_ENABLED=0 on macOS, or it stops on a missing office_oxide.h. With cgo off, one unrelated package still fails on this platform:

internal/agent/sandbox/local.go:307:3: unknown field Pdeathsig in struct literal of type "syscall".SysProcAttr

Pdeathsig is Linux only and that file carries //go:build !windows. The same failure appears on a clean main, so it is not from this change.

extractQATable dropped empty cells and then paired texts[0] with texts[1].
A CSV row "question,,extra" renders as three <td> cells, so the pair became
"question" and "extra". The empty middle column was skipped without notice.

Python does not use one rule for every format. rag/app/qa.py:365 requires
exactly two fields for CSV. rag/app/qa.py:53 skips empty cells for XLS and
XLSX, which is what the Go code already did.

Apply the strict rule to CSV only, selected by the source document name.
XLS and XLSX keep their current behaviour.

Co-Authored-By: Claude Opus 5 (1M context) <[email protected]>
@dosubot dosubot Bot added size:S This PR changes 10-29 lines, ignoring generated files. 🐞 bug Something isn't working, pull request that fix bug. 🧪 test Pull requests that update test cases. labels Aug 30, 2026
@coderabbitai

coderabbitai Bot commented Aug 30, 2026 •

Copy link
Copy Markdown
Contributor

Review Change Stack

No actionable comments were generated in the recent review. 🎉

ℹ️ Recent review info
⚙️ Run configuration

Configuration used: Organization UI

Review profile: CHILL

Plan: Team

Run ID: 1d65340c-281c-481f-a05d-f4500f452d8f

📥 Commits

Reviewing files that changed from the base of the PR and between 2da6414 and f080b22.

📒 Files selected for processing (1)
  • internal/ingestion/component/chunker/qa_test.go

Included review availability: Your plan provides up to 10 included reviews per hour; 8 remain after this review.


📝 Walkthrough

Walkthrough

The QA chunker now applies strict two-cell parsing to CSV HTML tables. Non-CSV tables retain first-two-non-empty-cell handling. Tests cover CSV rejection, CSV acceptance, and non-CSV HTML handling.

Changes

QA chunker table parsing

Layer / File(s) Summary
Strict CSV pair routing and extraction
internal/ingestion/component/chunker/qa.go
QAChunkerComponent.invoke detects CSV file names and passes strict-pair mode to extractQATable. Strict mode skips rows that do not contain exactly two cells.
CSV and non-CSV parsing tests
internal/ingestion/component/chunker/qa_test.go
Tests verify three-cell CSV row rejection, two-cell CSV row acceptance, and first-two-non-empty-cell extraction for a non-CSV HTML table.

Estimated code review effort: 2 (Simple) | ~10 minutes

Merge Risk: ⚪ Minimal · up to f080b

CSV QA parsing now rejects rows that do not contain exactly two cells while preserving existing behavior for other formats. The change is localized and covered by tests, with no actionable merge-blocking risk remaining.

Poem

A rabbit checks each table row,
Two CSV cells must neatly show.
Extra cells are skipped from sight,
Non-CSV pairs keep their prior right.
Tests confirm the chunks are bright.

🚥 Pre-merge checks | ✅ 4 | ❌ 1

❌ Failed checks (1 warning)

Check name Status Explanation Resolution
Docstring Coverage ⚠️ Warning Docstring coverage is 12.50% which is insufficient. The required threshold is 80.00%. Docstring coverage is scoped to functions touched by this diff. Analyzed 8 functions across 2 files. Write docstrings for the functions missing them to satisfy the coverage threshold.
✅ Passed checks (4 passed)
Check name Status Explanation
Linked Issues check ✅ Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check ✅ Passed Check skipped because no linked issues were found for this pull request.
Description check ✅ Passed The description includes the required Summary section and provides clear problem context, implementation details, scope, tests, and build information.
Title check ✅ Passed The title is concise, specific, and accurately identifies the main change: requiring exactly two cells for CSV QA pairs.
  • Fix all pre-merge checks with AI

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands.

@devYRPauli

Copy link
Copy Markdown
Contributor Author

The checks on this PR all report SKIPPED. tests.yml gates every job on the ci label:

if: ${{ github.event_name != 'pull_request' || (github.event.pull_request.draft == false && contains(github.event.pull_request.labels.*.name, 'ci') && (github.event.action != 'labeled' || github.event.label.name == 'ci')) }}

This PR carries bug, size:S and test. All 9 check runs skip, which reads like a pass in the checks list.

I cannot add the label myself. Please add ci when you want the suite to run.

Local results are in the PR body. go test ./internal/ingestion/component/chunker/ passes. Reverting the source guard and keeping the tests fails one test, and only the intended one.

@xugangqiang

xugangqiang commented Sep 2, 2026 •

Copy link
Copy Markdown
Contributor

Thanks for the fix. The change is minimal and correctly aligns CSV QA pairing with Python (qa.py:366 requires exactly two fields). I reviewed it against current main and have a few findings plus one question for you.

Findings

1. PR description premise is stale relative to current main (Improvement, non-blocking)
The description states csv_parser.go, xlsx_parser.go, and xls_parser.go all emit OutputFormat: "html" and share extractQATable. That was true when the PR was opened (2026-08-30), but xlsx_parser.go was introduced by #19083 (2026-09-01) and now returns OutputFormat: "json" (xlsx_parser.go:152). So on current main, XLSX goes through extractQAJSON, not extractQATable.

  • Consequence: this change is effectively CSV-only (+ XLS, which still emits html). For XLSX it is a no-op because the function is never reached.
  • Please update the description so reviewers don't assume "XLSX behavior unchanged" is a guarantee about the XLSX production path.

2. The XLSX regression test is synthetic (Nit)
TestQAChunker_XLSXThreeCellsKeepsFirstTwoNonEmpty manually sets "output_format": "html" with a test.xlsx name. It validates the non-CSV HTML branch, not the real XLSX path (which is JSON). Consider renaming it to something like TestQAChunker_NonCSVHTMLThreeCellsKeepsFirstTwo to avoid implying it exercises the XLSX production flow.

3. Known out-of-scope gaps (not introduced by this PR, just noting)

  • Python's CSV continuation handling (qa.py:367-369: a non-2-field row with a pending question is appended to the answer) is not implemented here — correctly scoped out, but a follow-up.
  • A two-cell row with an empty answer (e.g. question,) is still paired in Go, whereas Python keeps it pending. Pre-existing behavior, not changed here.

Question for the author

Since #19083, XLSX now outputs json and the QA chunker handles it via extractQAJSON instead of extractQATable. This PR only adjusts the HTML/table path. Do you know whether the JSON path for XLSX already applies the Python-consistent rule (skip empty cells, take the first two), or does it need its own adaptation to match Python? If the JSON path has the same "wrong pairing" bug for XLSX, it may be worth a follow-up PR. Could you confirm?

@xugangqiang xugangqiang added the ci Continue Integration label Sep 2, 2026
devYRPauli and others added 2 commits September 2, 2026 10:37
TestQAChunker_XLSXThreeCellsKeepsFirstTwoNonEmpty set output_format to
"html" with an .xlsx name. Since infiniflow#18800 the xlsx parser emits "json", so
an .xlsx file reaches extractQAJSON and never reaches this branch. The
name claimed a path the test does not exercise.

Rename it to TestQAChunker_NonCSVHTMLThreeCellsKeepsFirstTwo and use an
.xls name, which does still emit "html".

Co-Authored-By: Claude Opus 5 (1M context) <[email protected]>
Upstream infiniflow#19108 moved every test in this package from context.Background()
to t.Context() and removed the "context" import from qa_test.go. This branch
still called context.Background() in the three tests it adds, so the merge of
this branch with main had three calls and no import.

Go build failed with "undefined: context" at qa_test.go:140, :160 and :187 in
the ragflow_tests_infinity (go) and ragflow_tests_elasticsearch (go) jobs.

Use t.Context() in those three tests to match the current convention.

Co-Authored-By: Claude Opus 5 (1M context) <[email protected]>
@devYRPauli

Copy link
Copy Markdown
Contributor Author

1. Stale description. Corrected. I also traced the change: xlsx_parser.go dates to #16647, and #18800 flipped its return from "html" to "json" on 2026-08-27. #19083 is not the commit.

git log -L 150,155:internal/parser/parser/xlsx_parser.go
82e363919 feat: extract images from xlsx parser (#18800)
-		OutputFormat: "html",
+		OutputFormat: "json",

The description now states that this change is CSV-only in effect, plus .xls, and a no-op for .xlsx.

2. Synthetic XLSX test. Renamed to TestQAChunker_NonCSVHTMLThreeCellsKeepsFirstTwo and switched to an .xls name, which still emits "html". Pushed as 2da6414.

3. Out-of-scope gaps. Agreed on both. One correction on the first: the Go text paths already implement Python's continuation rule. extractQATextTab and extractQATextCSV both require exactly two fields and append a non-conforming row to the pending answer. Only the table path lacks it.

Your question. The XLSX JSON path does not apply the Python rule, and it does not pair rows wrongly either. It produces nothing at all.

xlsx_parser.go:152 puts the rendered HTML table in each item's text field and returns "json". extractQAJSON sends that text to extractQAText, which splits on newlines, runs detectDelimiter, then parses the string as CSV or tab-separated text. An HTML <table> string holds no two-field record, so no pair is built.

Measured with one spreadsheet driven through the real parser and the real chunker:

commit parser output_format QA chunks
4168b3f9d (main) json 0
6c2bca893 (parent of #18800) html 3

The rendered HTML is byte-identical in both. Only the route changed, and the chunker was never adapted to it.

So this needs its own fix, and it is more serious than the pairing gap. I filed it as #19174 with the reproduction test and the bisect.

Two repairs are possible. extractQAJSON can detect a table item by doc_type_kwd == "table" and send its text to extractQATable. Or xlsx_parser.go can keep "html" and carry the images separately. The first is smaller and keeps the image work from #18800. Tell me which you want and I will send it.

CI. The go jobs were red for a separate reason. #19108 moved this package to t.Context() and dropped the "context" import. The merge of this branch with main then had three context.Background() calls and no import. Fixed in f080b22.

@xugangqiang

Copy link
Copy Markdown
Contributor

LGTM

@yuzhichang
yuzhichang merged commit b502ddf into infiniflow:main Sep 3, 2026
4 checks passed
euvre added a commit to euvre/ragflow that referenced this pull request Sep 7, 2026
Main's infiniflow#19008 added TestQAChunker_CSVStrictPairAcceptsTwoCells and
TestQAChunker_NonCSVHTMLThreeCellsKeepsFirstTwo reading the chunk
content from content_with_weight. This branch carries QA content in
the canonical text field (ingestion renames it to content_with_weight
at the index boundary), so both tests saw an empty string after the
merge. Read text like every other QA test adapted in this PR.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

🐞 bug Something isn't working, pull request that fix bug. ci Continue Integration size:S This PR changes 10-29 lines, ignoring generated files. 🧪 test Pull requests that update test cases.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants