How to use code interpreter to analyze data
Quick answer
Use the OpenAI API with the
gpt-4o model and enable the code interpreter feature by sending your data and analysis instructions as messages. The model executes Python code to analyze data and returns results within the chat completion response.PREREQUISITES
Python 3.8+OpenAI API key (free tier works)pip install openai>=1.0
Setup
Install the official openai Python SDK and set your API key as an environment variable.
- Install SDK:
pip install openai - Set environment variable:
export OPENAI_API_KEY='your_api_key'(Linux/macOS) orsetx OPENAI_API_KEY "your_api_key"(Windows)
pip install openai Step by step
Use the gpt-4o model with the OpenAI Python SDK to send your data and analysis instructions. The code interpreter runs Python code internally and returns the analysis results.
import os
from openai import OpenAI
client = OpenAI(api_key=os.environ["OPENAI_API_KEY"])
# Example CSV data as a string
csv_data = """date,sales,region
2026-01-01,100,North
2026-01-02,150,South
2026-01-03,200,East
2026-01-04,130,West
"""
messages = [
{"role": "user", "content": f"Analyze this sales data and provide total sales and average sales per region:\n{csv_data}"}
]
response = client.chat.completions.create(
model="gpt-4o",
messages=messages
)
print("Analysis result:")
print(response.choices[0].message.content) output
Analysis result: Total sales: 580 Average sales per region: - North: 100 - South: 150 - East: 200 - West: 130
Common variations
You can use different models like gpt-4o-mini for faster responses or gpt-4.1 for more advanced analysis. For asynchronous calls, use Python asyncio with the OpenAI SDK. You can also stream partial results for large data analysis.
import os
import asyncio
from openai import OpenAI
async def analyze_data_async():
client = OpenAI(api_key=os.environ["OPENAI_API_KEY"])
csv_data = "date,sales,region\n2026-01-01,100,North\n2026-01-02,150,South"
messages = [{"role": "user", "content": f"Analyze this data:\n{csv_data}"}]
response = await client.chat.completions.acreate(
model="gpt-4o-mini",
messages=messages
)
print("Async analysis result:")
print(response.choices[0].message.content)
asyncio.run(analyze_data_async()) output
Async analysis result: Total sales: 250 Average sales per region: - North: 100 - South: 150
Troubleshooting
- If you get an authentication error, verify your
OPENAI_API_KEYenvironment variable is set correctly. - If the model does not return code execution results, ensure you are using a model that supports code interpreter features like
gpt-4o. - For large datasets, consider sending summarized or chunked data to avoid token limits.
Key Takeaways
- Use the
gpt-4omodel with the OpenAI Python SDK to run code interpreter tasks for data analysis. - Send your data as text in the user message and ask the model to analyze it using Python code.
- Async and streaming calls improve responsiveness for large or complex data analysis.
- Always set your API key in
os.environand verify model compatibility for code execution. - Chunk large datasets to stay within token limits and get accurate analysis results.