Code interpreter supported file types
Quick answer
The
OpenAI code interpreter supports common file types including .py (Python scripts), .csv (comma-separated values), .json, .txt, and .xlsx (Excel spreadsheets). It can also handle image files like .png and .jpg for analysis within supported contexts.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.
- Run
pip install openaito install the SDK. - Set your API key in your shell:
export OPENAI_API_KEY='your_api_key_here'(Linux/macOS) orsetx OPENAI_API_KEY "your_api_key_here"(Windows).
pip install openai Step by step
Use the OpenAI client to upload and process supported file types with the code interpreter. Below is a sample to demonstrate reading a .csv file and running code on it.
import os
from openai import OpenAI
client = OpenAI(api_key=os.environ["OPENAI_API_KEY"])
# Example: Upload a CSV file and run code interpreter on it
file_path = "data.csv"
# Upload the file
with open(file_path, "rb") as f:
upload_response = client.files.create(
file=f,
purpose="code_interpreter"
)
file_id = upload_response.id
# Create a chat completion with code interpreter model
response = client.chat.completions.create(
model="gpt-4o-mini",
messages=[
{"role": "user", "content": f"Analyze the data in the uploaded file with id {file_id}."}
]
)
print(response.choices[0].message.content) output
Summary of data.csv contents and analysis results printed here.
Common variations
You can use other supported file types such as .py for scripts, .json for structured data, or .xlsx for Excel files. The code interpreter can also process images like .png or .jpg for tasks like OCR or image analysis when supported by the model.
For asynchronous usage, use async Python with the OpenAI SDK's async client methods.
import os
import asyncio
from openai import OpenAI
async def main():
client = OpenAI(api_key=os.environ["OPENAI_API_KEY"])
# Async example for chat completion
response = await client.chat.completions.acreate(
model="gpt-4o-mini",
messages=[{"role": "user", "content": "Run code on the uploaded Python script."}]
)
print(response.choices[0].message.content)
asyncio.run(main()) output
Output from async code interpreter execution.
Troubleshooting
- If you get an error uploading a file, verify the file path and that the file type is supported.
- Ensure your API key is correctly set in the environment variable
OPENAI_API_KEY. - For large files, check size limits and consider splitting data.
- If the code interpreter does not recognize the file, confirm the
purposeparameter is set tocode_interpreterduring upload.
Key Takeaways
- The OpenAI code interpreter supports .py, .csv, .json, .txt, .xlsx, and common image files like .png and .jpg.
- Upload files with purpose='code_interpreter' to enable processing by the code interpreter model.
- Use the OpenAI Python SDK v1+ with environment variable API keys for secure and reliable integration.