Skip to main content

o3 model Python quickstart using the OpenAI API

Get set up and running the new o3hmini models in Python using the OpenAI API quickly and easily in this tutorial.
Created on January 31|Last edited on January 31
Getting started with OpenAI's new o3 models via the API is straightforward, offering flexibility beyond what’s available in the ChatGPT interface or GPT-4o. This quickstart guide will have you up and running in about 5 minutes.
If you want to jump right in, there's also a Colab to go with it:

Here's what we're covering in this tutorial:

Table Of Contents



If you're just getting started and don't yet have your machine set up to run Python, we've created a quick tutorial here that will have you up and running in just a few minutes.

Additionally, if you need a walkthrough on getting the OpenAI API, you'll find that here.
💡
Let's start with why the core question:

What is o3 and how does it differ from other OpenAI models?

OpenAI o3-mini is the latest reasoning model from the folks at OpenAI's and excels at STEM applications (science, technology, engineering and mathematics). This model builds on the capabilities of o1-mini, but brings superior speed, accuracy, and reasoning ability while maintaining low latency and cost-effectiveness.
Source

The key upgrades from o1

  • Performance: o3-mini matches exceeds o1-mini in reasoning tasks, with expert evaluations showing a 39% reduction in major errors.
  • Developer features: It introduces function calling, structured outputs, and developer messages to a mini reasoning model, making it production-ready from launch.
  • Reasoning effort options: Perhaps one of the best features (IMO) that that like o1, users can optimize between low, medium, and high reasoning effort to balance speed and accuracy.
  • Availability: o3-mini is available in the Chat Completions API, Assistants API, and Batch API, with access for Tier 3-5 API users - as opposed to only tier 5 for o1.
  • ChatGPT integration: o3-mini replaces o1-mini in ChatGPT for Plus, Team, and Pro users, increasing message rate limits significantly.
  • Pricing: o3-mini is priced significantly lower than o1.
    Source
A notable miss is that unlike o1, o3-mini does not support visual reasoning.
Overall, it's a strong step forward giving users greater access—and at a cheaper rate.

W&B Weave

If you'd like to build with o3, you'll need tools and W&B Weave simplifies the process of tracking and analyzing model outputs.
To get started, you'll import and initialize it with your project name. We recommend using the @weave.op decorator, which allows Weave to automatically log a function’s inputs and outputs. This makes tracking your model interactions seamless. Once your script runs, you’ll find detailed visualizations and traces in the Weave dashboard, making debugging and model refinement easier. Our docs are a good place to get started.

o3 models in Python quickstart

This tutorial assumes you're working in a Jupyter notebook but of course, the code will work in other applications. We're going to be working with the o1-mini model. And without further ado, let's jump right in.
Note: Okay. A little further ado, sorry. When you execute the cells below, you'll notice an asterisk ([*]) appear between the brackets [ ]. This indicates that the cell is running, and you’ll need to wait until the asterisk turns into a number before proceeding.
💡

Step 1: The OpenAI o1-mini API key

First, set up your o3-preview API key:
%env OPENAI_API_KEY=KEY
(You'll want to replace KEY with your OpenAI API key.)
When you run the cell you'll get your key back as the output. This confirms that it's been successfully entered as an environment variable.


Step 2: Installing OpenAI and W&B Weave

To get started with the o1-preview model, all you need to install is OpenAI. However, we’ll also show you how to simplify reviewing multiple outputs using W&B Weave, which makes the process much more efficient.
This is a great time to sign up for Weights & Biases, if you haven't already. This will save you from interrupting your workflow later.
The code to do this is:
!pip install openai weave
Run the Jupyter cell after entering this code.

Now you've installed it, we still need to import it for use.
If you're new to Python, think of it this way: Installing a library is like uploading an image to a server—you’ve got the resource. Importing is like embedding that image on a webpage, making it accessible in your code.
💡

Step 3: Import libraries and pass the OpenAI API key

In this block, we'll import the OpenAI and W&B Weave libraries we've already installed. We're also importing os and re to give us access to regular expressions and os to fetch environment variables (in this case, the OpenAI API).
import os
import weave
from openai import OpenAI
import re

# Initialize Weave and W&B
weave.init('o3-mini')

# Initialize OpenAI client
client = OpenAI(api_key=os.getenv('OPENAI_API_KEY'))
In the line: weave.init('o3-mini')
You can change o3-mini to whatever you would like. This is going to be the project name inside Weights & Biases.

Step 4: Configuring your o3 prompt

The following code will generate inputs you can fill in as they run. They will ask for:
  • Who the model should act as. This ties to the assistant role, and is used to define traits the o3 model should take on.
  • The prompt of what they should do. This ties to the user role, and is used to define what's expected from them. This is akin to what you would enter into the ChatGPT interface.
o3_assistant_prompt = "You are a " + input("Who should I be, as I answer your prompt?")
o3_user_prompt = input("What prompt do you want me to complete?")
o3_prompt = o3_assistant_prompt, o3_user_prompt
print(o3_prompt)

Step 5: Generating content with the o3 models

Hopefully, this hasn't been too painful because now we're at the fun part.
Now that we’ve set everything up, it’s time to generate content.
The function below requests a response from the o3-mini model based on the provided prompt and logs the interaction in Weave:
The code defaults to using a high reasoning effort, since I'm moving the content burden onto GPT-4, but it can be adjusted to low or medium.
@weave.op()
def generate_content(o3_assistant_prompt: str, o3_user_prompt: str) -> str:
prompt = f"{o3_assistant_prompt}\nUser request: {o3_user_prompt}"
messages = [
{"role": "assistant", "content": o3_assistant_prompt},
{"role": "user", "content": o3_user_prompt}
]
response = client.chat.completions.create(
model="o3-mini",
messages=messages,
reasoning_effort="high" # This can be set to low, medium or high.
)
return response.choices[0].message.content

# Generate content using the provided assistant and user prompts
content = generate_content(o3_assistant_prompt, o3_user_prompt)

# Print the generated response
print(content)
When run, these appear as:


Viewing your o3 model output in W&B Weave

This proved especially important while writing this article.
When writing the quickstart for the o1 model, the model would send reasoning steps back through the API. They weren't necessarily the literal steps, but we could ask the o1 models to send steps back. This article was going to initially use o3 to generate the steps, and then GPT-4o for the content, but looking at the traces in Weave, we can see why this wouldn't produce the effect we'd want:


Worse, it will still try to produce the content requested, you simply may not know it's not based on reasoning.
When run through o1 only, we can see the trace:


Notwithstanding that it went a different direction than I intended with "o1" it did pretty well.
Hopefully you've found this o3 model quickstart tutorial helpful. Please drop in the comments what you end up working on!

Iterate on AI agents and models faster. Try Weights & Biases today.