← Master Index
Vol. 18 Module 18.1 Lecture

OpenAI SDK

SDKs

How This Lesson Fits the Module & Volume

Volume 17 ended in local generation UIs: Automatic1111 tabs and ComfyUI graphs wrapping diffusion checkpoints. Those surfaces are excellent studios. They are not a product. Volume 18 is the shipping volume: vendor SDKs, then your own API, FastAPI, Docker, and hardware (GPU).

The OpenAI Python SDK is the first production contract most teams learn. You stop clicking a WebUI and start calling a remote model with an API key, retries, streaming, and tool round-trips you already met in Vol. 15 function calling. After this page: Anthropic and Gemini—same job, different wire formats—then wrap them behind your backend.

Learning Objectives

By the end of this lesson, students should be able to:

  • Contrast a local gen UI (A1111 / ComfyUI) with a hosted SDK as a product surface.
  • Install and authenticate the official openai Python client via environment variables.
  • Run a chat completion (or Responses) call and read text, usage, and finish reasons.
  • Stream tokens and sketch where FastAPI / SSE will sit in Module 18.2.
  • Wire tools using the Vol. 15 function-calling round-trip, not ad-hoc JSON prompting.
  • Treat model IDs as SKUs that change—never hard-code a nickname as architecture.
Definition

The OpenAI SDK (official openai Python / Node packages) is a typed HTTP client for OpenAI’s APIs: chat/completions or Responses, embeddings, images, audio, and assistants/tools. It is not the model. GPT-class weights live on OpenAI’s side; the SDK only serializes requests, handles auth headers, retries, and streaming. Compatible gateways (Azure OpenAI, many local servers) speak a similar wire format—still not “the OpenAI model.”

From Local Studio to Production Call

SurfaceWhat you operateWhat ships to users
Automatic1111Gradio tabs, PNG infotext, local checkpointA demo on port 7860—not multi-tenant
ComfyUITensor DAG + workflow JSONBetter reproducibility; still a studio
OpenAI SDKAPI key + model SKU + messages/toolsA backend you can auth, meter, and deploy
Module 18.2 wrapFastAPI + Docker + Redis/CeleryYour public product API

Keep A1111/Comfy for stills R&D. When the user-facing app needs chat, RAG, or agents, call a hosted (or self-hosted compatible) API. Do not expose 7860 to the internet and call it shipping.

Client, Auth, and a First Completion

Never put keys in source. OpenAI() reads OPENAI_API_KEY. Model IDs below are teaching stand-ins—re-read current docs before a spike.

import os from openai import OpenAI # pip install openai # export OPENAI_API_KEY=sk-... (never commit this) client = OpenAI() # or OpenAI(api_key=os.environ["OPENAI_API_KEY"]) resp = client.chat.completions.create( model="gpt-4.1-mini", # SKU changes; pin in config, not scattered literals messages=[ {"role": "system", "content": "You are a concise backend tutor."}, {"role": "user", "content": "Contrast A1111 with a hosted chat API in two sentences."}, ], temperature=0.2, max_tokens=200, ) print(resp.choices[0].message.content) print(resp.usage) # prompt_tokens, completion_tokens — feed Module 18.2 monitoring # Streaming preview (full story: volumes/vol-18/module-18-2/streaming.html) stream = client.chat.completions.create( model="gpt-4.1-mini", messages=[{"role": "user", "content": "Count to five, slowly."}], stream=True, ) for chunk in stream: delta = chunk.choices[0].delta.content if delta: print(delta, end="", flush=True)

What Else the SDK Is For

Chat / Responses

Embeddings / audio

  • Vectors for RAG (Vol. 14)
  • Whisper / TTS (Vol. 16)
  • Same client, different method

Images

  • DALL·E / GPT Image API
  • Not a replacement for SD/FLUX self-host
  • Closed weights, ToS, safety filters

OpenAI vs the Rest of Module 18.1

Why start here

  • Largest ecosystem + Azure / compatible servers
  • Tool-calling contract students already practiced
  • Images + audio + embeddings in one vendor

Do not stop here

  • Anthropic: Messages API, strong long-context / computer-use story
  • Gemini: native multimodal + Google grounding
  • Your app should depend on your API, not a single vendor SDK forever

Related Lectures

LectureWhy it sits next to this SDK
A1111 / ComfyUILocal stills UIs you are leaving as the product surface
Function callingTool round-trip the OpenAI client implements
DALL·EImages API vs self-host SD/FLUX
Anthropic SDKNext vendor; same shipping job
FastAPI / StreamingWrap this client behind your HTTP API
AuthenticationYour users never see OPENAI_API_KEY
Common Misconception

“The OpenAI SDK is ChatGPT.” ChatGPT is a consumer app. The SDK talks to model endpoints. Second: copying a notebook that hard-codes sk-... is not authentication—that is a leak. Third: gpt-4o / gpt-4.1 / whatever ships next are product SKUs, not architectures like Stable Diffusion. Fourth: a working chat.completions.create in a laptop REPL is not a backend; Module 18.2 exists because latency, auth, queues, and cost meters are the real product.

Knowledge Check

  1. Short Answer: What did Volume 17 leave you with that is not a production API? Answer: Local gen UIs (Automatic1111 / ComfyUI) wrapping diffusion checkpoints.
  2. True/False: The OpenAI SDK contains the GPT weights. Answer: False—it is an HTTP client; weights stay on the provider (or a compatible server).
  3. Multiple Choice: Where should OPENAI_API_KEY live? (a) committed in git, (b) environment / secret store, (c) the system prompt. Answer: (b).
  4. Short Answer: Name one usage field you should meter in production. Answer: prompt_tokens and/or completion_tokens (or total_tokens / cost).
  5. True/False: Streaming is done by setting stream=True and iterating chunk deltas. Answer: True (chat.completions pattern).
  6. Multiple Choice: Tool calling in this SDK implements: (a) Vol. 15 function-calling round-trip, (b) k-means, (c) DDIM steps. Answer: (a).
  7. Short Answer: Why wrap the SDK in FastAPI instead of giving every frontend the vendor key? Answer: Auth, rate limits, cost control, prompt/policy, and key secrecy.
  8. True/False: Model ID strings are stable architectures you can cite like “UNet.” Answer: False—they are changing SKUs; pin in config and re-read docs.
  9. Multiple Choice: Next lecture in this module: (a) Anthropic SDK, (b) DBSCAN, (c) Automatic1111. Answer: (a).
  10. Short Answer: When would you still use A1111/Comfy instead of the OpenAI Images API? Answer: Self-host stills, ControlNet/LoRA graphs, license/offline constraints—not consumer ChatGPT stills.

Key Takeaways

  • Vol. 17 UIs are studios; Vol. 18 SDKs are how products call models.
  • Official openai client + env key + pin model SKUs in config.
  • Read usage; stream tokens; tools follow Vol. 15, not vibes.
  • Never ship the vendor key to browsers; that is Module 18.2.
  • Next: Anthropic SDK, then Gemini, then your API.
Trainer’s Guide

Lab: Same user brief via (1) A1111 txt2img still, (2) OpenAI chat explaining the still, (3) OpenAI Images API still. Discuss license, latency, and what you would put behind FastAPI. Add a tiny tool (e.g. get_sku_price) using the Vol. 15 round-trip.

Whiteboard: Box “studio UI” vs “vendor SDK” vs “your API.” Arrow secrets only into the backend. Preview streaming → SSE.

Recap: Leave Automatic1111/Comfy as labs; call models with the OpenAI SDK. Continue with Anthropic SDK.