Python / Python Modern Generative AI and Agents Interview Questions
What is the OpenAI Assistants API and how does it differ from the Chat Completions API?
The Assistants API (part of OpenAI's platform) provides a higher-level abstraction for building AI agents with persistent conversation threads, built-in tool use, and file handling — without managing state manually. Key concepts: an Assistant holds configuration (model, system prompt, tools); a Thread maintains conversation history automatically; a Run is an invocation of the assistant on a thread.
Unlike Chat Completions (stateless — you manage the message list), the Assistants API stores threads server-side. The built-in tools include code_interpreter (executes Python in a sandboxed environment), file_search (built-in RAG over uploaded files), and function calling. This makes it well-suited for multi-turn agentic workflows where you want OpenAI to manage state and tool execution loops.
from openai import OpenAI
import time
client = OpenAI()
# ── 1. Create an Assistant (once; reuse by ID)
assistant = client.beta.assistants.create(
name='Data Analyst',
instructions='You are a data analyst. Write and run Python code to answer questions.',
model='gpt-4o',
tools=[{'type': 'code_interpreter'}],
)
# ── 2. Create a Thread (conversation session)
thread = client.beta.threads.create()
# ── 3. Add a user message to the thread
client.beta.threads.messages.create(
thread_id=thread.id,
role='user',
content='Calculate the mean and standard deviation of [4, 8, 15, 16, 23, 42]',
)
# ── 4. Run the assistant
run = client.beta.threads.runs.create(
thread_id=thread.id,
assistant_id=assistant.id,
)
# ── 5. Poll for completion
while run.status not in ('completed', 'failed'):
time.sleep(1)
run = client.beta.threads.runs.retrieve(thread_id=thread.id, run_id=run.id)
# ── 6. Retrieve the latest message
messages = client.beta.threads.messages.list(thread_id=thread.id)
print(messages.data[0].content[0].text.value)
# 'Mean: 18.0, Standard deviation: 13.29...'
Invest now in Acorns!!! 🚀
Join Acorns and get your $5 bonus!
Acorns is a micro-investing app that automatically invests your "spare change" from daily purchases into diversified, expert-built portfolios of ETFs. It is designed for beginners, allowing you to start investing with as little as $5. The service automates saving and investing. Disclosure: I may receive a referral bonus.
Invest now!!! Get Free equity stock (US, UK only)!
Use Robinhood app to invest in stocks. It is safe and secure. Use the Referral link to claim your free stock when you sign up!.
The Robinhood app makes it easy to trade stocks, crypto and more.
Webull! Receive free stock by signing up using the link: Webull signup.
More Related questions...
