[Bug] tools/qa_chunk: KeyError when accessing CSV rows with integer column index (header-present CSV) #965

Open
opened 2026-02-16 10:21:08 -05:00 by yindo · 1 comment
Owner

Originally created by @Olemi-llm-apprentice on GitHub (Feb 9, 2026).

Self Checks

  • This is only for bug report, if you would like to ask a question, please head to Discussions.
  • I have searched for existing issues Dify issues & Dify Official Plugins, including closed ones.
  • I confirm that I am using English to submit this report (我已阅读并同意 Language Policy).
  • [FOR CHINESE USERS] 请务必使用英文提交 Issue,否则会被关闭。谢谢!:)
  • Please do not modify this template :) and fill in all the required fields.

Dify version

1.12.x (Knowledge Pipeline feature)

Plugin version

qa_chunk latest (commit 45f3b7c, Sep 4, 2025)

Cloud or Self Hosted

Self Hosted (Docker)

Steps to reproduce

Bug Description

The qa_chunk plugin (tools/qa_chunk/tools/qa.py) raises a KeyError: 1 when processing any CSV file that has a header row. This makes the QA Processor node in Knowledge Pipeline unusable for standard CSV files.

Root Cause

In qa.py lines 42-43, the code accesses pandas DataFrame rows using integer column indices:

question = str(row[question_column])  # question_column = 0 (int)
answer = str(row[answer_column])      # answer_column = 1 (int)

However, pd.read_csv() reads the first row as column names (strings) by default. When iterating with df.iterrows(), each row is a pandas Series indexed by column names (strings), not integers. Accessing row[1] (integer) raises KeyError: 1 because the integer key 1 does not exist in the Series index.

Steps to Reproduce

  1. Create a CSV file with a header row, e.g.:
  2. question,answer
  3. What is Dify?,A workflow tool
  4. What is LLM?,Large Language Model
    1. Create a Knowledge Pipeline using the QA template
    1. Configure the QA Processor node with question_column=0, answer_column=1
    1. Run the pipeline
      Result: KeyError: 1

Error message:

An error occurred in the langgenius/qa_chunk/qa_chunk, please contact the author of langgenius/qa_chunk/qa_chunk for help, error type: KeyError, error details: 1

Suggested Fix

Replace label-based indexing with positional indexing using iloc:

# Before (broken)
question = str(row[question_column])
answer = str(row[answer_column])

# After (fixed)
question = str(row.iloc[int(question_column)])
answer = str(row.iloc[int(answer_column)])

Or alternatively, resolve column names first:

q_col = df.columns[int(question_column)]
a_col = df.columns[int(answer_column)]
for index, row in df.iterrows():
    question = str(row[q_col])
    answer = str(row[a_col])

✔️ Error log

An error occurred in the langgenius/qa_chunk/qa_chunk, please contact the author of langgenius/qa_chunk/qa_chunk for help, error type: KeyError, error details: 1

The error originates from tools/qa_chunk/tools/qa.py, line 43:

