GENERATIVE AI • LESSON 5

Using an AI API with Python

Now we need a convenient way for Python to communicate with the API.

PART 1

Python SDK — What Is It?

Now we need a convenient way for Python to communicate with the API.

You could manually create HTTP requests. But that's annoying.

Instead, many AI providers provide an SDK.

CORE IDEA

SDK = Software Development Kit

A Python SDK gives you Python code that makes interacting with the API easier.

Instead of manually constructing HTTP requests:

Python
   ↓
HTTP headers
   ↓
Authentication
   ↓
JSON
   ↓
Endpoint
   ↓
Request

you can use:

client.responses.create(...)

Much easier.

INSTALL

Install the Python SDK

For OpenAI's Python SDK, the basic installation is:

pip install openai

Then:

from openai import OpenAI

Now Python can use the SDK.

PRACTICAL

Your First Python Program

Add an instruction:

</> Python
from openai import OpenAI

client = OpenAI()

question = input("Ask AI: ")

prompt = f"""
Answer the user's question in simple English.

User question:
{question}

Give one practical example.
"""

response = client.responses.create(
    model="gpt-5.6",
    input=prompt
)

print("\nAI:", response.output_text)
ARCHITECTURE

How Does the Program Work?

User Question
      ↓
Python
      ↓
Build Prompt
      ↓
Python SDK
      ↓
AI API
      ↓
LLM
      ↓
Answer
      ↓
Python
      ↓
User

This connects directly to Lesson 4 — Prompts.

LESSON 5 • TOPIC COMPLETE

Using an AI API with Python

This connects directly to Lesson 4 — Prompts.