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...'
More Related questions...