Home/Tools/@tpmjs/tools-csv-parse

csvParseTool

@tpmjs/tools-csv-parse

Parse CSV text into an array of objects. Automatically detects headers and infers data types. Returns parsed rows, headers, and metadata. Useful for processing CSV data from files or API responses.

Official
data
v0.2.0
MIT

Interactive Playground

Test @tpmjs/tools-csv-parse (csvParseTool) with AI-powered execution

0/2000 characters

Installation & Usage

Install this tool and use it with the AI SDK

1. Install the package

npm install @tpmjs/tools-csv-parse
pnpm add @tpmjs/tools-csv-parse
yarn add @tpmjs/tools-csv-parse
bun add @tpmjs/tools-csv-parse
deno add npm:@tpmjs/tools-csv-parse

2. Import the tool

import { csvParseTool } from '@tpmjs/tools-csv-parse';

3. Use with AI SDK

import { generateText } from 'ai';
import { openai } from '@ai-sdk/openai';
import { csvParseTool } from '@tpmjs/tools-csv-parse';

const result = await generateText({
  model: openai('gpt-4o'),
  tools: { csvParseTool },
  prompt: 'Your prompt here...',
});

console.log(result.text);

Parameters

Available configuration options

Auto-extracted
csv
Required
Type: string

The CSV text to parse

hasHeaders
Optional
Type: boolean

Whether the first row contains headers (default: true)

Schema extracted: 1/1/2026, 8:18:08 AM

README

@tpmjs/tools-csv-parse

Parse CSV text into array of objects using papaparse.

Installation

npm install @tpmjs/tools-csv-parse
# or
pnpm add @tpmjs/tools-csv-parse
# or
yarn add @tpmjs/tools-csv-parse

Usage

With Vercel AI SDK

import { csvParseTool } from '@tpmjs/tools-csv-parse';
import { generateText } from 'ai';

const result = await generateText({
  model: yourModel,
  tools: {
    csvParse: csvParseTool,
  },
  prompt: 'Parse this CSV data and tell me the average age: name,age\nAlice,25\nBob,30\nCharlie,35',
});

Direct Usage

import { csvParseTool } from '@tpmjs/tools-csv-parse';

const result = await csvParseTool.execute({
  csv: `name,age,city
Alice,25,New York
Bob,30,San Francisco
Charlie,35,Boston`,
  hasHeaders: true,
});

console.log(result);
// {
//   rows: [
//     { name: 'Alice', age: 25, city: 'New York' },
//     { name: 'Bob', age: 30, city: 'San Francisco' },
//     { name: 'Charlie', age: 35, city: 'Boston' }
//   ],
//   headers: ['name', 'age', 'city'],
//   rowCount: 3,
//   metadata: {
//     parsedAt: '2025-01-15T12:00:00.000Z',
//     hasErrors: false,
//     errorCount: 0
//   }
// }

Features

  • Automatic Type Inference - Numbers and booleans are automatically converted
  • Header Detection - Automatically uses first row as headers or generates generic ones
  • Error Handling - Reports parsing errors with row numbers
  • Data Cleaning - Trims whitespace from headers and values
  • Empty Line Handling - Automatically skips empty lines

Parameters

ParameterTypeRequiredDefaultDescription
csvstringYes-The CSV text to parse
hasHeadersbooleanNotrueWhether the first row contains headers

Returns

{
  rows: Record<string, string | number | boolean | null>[];
  headers: string[];
  rowCount: number;
  metadata: {
    parsedAt: string;
    hasErrors: boolean;
    errorCount: number;
    errors?: Array<{
      row: number;
      message: string;
    }>;
  };
}

Examples

CSV with Headers

const result = await csvParseTool.execute({
  csv: 'product,price,inStock\nLaptop,999.99,true\nMouse,29.99,false',
});
// rows: [
//   { product: 'Laptop', price: 999.99, inStock: true },
//   { product: 'Mouse', price: 29.99, inStock: false }
// ]

CSV without Headers

const result = await csvParseTool.execute({
  csv: 'Alice,25,Engineer\nBob,30,Designer',
  hasHeaders: false,
});
// rows: [
//   { col_0: 'Alice', col_1: 25, col_2: 'Engineer' },
//   { col_0: 'Bob', col_1: 30, col_2: 'Designer' }
// ]

Handling Errors

const result = await csvParseTool.execute({
  csv: 'name,age\nAlice,25\nBob,invalid\nCharlie,35',
});
// metadata.hasErrors: true
// metadata.errors: [{ row: 1, message: '...' }]

License

MIT

Statistics

Downloads/month

36

Quality Score

77%

Bundle Size

NPM Keywords

tpmjs
data
csv
parse

Maintainers

thomasdavis(thomasalwyndavis@gmail.com)

Frameworks

vercel-ai