import getpass
from openai import OpenAI
api_key = getpass.getpass("OpenAI API Key: ")
client = OpenAI(api_key=api_key)
response = client.responses.create(
model="gpt-4o-mini",
instructions="You are a coding assistant that talks like a pirate.",
input="How do I check if a Python object is an instance of a class?",
)
print(response.output[0].content[0].text)The openai package is the official Python library for OpenAI's API. It
sends your request to a model such as GPT-4o mini and turns the reply
into Python objects. People use it to build chatbots, summarize or
classify text, pull structured data out of documents and add AI features
to their own scripts. Every request needs an API key from your OpenAI
account. This page is an online OpenAI compiler: the code runs in your
browser, so you can try it without installing anything.
Run the example first, then paste any snippet below into a new cell to try it.
getpass.getpass("OpenAI API Key: ") asks for your key when the code
runs, so the key never appears in the code. On this page the question
opens a browser dialog, as the getpass example
explains. OpenAI(api_key=api_key) creates a client that sends the key
with each request, from your browser straight to
https://api.openai.com/v1/. client.responses.create() calls the
Responses API: model picks the model, instructions adds a system
message that sets the tone, here a pirate's, and input is the
question. The reply is a Response object. Its output list holds what
the model produced, the first item is a message, and
output[0].content[0].text is that message's text.
previous_response_id links a request to an earlier response, so the
model also sees the first question and answer. output_text is a
shortcut that joins the text of every message in output:
import getpass
from openai import OpenAI
client = OpenAI(api_key=getpass.getpass("OpenAI API Key: "))
first = client.responses.create(
model="gpt-4o-mini",
input="Suggest a name for a Python library that parses dates.",
)
print(first.output_text)
second = client.responses.create(
model="gpt-4o-mini",
previous_response_id=first.id,
input="Make it shorter.",
)
print(second.output_text)
Instructions are not carried over from the earlier response, so pass
instructions again on each call that needs them.
client.responses.parse() takes a Pydantic model as text_format,
sends its JSON schema with the request and turns the reply into an
instance of that class, available as output_parsed:
import getpass
from openai import OpenAI
from pydantic import BaseModel
class Event(BaseModel):
name: str
day: str
people: list[str]
client = OpenAI(api_key=getpass.getpass("OpenAI API Key: "))
response = client.responses.parse(
model="gpt-4o-mini",
instructions="Extract the event.",
input="Alice and Bob are going to a science fair on Friday.",
text_format=Event,
)
event = response.output_parsed
print(event.name, event.day, event.people)
Much existing code uses the Chat Completions API, which the library also
supports. It takes a list of messages, each with a role and
content, and the reply text is in choices[0].message.content:
import getpass
from openai import OpenAI
client = OpenAI(api_key=getpass.getpass("OpenAI API Key: "))
completion = client.chat.completions.create(
model="gpt-4o-mini",
messages=[
{"role": "system", "content": "Answer in one sentence."},
{"role": "user", "content": "What is a Python decorator?"},
],
)
print(completion.choices[0].message.content)
getpass, as these examples do. OpenAI() with no api_key reads
the OPENAI_API_KEY environment variable instead.output can also hold tool calls and reasoning items, so output[0]
is not always a message. output_text collects only message text,
which makes it the safer choice.openai.AuthenticationError
for status 401 and openai.RateLimitError for status 429. The client
retries a 429 twice before it raises. Pass max_retries=0 to
OpenAI() to fail at once.