Skip to main content
CSVToChat is an open-source app that turns static CSV files into a conversation. You upload a CSV, ask questions in natural language, and get back answers and charts produced by Python code that a Together AI model writes and the code interpreter executes.
The CSVToChat interface showing a chat about an uploaded CSV file
This guide walks through the AI core of CSVToChat as it exists in the open-source repository: how the app describes the CSV to the model without flooding its context, how it generates analysis code, and how it runs that code in a sandbox. Each section names the source file it covers and links to it on GitHub, so you can read the guide alongside the real code.
The TypeScript examples use Vercel’s AI SDK v6 and the together-ai Node SDK. They are adapted from the CSVToChat source and updated to current SDK and model versions, so they can differ in small ways from the linked files.

Provide CSV context without flooding the model

Sending an entire CSV file to the model doesn’t scale, as large files will overflow the context window, and even mid-sized ones can crowd out the conversation. Instead, the app’s system prompt gives the model a compact description of the data:
  1. S3 URL: The generated code downloads the full dataset at execution time, so the model never needs the raw rows in context.
  2. Column names: The model needs the schema to write correct code.
  3. Sample rows: A few representative rows guide the analysis.
  4. Instructions: Constraints on code structure and output format.
Generated Python code that downloads the CSV and analyzes it
The generateCodePrompt function in src/lib/prompts.ts assembles these pieces into a system prompt that the model will use to generate the analysis code:

Generate analysis code

When a user asks a question, the model receives their query, the CSV metadata via the system prompt, and the conversation history. It responds with Python code that:
  • Downloads the CSV from the provided S3 URL using pandas.read_csv().
  • Performs the requested analysis using data science libraries.
  • Produces either text results or a single visualization.
The chat route handler in src/app/api/chat/route.ts calls streamText and streams the response to the client so the user sees the code as the model writes it:

Execute code in the sandbox

Model-generated code can contain mistakes, and in an app that accepts arbitrary user data you have to assume it might include malicious content. CSVToChat never runs generated code on its own servers. Instead, the runPython helper in src/lib/coding.ts sends the code to the Together code interpreter, which executes it in an isolated sandbox and returns the outputs:
The outputs array contains stdout and stderr text entries along with rich display outputs. When the generated code renders a chart, the sandbox returns it as a base64-encoded image/png entry that the frontend displays inline.

End-to-end flow

A full interaction runs through these steps:
  1. Upload: The app parses the column names and sample rows, then uploads the file to S3.
  2. Question: The user asks a question, and the app builds the system prompt from the CSV context.
  3. Generation: The model writes Python code, which streams to the frontend in real time.
  4. Execution: The app extracts the code block from the finished response and runs it in the sandbox.
  5. Display: The frontend renders the printed results or chart.
In the app, the client extracts the code block from the streamed message with extractCodeFromText in src/lib/utils.ts, then posts it to the execution route in src/app/api/coding/route.ts. The same flow condensed into one server-side function looks like this:

Choose a model

CSVToChat lets users pick from several Together AI models depending on the complexity of the analysis: See recommended models for current picks, and the serverless model catalog for the full list.

Explore the full app

The production app adds chat history persistence, per-question execution timeouts, and rate limiting on top of the core flow covered here. CSVToChat is open source, so you can read the complete implementation on GitHub or try it at csvtochat.com.