answer = str(row[answer_column])  # answer_column = 1 (int), but row is indexed by string column names
Originally created by @Olemi-llm-apprentice on GitHub (Feb 9, 2026). ### Self Checks - [x] This is only for bug report, if you would like to ask a question, please head to [Discussions](https://github.com/langgenius/dify/discussions/categories/general). - [x] I have searched for existing issues [Dify issues](https://github.com/langgenius/dify/issues) & [Dify Official Plugins](https://github.com/langgenius/dify-official-plugins/issues), including closed ones. - [x] I confirm that I am using English to submit this report (我已阅读并同意 [Language Policy](https://github.com/langgenius/dify/issues/1542)). - [x] [FOR CHINESE USERS] 请务必使用英文提交 Issue,否则会被关闭。谢谢!:) - [x] Please do not modify this template :) and fill in all the required fields. ### Dify version 1.12.x (Knowledge Pipeline feature) ### Plugin version qa_chunk latest (commit 45f3b7c, Sep 4, 2025) ### Cloud or Self Hosted Self Hosted (Docker) ### Steps to reproduce ## Bug Description The `qa_chunk` plugin (`tools/qa_chunk/tools/qa.py`) raises a `KeyError: 1` when processing any CSV file that has a header row. This makes the QA Processor node in Knowledge Pipeline unusable for standard CSV files. ## Root Cause In `qa.py` lines 42-43, the code accesses pandas DataFrame rows using integer column indices: ```python question = str(row[question_column]) # question_column = 0 (int) answer = str(row[answer_column]) # answer_column = 1 (int) ``` However, `pd.read_csv()` reads the first row as column **names** (strings) by default. When iterating with `df.iterrows()`, each `row` is a pandas Series indexed by column names (strings), not integers. Accessing `row[1]` (integer) raises `KeyError: 1` because the integer key `1` does not exist in the Series index. ## Steps to Reproduce 1. Create a CSV file with a header row, e.g.: 2. ```csv 3. question,answer 4. What is Dify?,A workflow tool 5. What is LLM?,Large Language Model 6. ``` 7. 2. Create a Knowledge Pipeline using the QA template 8. 3. Configure the QA Processor node with `question_column=0`, `answer_column=1` 9. 4. Run the pipeline **Result:** `KeyError: 1` **Error message:** ``` An error occurred in the langgenius/qa_chunk/qa_chunk, please contact the author of langgenius/qa_chunk/qa_chunk for help, error type: KeyError, error details: 1 ``` ## Suggested Fix Replace label-based indexing with positional indexing using `iloc`: ```python # Before (broken) question = str(row[question_column]) answer = str(row[answer_column]) # After (fixed) question = str(row.iloc[int(question_column)]) answer = str(row.iloc[int(answer_column)]) ``` Or alternatively, resolve column names first: ```python q_col = df.columns[int(question_column)] a_col = df.columns[int(answer_column)] for index, row in df.iterrows(): question = str(row[q_col]) answer = str(row[a_col]) ``` ### ✔️ Error log ``` An error occurred in the langgenius/qa_chunk/qa_chunk, please contact the author of langgenius/qa_chunk/qa_chunk for help, error type: KeyError, error details: 1 ``` The error originates from `tools/qa_chunk/tools/qa.py`, line 43: ```python answer = str(row[answer_column]) # answer_column = 1 (int), but row is indexed by string column names ```
yindo added the bug label 2026-02-16 10:21:08 -05:00
Author
Owner

@dosubot[bot] commented on GitHub (Feb 9, 2026):

Hi @Olemi-llm-apprentice! I'm Dosu and I’m helping the dify-official-plugins team.

The KeyError happens because the code in qa_chunk accesses DataFrame rows using integer indices (like row[1]), but when a CSV has headers, pandas rows are indexed by column names (strings), not integers. This causes the KeyError when processing standard CSV files with headers. The fix is to use positional indexing with .iloc, for example: row.iloc[int(question_column)], or to resolve the column names first and use them as keys (e.g., row[df.columns[int(question_column)]]). No merged pull requests or issues currently address this bug, and the problem appears isolated to qa_chunk—updating the code as described will resolve the error when handling headered CSVs. You can see the relevant code and context here.

To reply, just mention @dosu.


How did I do? Good | Irrelevant | Incorrect | Verbose | Hallucination | Report 🐛 | Other

Open in Cursor Join Discord Share on X

