Using Code Interpreter with CSV Analysis
In this lesson, you'll learn how to feed raw CSV data to GPT-4o and analyze it using the code_interpreter tool.
Prerequisites
Install the required packages:
pip install openai requests
The Code
import requests
from openai import OpenAI
client = OpenAI()
# Download entire CSV as text
url = "https://raw.githubusercontent.com/wlodarzmar/csvToSqlTable/refs/heads/master/100%20Sales%20Records.csv"
print(f"Downloading CSV from {url}...")
csv_text = requests.get(url).text
print(f"Downloaded {len(csv_text)} characters")
# Compose full prompt with entire CSV embedded
input_text = f"""
You are a data analyst. Here is the complete CSV data of sales records:
\"\"\"
{csv_text}
\"\"\"
Please write and run Python code to:
- Parse this CSV data
- Calculate total revenue (sum of 'Total Revenue' column)
- Compute average order value (average of 'Total Revenue')
- Identify the top 3 regions by total revenue
- Provide any other useful insights you find
Show your work using Python code and print the results clearly.
"""
response = client.responses.create(
model="gpt-4o-mini",
tools=[
{
"type": "code_interpreter",
"container": {"type": "auto"}
}
],
instructions="You are a helpful analyst who can write and execute Python code.",
input=input_text,
)
print("\n=== GPT Code Interpreter Output ===\n")
print(response.output_text)
Explanation
requests.get(url).text: Downloads raw CSV file.- Embeds CSV directly into the prompt.
tools=[{"type": "code_interpreter"}]: Enables Python execution.- Model responds with both code and analysis in real-time.
Use Case
Perfect for:
- Data analysis & visualization
- Working with CSV, JSON, and other raw data formats
- Executable data pipelines powered by AI