
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:- S3 URL: The generated code downloads the full dataset at execution time, so the model never needs the raw rows in context.
- Column names: The model needs the schema to write correct code.
- Sample rows: A few representative rows guide the analysis.
- Instructions: Constraints on code structure and output format.

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.
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, therunPython 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:
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:- Upload: The app parses the column names and sample rows, then uploads the file to S3.
- Question: The user asks a question, and the app builds the system prompt from the CSV context.
- Generation: The model writes Python code, which streams to the frontend in real time.
- Execution: The app extracts the code block from the finished response and runs it in the sandbox.
- Display: The frontend renders the printed results or chart.
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:- Kimi K2.7 Code (default): Fast, reliable code generation.
- DeepSeek-V4-Pro: Complex analysis tasks that benefit from more reasoning.
- GPT-OSS 120B: Low-cost code generation for shorter questions.
- Llama 3.3 70B Instruct Turbo: Balanced performance and cost.