@tpmjs/tools-csv-stringify
Convert an array of objects to CSV string format. Optionally specify custom headers. Returns the CSV string, row count, and metadata. Useful for exporting data to CSV files or API responses.
Rows array cannot be empty
Last checked: 1/1/2026, 8:18:20 AM
Test @tpmjs/tools-csv-stringify (csvStringifyTool) with AI-powered execution
0/2000 characters
Install this tool and use it with the AI SDK
npm install @tpmjs/tools-csv-stringifypnpm add @tpmjs/tools-csv-stringifyyarn add @tpmjs/tools-csv-stringifybun add @tpmjs/tools-csv-stringifydeno add npm:@tpmjs/tools-csv-stringifyimport { csvStringifyTool } from '@tpmjs/tools-csv-stringify';import { generateText } from 'ai';
import { openai } from '@ai-sdk/openai';
import { csvStringifyTool } from '@tpmjs/tools-csv-stringify';
const result = await generateText({
model: openai('gpt-4o'),
tools: { csvStringifyTool },
prompt: 'Your prompt here...',
});
console.log(result.text);Available configuration options
rowsarrayArray of objects to convert to CSV
headersarrayOptional array of header names. If not provided, uses object keys from first row.
Schema extracted: 1/1/2026, 8:18:19 AM
Convert array of objects to CSV string using papaparse.
npm install @tpmjs/tools-csv-stringify # or pnpm add @tpmjs/tools-csv-stringify # or yarn add @tpmjs/tools-csv-stringify
import { csvStringifyTool } from '@tpmjs/tools-csv-stringify'; import { generateText } from 'ai'; const result = await generateText({ model: yourModel, tools: { csvStringify: csvStringifyTool, }, prompt: 'Convert this data to CSV format: [{"name":"Alice","age":25},{"name":"Bob","age":30}]', });
import { csvStringifyTool } from '@tpmjs/tools-csv-stringify'; const result = await csvStringifyTool.execute({ rows: [ { name: 'Alice', age: 25, city: 'New York' }, { name: 'Bob', age: 30, city: 'San Francisco' }, { name: 'Charlie', age: 35, city: 'Boston' }, ], }); console.log(result.csv); // name,age,city // Alice,25,New York // Bob,30,San Francisco // Charlie,35,Boston console.log(result); // { // csv: '...', // rowCount: 3, // metadata: { // headers: ['name', 'age', 'city'], // stringifiedAt: '2025-01-15T12:00:00.000Z', // byteSize: 85 // } // }
| Parameter | Type | Required | Default | Description |
|---|---|---|---|---|
rows | Record<string, unknown>[] | Yes | - | Array of objects to convert to CSV |
headers | string[] | No | Object keys from first row | Custom header names |
{ csv: string; rowCount: number; metadata: { headers: string[]; stringifiedAt: string; byteSize: number; }; }
const result = await csvStringifyTool.execute({ rows: [ { product: 'Laptop', price: 999.99, inStock: true }, { product: 'Mouse', price: 29.99, inStock: false }, ], }); console.log(result.csv); // product,price,inStock // Laptop,999.99,true // Mouse,29.99,false
const result = await csvStringifyTool.execute({ rows: [ { name: 'Alice', age: 25, city: 'NYC' }, { name: 'Bob', age: 30, city: 'SF' }, ], headers: ['name', 'city'], // Only include these columns }); console.log(result.csv); // name,city // Alice,NYC // Bob,SF
const result = await csvStringifyTool.execute({ rows: [ { age: 25, name: 'Alice', city: 'NYC' }, { age: 30, name: 'Bob', city: 'SF' }, ], headers: ['name', 'age', 'city'], // Specify order }); console.log(result.csv); // name,age,city // Alice,25,NYC // Bob,30,SF
const result = await csvStringifyTool.execute({ rows: [ { name: 'Alice, Jr.', message: 'Hello "World"' }, { name: 'Bob\nSmith', message: 'Line\nBreak' }, ], }); // Properly escapes commas, quotes, and newlines console.log(result.csv); // name,message // "Alice, Jr.","Hello ""World""" // "Bob\nSmith","Line\nBreak"
import { writeFile } from 'fs/promises'; const result = await csvStringifyTool.execute({ rows: [...], }); await writeFile('output.csv', result.csv, 'utf-8'); console.log(`Wrote ${result.metadata.byteSize} bytes to output.csv`);
try { const result = await csvStringifyTool.execute({ rows: [], }); } catch (error) { console.error(error.message); // "Rows array cannot be empty" } try { const result = await csvStringifyTool.execute({ rows: ['not', 'objects'], // Invalid }); } catch (error) { console.error(error.message); // "All rows must be objects" }
MIT
Downloads/month
0
Quality Score