Back
beginner

AI Fundamentals

Make one model request and inspect what happened

Send a bounded Python API request, keep the credential out of source code, and distinguish request success from answer quality.

Lesson 13 of 31About 25 min with practice

A chat interface hides most of the request. In an application, your code must choose what to send, handle failure, and decide whether the response is usable. Start with a single public-text request that is easy to inspect.

Before you begin: Know how to run a Python file and use a terminal. A provider account with API access is needed only for the live request.

Understand the trip

An API is a programmatic interface to a service. Your program sends a request containing a model identifier and input. The provider authenticates the request, runs the model if the request is accepted, and returns structured data. An SDK is a library that helps your program construct requests and interpret responses.

This lesson uses the official OpenAI Python SDK as one concrete example, reviewed on September 10, 2026. Other providers have different request fields and response objects. Do not copy one provider's message schema into another without checking its documentation.

Prepare an isolated environment

Use Python 3.10 or later, the current minimum documented by this SDK at review time. Create a virtual environment so this lesson's packages do not alter an unrelated project.

bash
python3 -m venv .venv

Activate it with source .venv/bin/activate on macOS or Linux, or .venv\Scripts\Activate.ps1 in Windows PowerShell. Then install and record the dependency:

bash
python -m pip install openai
python -m pip freeze > requirements-lock.txt

The lock record captures what you actually installed; this lesson does not claim an unverified latest version. Obtain an API credential through the provider account and supply it to the process as OPENAI_API_KEY. Set OPENAI_MODEL to a text model identifier available to your account that supports the Responses API. Do not use a made-up placeholder as an actual model name.

Use your terminal's secure secret mechanism or an environment-file loader configured to ignore that file in Git. Never paste a credential into the program, a screenshot, a public repository, or browser-side JavaScript. A ChatGPT subscription and API access are separate product arrangements; check your account before making requests.

Read the request before running it

Save this as first_request.py. This is a complete integration example. It requires the dependency, credential, model access, and network connection above; it can incur provider charges. Its signature was checked against the official SDK, but a paid API call was not executed during this lesson's review.

python
import os
from openai import OpenAI

model = os.environ['OPENAI_MODEL']
with OpenAI(timeout=30.0, max_retries=0) as client:
    response = client.responses.create(
        model=model,
        instructions=(
            'Answer using only the notice. '
            'If a detail is missing, say it is not provided.'
        ),
        input=(
            'Notice: Drawing class, Saturday 10:00, Room 4. '
            'Bring a pencil. Question: What should I bring?'
        ),
        max_output_tokens=300,
    )
    print('Response status:', response.status)
    print('Answer:', response.output_text)
    print('Usage:', response.usage)

Run python first_request.py from the activated environment. The desired answer mentions a pencil. Exact wording and token usage depend on the model. The output limit bounds generation, but some model configurations may need a larger budget to produce a complete answer; inspect the status rather than silently treating an empty or incomplete response as success.

Separate three kinds of failure

A local KeyError for OPENAI_MODEL means the process configuration is missing. An authentication or access error comes from the provider rejecting the credential or requested resource. A timeout or rate-limit response is an operational failure. None of these is an incorrect natural-language answer.

An HTTP-successful response that says “bring paint” is a quality failure. It needs an evidence check, not a network retry. This distinction will matter when you build automatic retries: repeating every failure can increase cost without improving correctness. Here retries are disabled so the first experiment is easy to observe.

Explore a missing answer

Change only the question to “Who is teaching the class?” Predict the appropriate response. Keep the notice unchanged. Then inspect the answer and status.

Judge the result

The notice does not identify a teacher. A useful answer says that the name is not provided. An invented name is a grounding failure even if the API status is successful. Record the input and model identifier when reporting that failure, while keeping credentials out of logs.

Next, we will refine prompting through controlled comparisons. The API is a way to repeat those comparisons, not a substitute for defining what good output means.

Reference

The official OpenAI Python SDK documents OpenAI, responses.create, output_text, timeouts, and retries. Check its README and API reference when updating your installed version.

Continue to the next lesson.

Practice for this lesson

Read a request, then separate three kinds of failure

Make one model call and tell configuration, transport, and content failures apart.

About 10 min40 points3 checks and one written task
Loading your lesson progress...