Appendix C. Using the OpenAI API
The appendix serves as a practical reference for moving from browser-based prompting to direct OpenAI API use in Python. It focuses on the patterns used throughout the book: sending a request, selecting an appropriate model, receiving structured results, estimating token usage, controlling cost, and recovering from errors. It also clarifies that the examples assume the newer Python SDK, so older code written for the v0.x interface will not work without changes.
At the core of API usage is a simple chat-completions call built from a conversation list, usually with a system message that sets rules and a user message that supplies the current record or question. The appendix emphasizes separating persistent instructions from per-item input because that makes one prompt reusable across many records in pipeline workflows. It also explains model choice as a tradeoff between capability and cost: use the more capable model for difficult reasoning or extraction tasks, and the cheaper model for high-volume, simpler steps.
A major theme is structured output, where a Pydantic schema defines the exact data shape expected from the model so the reply can be validated and returned as a ready-to-use object rather than free text. The appendix also highlights that token counts drive pricing, so reducing input size and choosing cheaper models are the most effective ways to control spending. Finally, it recommends wrapping API calls in retry logic with exponential backoff for transient failures, while avoiding retries for errors caused by bad inputs, and it provides a quick map of the main syntax differences between the old and current SDK styles.
Learn AI Data Engineering ebook for free