OpenAI Assistant API with Code Interpreter

The tutorial explains usage of Assistant API — Code Interpreter.

Teemu Maatta


Photo by Safar Safarov on Unsplash


Assistant API enables developers to create complex workflows using: functions, code interpreter and knowledge retrieval. In this tutorial, I will explain usage of them in concrete manner, but what makes them useful?

Assistant API enables GPT-4 model to decide automatically when to retrieve knowledge, call function or use code interpreter. The automatization simplifies design of Autonomous Agents workflows.

Code interpreter

I will start by importing the required libraries and retrieve the OpenAI API key.

!pip install --upgrade openai
import os
import time
from openai import OpenAI
client = OpenAI(

I next define parameters in advance, which I will be using. These variables will make more sense later in this tutorial, but you can modify them for your use case.

assistant_name = "Power Point Generator"
output_file_name = "Presentation.pptx"
assistant_instruction = r"Generate {} file, always. You are subject-matter expert in the topic and professional in creating PowerPoints.. Betweem 1-5 slides. Background, colors, fonts and styling must be modern and easy to read. Make content engaging. Make the file id available to download.".format(output_file_name)
prompt_user = "Make a presentation for runner practicing for a half-marathon with an aim for a personal record. Make a presentaton with useful insights, training plan for different levels and some tips before, during and after the training period. Give insights."

I will create next the assistant.

assistant = client.beta.assistants.create(
tools=[{"type": "retrieval"},{"type": "code_interpreter"}],

I will create then the thread within the assistant, which makes it possible to create messages within the thread.

thread = client.beta.threads.create()

message = client.beta.threads.messages.create(,

I can now run the thread to generate the respond by the assistant api.

run = client.beta.threads.runs.create(,

Once the response is available, I can retrieve it.

timeout = 180
interval_time = 5
time_taken = 0
while time_taken < timeout:
run = client.beta.threads.runs.retrieve(,

if run.status == 'completed':
time_taken += interval_time

# Alternatively solution is to run below simpler code and ensure run.status printed is 'completed', which can take up to 2 minutes:
#run = client.beta.threads.runs.retrieve(
# print(run.status)

I then pull the required information about the file id and filepath.

messages = client.beta.threads.messages.list(

file_path =[0].content[0].text.annotations[0].file_path.file_id

file_name = client.files.with_raw_response.retrieve_content(file_path)

I can then save the file locally.

with open(output_file_name, "wb") as file:  

I finally delete the file and the assistant. I remind, that the Assistant API pricing is different from the other APIs, so I recommend not to leave the Assistants or files running longer periods.



The tutorial explained usage of the Code Interpreter within the Assistant API. I created with the Code Interpreter a Powerpoint presentation by giving it a topic and I can then retrieve this file to the local file system.

Please make sure to familiarize to the Assistant API pricing before using it.

This article is in the series: Open Knowledge: AI.

The aim is to share at least 10% of my articles without the Medium paywall for free to everybody.


[1] Github. Teemu Maatta.



Teemu Maatta

Author (+200k views) in Artificial General Intelligence. Autonomous Agents. Robotics. Madrid.