Skip to main content
Read a file from disk and parse it into the Data structure expected by @graphysdk/core. The format is auto-detected from the file extension.

fromFile

import { fromFile } from '@graphysdk/data-import-utils/file';

const data = await fromFile('sales.csv');
const data = await fromFile('report.xlsx', { sheet: 'Revenue' });

Signature

function fromFile(
  filePath: string,
  options?: FileParseOptions
): Promise<Data>
FileParseOptions combines spreadsheet options with the hasHeader flag from delimited parsing.

Options

filePath
string
required
Path to the file. The extension determines the format.
options.hasHeader
boolean
default:"true"
Whether the first row contains column headers (CSV/TSV only). When false, columns are auto-named Column 1, Column 2, etc.
options.sheet
string | number
default:"0"
Sheet to parse (spreadsheets only). Pass a sheet name or 0-based index.
options.locale
VizLocale
default:"EN_US"
Locale for number parsing. Determines thousand/decimal separator conventions.
options.maxFileSize
number
default:"5"
Maximum allowed input size in megabytes. Throws an error if the input exceeds this limit.
options.maxRows
number
default:"100000"
Maximum number of data rows to process (spreadsheets only).
options.maxCells
number
default:"5000000"
Maximum total cells to process (spreadsheets only).

Supported Extensions

ExtensionFormat
.csvCSV
.tsv, .tabTSV
.xlsxXLSX
.xlsXLS
.odsODS
Unsupported extensions throw an error listing the supported formats.

Examples

With the AI SDK

import { fromFile } from '@graphysdk/data-import-utils/file';
import { GraphyAiSdk } from '@graphysdk/agents-sdk';

const ai = new GraphyAiSdk({
  apiKey: process.env.GRAPHY_API_KEY,
  baseUrl: 'https://agents.graphy.dev',
});

const data = await fromFile('quarterly-sales.csv');

const result = await ai.generateGraph({
  config: { data },
  userPrompt: 'line chart showing quarterly trends',
});

Error Handling

try {
  const data = await fromFile('data.pdf');
} catch (error) {
  // Error: Unsupported file extension ".pdf". Supported: .csv, .tsv, .tab, .xlsx, .xls, .ods
}
fromFile uses Node.js fs.readFile under the hood and is not available in the browser. For browser usage, use the format-specific parsers (fromCSV, fromXLSX, etc.) after reading the file with the File API.