@dosubot[bot] commented on GitHub (Feb 9, 2026): <!-- Greeting --> Hi @Olemi-llm-apprentice! I'm [Dosu](https://go.dosu.dev/dosubot) and I’m helping the dify-official-plugins team. <!-- Answer --> The KeyError happens because the code in `qa_chunk` accesses DataFrame rows using integer indices (like `row[1]`), but when a CSV has headers, pandas rows are indexed by column names (strings), not integers. This causes the KeyError when processing standard CSV files with headers. The fix is to use positional indexing with `.iloc`, for example: `row.iloc[int(question_column)]`, or to resolve the column names first and use them as keys (e.g., `row[df.columns[int(question_column)]]`). No merged pull requests or issues currently address this bug, and the problem appears isolated to `qa_chunk`—updating the code as described will resolve the error when handling headered CSVs. You can see the relevant code and context [here](https://github.com/langgenius/dify-official-plugins/blob/058d25c9d4d3a5b826ac7cfcf82ef2c7e9e482b8/tools/qa_chunk/tools/qa.py#L33-L48). <!-- Dosu Comment Footer --> *To reply, just mention [@dosu](https://go.dosu.dev/dosubot).* --- <sup>How did I do? [Good](https://app.dosu.dev/response-feedback/0419b46e-965a-4754-adc2-9b948ff2ff37?feedback_type=great_response) | [Irrelevant](https://app.dosu.dev/response-feedback/0419b46e-965a-4754-adc2-9b948ff2ff37?feedback_type=irrelevant_answer) | [Incorrect](https://app.dosu.dev/response-feedback/0419b46e-965a-4754-adc2-9b948ff2ff37?feedback_type=incorrect_sources) | [Verbose](https://app.dosu.dev/response-feedback/0419b46e-965a-4754-adc2-9b948ff2ff37?feedback_type=too_verbose) | [Hallucination](https://app.dosu.dev/response-feedback/0419b46e-965a-4754-adc2-9b948ff2ff37?feedback_type=hallucination) | [Report 🐛](https://app.dosu.dev/response-feedback/0419b46e-965a-4754-adc2-9b948ff2ff37?feedback_type=bug_report) | [Other](https://app.dosu.dev/response-feedback/0419b46e-965a-4754-adc2-9b948ff2ff37?feedback_type=other)</sup> [![Open in Cursor](https://dosu.dev/dosu-in-cursor.svg)](https://cursor.com/link/prompt?text=The%20KeyError%20happens%20because%20the%20code%20in%20%60qa_chunk%60%20accesses%20DataFrame%20rows%20using%20integer%20indices%20%28like%20%60row%5B1%5D%60%29%2C%20but%20when%20a%20CSV%20has%20headers%2C%20pandas%20rows%20are%20indexed%20by%20column%20names%20%28strings%29%2C%20not%20integers.%20This%20causes%20the%20KeyError%20when%20processing%20standard%20CSV%20files%20with%20headers.%20The%20fix%20is%20to%20use%20positional%20indexing%20with%20%60.iloc%60%2C%20for%20example%3A%20%60row.iloc%5Bint%28question_column%29%5D%60%2C%20or%20to%20resolve%20the%20column%20names%20first%20and%20use%20them%20as%20keys%20%28e.g.%2C%20%60row%5Bdf.columns%5Bint%28question_column%29%5D%5D%60%29.%20No%20merged%20pull%20requests%20or%20issues%20currently%20address%20this%20bug%2C%20and%20the%20problem%20appears%20isolated%20to%20%60qa_chunk%60%E2%80%94updating%20the%20code%20as%20described%20will%20resolve%20the%20error%20when%20handling%20headered%20CSVs.%20You%20can%20see%20the%20relevant%20code%20and%20context%20%5Bhere%5D%28https%3A//github.com/langgenius/dify-official-plugins/blob/058d25c9d4d3a5b826ac7cfcf82ef2c7e9e482b8/tools/qa_chunk/tools/qa.py%23L33-L48%29.)&nbsp;[![Join Discord](https://img.shields.io/badge/join-5865F2?logo=discord&logoColor=white&label=)](https://go.dosu.dev/discord-bot)&nbsp;[![Share on X](https://img.shields.io/badge/X-share-black)](https://twitter.com/intent/tweet?text=%40dosu_ai%20helped%20me%20solve%20this%20issue!&url=https%3A//github.com/langgenius/dify-official-plugins/issues/2560)
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: langgenius/dify-official-plugins#965