Skip to article frontmatterSkip to article content
Site not loading correctly?

This may be due to an incorrect BASE_URL configuration. See the MyST Documentation for reference.

Calling the OpenAI API

University of Kansas School of Business

In this notebook we demonstrate how to make a basic API call to OpenAI’s GPT models. OpenAI provides one of the most widely used commercial LLM APIs, offering models such as GPT-4 and GPT-4.1. You will learn how to authenticate with an API key, construct a request, and interpret the response object.

Setting Up the Client

We start by importing the OpenAI Python SDK and configuring the API key. The key is retrieved from the environment so that credentials are never hard-coded into the notebook. Once the client object is created, it manages authentication and connection pooling for every subsequent request.

import os
from google.colab import auth, userdata
os.environ["OPENAI_API_KEY"] = userdata.get("OPENAI_API_KEY")

from openai import OpenAI
client = OpenAI()

Sending a Chat Completion Request

Here we call the Responses API with the gpt-4.1 model. The request includes a system message (which can steer the model’s persona) and a user message containing our actual question. Parameters like temperature and max_output_tokens let us control creativity and response length.



response = client.responses.create(
  model="gpt-4.1",
  input=[
    {
      "role": "system",
      "content": [
        {
          "type": "input_text",
          "text": ""
        }
      ]
    },
    {
      "role": "user",
      "content": [
        {
          "type": "input_text",
          "text": "Explain RAG in simple words"
        }
      ]
    }
  ],
  text={
    "format": {
      "type": "text"
    }
  },
  reasoning={},
  tools=[],
  temperature=1,
  max_output_tokens=2048,
  top_p=1,
  store=True,
  include=["web_search_call.action.sources"]
)

Inspecting the Full Response

The API returns a rich JSON object that includes token usage statistics, model metadata, and the generated text. Dumping it with json.dumps helps us understand the structure and diagnose any issues with our request.

import json
print(json.dumps(response.model_dump(), indent=2))

Extracting the Generated Text

In most applications we only need the assistant’s message text. The response.output attribute gives us direct access to the list of output messages without parsing raw JSON.

print(response.output)

Key takeaways

  • The OpenAI SDK authenticates via the OPENAI_API_KEY environment variable (or a Colab secret) and exposes a client object that handles every request.

  • The Responses API accepts a list of role-tagged messages (system, user) and returns a structured object with the generated text plus metadata.

  • Generation parameters like temperature, top_p, and max_output_tokens let you dial creativity and cap response length.

  • Inspecting response.model_dump() reveals token usage and model info, while response.output gives direct access to the assistant’s message for production use.


Run the code

To run this notebook, copy the URL below into your browser’s address bar. The link opens the notebook directly in Google Colab. (If your PDF viewer makes the URL clickable and lands on a broken page, copy the full text manually -- the viewer may have truncated the link at a line break.)

Estimated run time: ~2 minutes (requires API key)

https://colab.research.google.com/github/KarAnalytics/code_demos/blob/main/OpenAI_API_call.ipynb