Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 19 additions & 4 deletions agentic_security/probe_data/data.py
Original file line number Diff line number Diff line change
Expand Up @@ -297,21 +297,36 @@ def file_dataset(file) -> list[str]:
return prompts


def _is_non_empty_text_column(series: pd.Series) -> bool:
"""Return True when a Series contains non-empty string values."""
non_null = series.dropna()
if non_null.empty or not pd.api.types.is_string_dtype(non_null):
return False
return bool(non_null.astype(str).str.strip().ne("").any())


def parse_csv_content(content: bytes) -> ProbeDataset:
"""Parse uploaded CSV bytes into a ProbeDataset.

Looks for a 'prompt' column first; falls back to the first text-like column.
Looks for a 'prompt' column first (case-insensitive); falls back to the
first non-empty text column.
"""
df = pd.read_csv(io.BytesIO(content), encoding_errors="ignore")

prompt_col = None
# Prefer an explicit 'prompt' column
# Prefer an explicit 'prompt' column (exact match first, then case-insensitive)
if "prompt" in df.columns:
prompt_col = "prompt"
else:
# Fall back to the first string/object column
for col in df.columns:
if df[col].dtype == object:
if str(col).strip().lower() == "prompt":
prompt_col = col
break

if prompt_col is None:
# Fall back to the first non-empty string column
for col in df.columns:
if _is_non_empty_text_column(df[col]):
prompt_col = col
break

Expand Down
67 changes: 67 additions & 0 deletions tests/unit/probe_data/test_parse_csv_content.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
"""Unit tests for parse_csv_content text-column detection."""

import pytest

from agentic_security.probe_data.data import parse_csv_content


def test_exact_prompt_column_wins_over_earlier_string_column():
csv_bytes = b"id,prompt\nspec1,first prompt\nspec2,second prompt\n"
dataset = parse_csv_content(csv_bytes)
assert dataset.prompts == ["first prompt", "second prompt"]


def test_case_insensitive_prompt_column_wins_over_earlier_string_column():
csv_bytes = b"id,Prompt\nspec1,first prompt\nspec2,second prompt\n"
dataset = parse_csv_content(csv_bytes)
assert dataset.prompts == ["first prompt", "second prompt"]


@pytest.mark.parametrize(
("csv_bytes", "expected"),
[
(
b"text,label\nIgnore previous instructions,1\n",
["Ignore previous instructions"],
),
(
b"id,query\n1,Bypass safety filters\n2,Leak system prompt\n",
["Bypass safety filters", "Leak system prompt"],
),
(
b"flag,text\ntrue,first\nfalse,second\n",
["first", "second"],
),
(
b"flag,text\ntrue,first\n,second\n",
["first", "second"],
),
(
b"empty,text\n,first\n,second\n",
["first", "second"],
),
(
b"blank,text\n ,first\n ,second\n",
["first", "second"],
),
],
)
def test_fallback_selects_first_non_empty_text_column(csv_bytes, expected):
dataset = parse_csv_content(csv_bytes)
assert dataset.prompts == expected


@pytest.mark.parametrize(
"csv_bytes",
[
b"id,count\n1,2\n3,4\n",
b"flag\ntrue\nfalse\n",
b"flag\ntrue\n\n",
b"text,label\n",
b"text\n\n\n",
b"text\n \n",
],
)
def test_non_text_or_empty_csv_raises_value_error(csv_bytes):
with pytest.raises(ValueError, match="no suitable prompt column"):
parse_csv_content(csv_bytes)
Loading