How to enable code interpreter in OpenAI assistant
Quick answer
To enable the code interpreter in an OpenAI assistant, you must specify the functions parameter with a code execution function in your chat completion request using the OpenAI SDK. This allows the assistant to run code snippets securely during the conversation.
PREREQUISITES
Python 3.8+OpenAI API key (free tier works)pip install openai>=1.0
Setup
Install the official openai Python SDK version 1.0 or higher and set your API key as an environment variable.
- Install SDK:
pip install openai>=1.0 - Set environment variable:
export OPENAI_API_KEY='your_api_key'(Linux/macOS) orsetx OPENAI_API_KEY "your_api_key"(Windows)
pip install openai>=1.0 Step by step
Use the OpenAI SDK to create a chat completion request that includes a functions parameter defining a code interpreter function. This enables the assistant to execute code snippets during the chat.
import os
from openai import OpenAI
client = OpenAI(api_key=os.environ["OPENAI_API_KEY"])
# Define a function schema for code execution
functions = [
{
"name": "code_interpreter",
"description": "Executes Python code snippets securely.",
"parameters": {
"type": "object",
"properties": {
"code": {
"type": "string",
"description": "Python code to execute"
}
},
"required": ["code"]
}
}
]
messages = [
{"role": "user", "content": "Calculate the factorial of 5 using Python code."}
]
response = client.chat.completions.create(
model="gpt-4o-mini",
messages=messages,
functions=functions,
function_call="auto" # Let the model decide when to call the function
)
print("Assistant response:")
print(response.choices[0].message.content)
# If the model calls the function, you would then execute the code securely on your side
# and send the result back to the model in a follow-up message (not shown here). output
Assistant response:
{"function_call": {"name": "code_interpreter", "arguments": "{\"code\": \"import math\nfactorial = math.factorial(5)\nfactorial\"}"}} Common variations
You can customize the code interpreter by changing the functions schema or use different models like gpt-4o-mini for faster responses. Async calls and streaming are also supported by the OpenAI SDK.
import asyncio
import os
from openai import OpenAI
client = OpenAI(api_key=os.environ["OPENAI_API_KEY"])
async def async_code_interpreter():
response = await client.chat.completions.acreate(
model="gpt-4o-mini",
messages=[{"role": "user", "content": "Run Python code to add 2 + 2."}],
functions=[
{
"name": "code_interpreter",
"description": "Executes Python code snippets.",
"parameters": {
"type": "object",
"properties": {
"code": {"type": "string"}
},
"required": ["code"]
}
}
],
function_call="auto"
)
print(response.choices[0].message.content)
asyncio.run(async_code_interpreter()) output
{"function_call": {"name": "code_interpreter", "arguments": "{\"code\": \"2 + 2\"}"}} Troubleshooting
- If the assistant does not execute code, ensure you have included the
functionsparameter with the correct schema. - Check that your API key is set correctly in
os.environ["OPENAI_API_KEY"]. - Function calling requires models that support it, like
gpt-4o-miniorgpt-4o-mini.
Key Takeaways
- Use the functions parameter in client.chat.completions.create() to enable code interpreter functionality.
- Define a clear function schema describing the code execution interface for the assistant.
- Models like gpt-4o-mini support function calling required for code interpretation.
- Always securely execute code on your backend after receiving the function call from the assistant.
- Set your API key in environment variables and use the official openai SDK v1+.