Implementation:Promptfoo Promptfoo CSV Parser
| Knowledge Sources | |
|---|---|
| Domains | Data_Parsing, Testing |
| Last Updated | 2026-02-14 07:45 GMT |
Overview
Concrete tool for parsing CSV-based evaluation files into structured test cases and assertions, shared by both frontend and backend.
Description
The CSV_Parser module converts CSV row data into promptfoo TestCase and Assertion objects. It handles parsing assertion strings (e.g., contains:hello, javascript:fn(), similar(0.9):expected) into structured Assertion objects with proper types, thresholds, and values. It supports all base assertion types defined in the schema, including negation (not-), threshold values, and various shorthand syntaxes.
Usage
Import this module when loading test cases from CSV files or Google Sheets. It is used internally by the test case reader to handle CSV-format inputs.
Code Reference
Source Location
- Repository: Promptfoo_Promptfoo
- File: src/csv.ts
- Lines: 1-322
Signature
export function assertionFromString(expected: string): Assertion
export function testCaseFromCsvRow(row: CsvRow): TestCase
export function testCasesFromCsv(
csvInput: string | object[],
vars?: string[]
): TestCase[]
Import
import { assertionFromString, testCaseFromCsvRow, testCasesFromCsv } from './csv';
I/O Contract
Inputs
| Name | Type | Required | Description |
|---|---|---|---|
| expected | string | Yes | Assertion string (e.g., "contains:hello", "similar(0.9):text") |
| row | CsvRow | Yes | Single CSV row as key-value object |
| csvInput | string or object[] | Yes | Raw CSV string or array of row objects |
| vars | string[] | No | Optional list of variable column names |
Outputs
| Name | Type | Description |
|---|---|---|
| Assertion | Assertion | Structured assertion with type, value, threshold |
| TestCase | TestCase | Test case with vars, assertions, and metadata |
| TestCase[] | TestCase[] | Array of test cases parsed from CSV |
Usage Examples
import { assertionFromString, testCasesFromCsv } from './csv';
// Parse a single assertion string
const assertion = assertionFromString('similar(0.9):expected output');
// Result: { type: 'similar', value: 'expected output', threshold: 0.9 }
// Parse CSV rows into test cases
const testCases = testCasesFromCsv([
{ question: 'What is AI?', __expected: 'contains:artificial intelligence' },
{ question: 'Hello', __expected: 'icontains:hi' },
]);