Reusable OpenAI Utilities

Ma Mahalakshmi V Updated 19 Sep 2026
7 min read ·Lesson 207 of 224

Packaging Reusable OpenAI SDK Utilities

Everything built in this unit so far — service classes, dependency injection, typed models, decorators, custom exceptions, careful abstraction design — has lived inside a single application's codebase. Once a team builds several AI-powered features across multiple projects, the same service classes, retry decorators, and exception hierarchies tend to get copy-pasted from one repository into the next. Packaging turns that shared code into an installable Python package — a proper internal library — so it is written once, versioned, and installed as a dependency everywhere it is needed, instead of copied and drifting out of sync.

Why Copy-Pasting Shared Code Fails Over Time

Imagine a retry decorator (Lesson 5) and an AIServiceError hierarchy (Lesson 6) copy-pasted into three different projects at a company. Six months later, someone fixes a bug in the retry decorator's delay logic in Project A. Unless someone remembers to manually copy that fix into Projects B and C, those two projects keep running the buggy version indefinitely — there is no mechanism connecting the three copies, and no way to know from Project B's code alone that a fix exists elsewhere. This is the core problem packaging solves: one canonical version of shared code, installed (and upgraded) as a versioned dependency, instead of N independent copies that silently diverge.

The Minimal Anatomy of an Installable Python Package

A modern Python package needs, at minimum: a directory containing your source code, an __init__.py (or, for namespace packages, none — but an explicit __init__.py is the simpler default), and a pyproject.toml file describing the package's metadata and dependencies.

ai-toolkit/
├── pyproject.toml
├── README.md
└── src/
    └── ai_toolkit/
        ├── __init__.py
        ├── service.py
        ├── decorators.py
        ├── exceptions.py
        └── settings.py

This is the src layout — source code lives under src/<package_name>/ rather than directly at the project root. It is the currently recommended layout because it prevents a common class of bug where tests accidentally import the local, uninstalled source directory instead of the actually-installed package, which can mask packaging mistakes that would otherwise only surface after publishing.

A minimal pyproject.toml for this package:

[build-system]
requires = ["hatchling"]
build-backend = "hatchling.build"

[project]
name = "ai-toolkit"
version = "0.1.0"
description = "Shared OpenAI SDK service classes, decorators, and exceptions for internal AI features."
requires-python = ">=3.10"
dependencies = [
    "openai>=1.0,<2.0",
    "pydantic>=2.0,<3.0",
]

[project.optional-dependencies]
dev = [
    "pytest>=7.0",
    "mypy>=1.0",
]

Note: pyproject.toml's structure is defined by Python packaging standards (PEP 621 and related), but the specific build backend (hatchling here; setuptools and poetry-core are common alternatives) and its exact configuration options can vary and evolve. Confirm current conventions against the Python Packaging User Guide before setting up a real package.

Each field here does real work. [build-system] tells any tool that installs this package (like pip) which backend actually builds the distributable artifact. [project] holds metadata: the package's importable name, its version (used for dependency resolution — see below), and its own dependencies, each pinned to a version range rather than left unconstrained.

Why Version Pinning in Dependencies Matters

Notice "openai>=1.0,<2.0" rather than a bare "openai". An unconstrained dependency lets any future version of the OpenAI SDK — including one with breaking changes — be installed silently the next time someone runs pip install. Pinning a range communicates: "this package was built and tested against the 1.x line of the SDK, and has not been verified against 2.x." When the SDK does release a 2.0, upgrading the internal library's own dependency range becomes a deliberate, tested decision (see Lesson 10) rather than something that happens by accident to whoever installs the package next.

What Goes Inside the Package

The package's own source mirrors the patterns from earlier lessons, but written once and exported cleanly:

# src/ai_toolkit/exceptions.py

class AIServiceError(Exception):
    """Base class for all errors raised by ai_toolkit integrations."""


class AIRateLimitedError(AIServiceError):
    """Raised when the AI provider is rate-limiting requests."""


class AITransientError(AIServiceError):
    """Raised for errors that are likely temporary and might succeed on retry."""
# src/ai_toolkit/service.py

from openai import APIError, APITimeoutError, RateLimitError

from ai_toolkit.exceptions import AIRateLimitedError, AIServiceError, AITransientError


class SummarizerService:
    def __init__(self, client, model: str = "gpt-5.6-terra") -> None:
        self._client = client
        self._model = model

    def summarize(self, text: str) -> str:
        try:
            response = self._client.responses.create(
                model=self._model,
                input=f"Summarize:\n\n{text}",
            )
            return response.output_text
        except RateLimitError as error:
            raise AIRateLimitedError("Rate limited by the AI provider.") from error
        except APITimeoutError as error:
            raise AITransientError("Request to the AI provider timed out.") from error
        except APIError as error:
            raise AIServiceError(f"AI provider error: {error}") from error
# src/ai_toolkit/__init__.py

from ai_toolkit.exceptions import AIRateLimitedError, AIServiceError, AITransientError
from ai_toolkit.service import SummarizerService

__all__ = [
    "AIRateLimitedError",
    "AIServiceError",
    "AITransientError",
    "SummarizerService",
]

The __init__.py re-exports the package's public names so that consumers can write from ai_toolkit import SummarizerService, AIServiceError instead of needing to know the internal module layout (ai_toolkit.service, ai_toolkit.exceptions). The __all__ list is a further, explicit statement of what is public API and what is internal implementation detail — a name left out of __all__ (even if technically importable) signals to consumers that it is not meant to be relied upon and might change without notice.

Installing the Package Locally During Development

While developing both the library and an application that uses it side by side, an editable install lets changes to the library's source be picked up immediately, without reinstalling after every edit:

pip install -e /path/to/ai-toolkit

Once published (to a private package index, or referenced directly via a git URL), consuming applications declare it as an ordinary dependency in their own pyproject.toml:

[project]
dependencies = [
    "ai-toolkit>=0.1.0,<0.2.0",
]

Testing a Package in Isolation

A packaged library should ship its own test suite, using exactly the dependency-injection pattern from Lesson 2 — testing the library's own classes against fake clients, with no dependency on any consuming application:

class FakeResponse:
    def __init__(self, output_text: str) -> None:
        self.output_text = output_text


class FakeResponsesAPI:
    def __init__(self, canned_text: str) -> None:
        self._canned_text = canned_text

    def create(self, **kwargs):
        return FakeResponse(self._canned_text)


class FakeClient:
    def __init__(self, canned_text: str) -> None:
        self.responses = FakeResponsesAPI(canned_text)


def test_summarizer_service_returns_model_output() -> None:
    from ai_toolkit import SummarizerService

    service = SummarizerService(client=FakeClient(canned_text="A concise summary."))

    result = service.summarize("Some long input text.")

    assert result == "A concise summary."
    print("PASS: SummarizerService returns the fake client's canned output")


test_summarizer_service_returns_model_output()

This test lives inside the library's own test suite (conventionally under tests/ at the project root) and is run whenever the library itself changes — independent of any application that eventually installs it.

Semantic Versioning

The version = "0.1.0" field follows semantic versioning (MAJOR.MINOR.PATCH): increment PATCH for backward-compatible bug fixes, MINOR for backward-compatible new functionality, and MAJOR for breaking changes to the public API. This convention lets consuming applications express exactly how much change they are willing to accept automatically (ai-toolkit>=0.1.0,<0.2.0 accepts patch and minor updates within 0.1.x, but never a breaking 0.2.0 change) — which is precisely what makes safe automatic upgrades possible at all.

Common Mistakes

Publishing a package with no version constraints on its own dependencies. This allows a future, possibly incompatible release of the OpenAI SDK (or Pydantic, or any other dependency) to be silently installed alongside the library, breaking consumers without warning.

Exposing internal implementation modules as if they were public API. If consumers start importing ai_toolkit.service.SummarizerService directly instead of ai_toolkit.SummarizerService, refactoring the internal module layout later becomes a breaking change even though the intended public API never changed.

Skipping the package's own test suite because "it's just internal code." Internal libraries used by multiple teams cause more widespread damage when broken than a single application's own bug, precisely because many things depend on them at once.

Best Practices

Use the src layout and export a clean public API through __init__.py and __all__. This creates a clear boundary between what consumers are meant to depend on and what remains free to change.

Pin dependency version ranges deliberately, and update them as a conscious decision, not by accident. This directly sets up the version-evolution practices covered in Lesson 10.

Follow semantic versioning strictly, especially for breaking changes. Consumers rely on the version number alone to decide whether an upgrade is safe to take automatically.

Give the package its own independent test suite, using fakes and dependency injection exactly as application code does. A library that cannot be tested without a real API key is a library nobody will want to depend on.

0 Comments

Reviewed before they appear

No comments yet.

OpenAI SDK
Introduction to the OpenAI SDK Setting Up Python Creating an API Key Your First Call — client.responses.create() and response.output_text Understanding Billing, Credits, and What a Request Costs Why Responses Replaced Chat Completions Anatomy of a Request: model, input, and instructions Anatomy of a Response: The Typed output Array, Not Just Text Roles: User, Assistant, and Developer/System Choosing a Model, and Reading the Models Page Instead of Memorizing Names Instructions vs. Input Writing Prompts That Get Consistent Results Few-Shot Examples Reasoning Models and the reasoning Parameter Debugging a Prompt That Misbehaves Why Streaming Matters for User Experience stream=True and Iterating Over Events Handling the Event Types You Actually Care About Background Mode for Long-Running Jobs Project — Add Live Streaming to Your Chatbot The Problem With Parsing Free Text JSON Schema and Strict Mode Pydantic Models With the SDK's Parse Helpers Handling Refusals and Validation Failures Project — A Resume-to-JSON Extractor Working With input_image input_file, PDFs, and the Files API Image Generation Speech-to-Text and Text-to-Speech Project: A PDF Question-Answering Script What Function Calling Is Defining a Tool Schema The Full Loop Multiple Tools Errors, Timeouts, and Untrusted Arguments Project: A Weather Assistant Web Search File Search and Vector Stores Code Interpreter Remote MCP Servers and Connectors Project: A Research Assistant What an Embedding Is, Without the Maths Generating and Storing Embeddings Similarity Search From Scratch Hosted Vector Stores vs. Rolling Your Own A Small RAG App Over a Folder of Notes Agents vs. a Single API Call — When You Need One pip install openai Giving Agents Tools Handoffs and Multi-Agent Triage Guardrails and Approvals Tracing and Observing What Your Agent Did A Multi-Agent Support Desk Error Codes and What Each One Means Retries, Timeouts, and Backoff Rate Limits and Spend Limits Prompt Caching and Cost Optimisation The Batch API for Bulk Work Async Clients and Concurrency Moderation and Safety Best Practices Designing the App Backend With FastAPI Streaming to a Simple Frontend Deploying and a Cost/Safety Checklist Why Web Search Is Useful for Current Information Using the Web Search Tool with the Responses API Configuring Search Behavior for Application Use Cases Understanding Citations and Source Attribution Where to Go Next Building a Research Assistant with Web Search Combining Web Search with Structured Outputs Handling Conflicting or Low-Quality Web Sources Reducing Unsupported Claims with Grounded Generation Testing Freshness-Sensitive AI Answers Production Considerations for Web-Grounded Applications Understanding File Search and Retrieval-Augmented Generation Creating and Organizing Vector Stores Uploading Documents for Retrieval Connecting Vector Stores to Responses API Requests Designing Document Metadata and Filtering Strategies Building a PDF Question-Answering Application Improving Retrieval Quality With Better Document Preparation Handling Missing Evidence and Retrieval Failures Combining File Search With Web Search Building a Production Knowledge-Base Assistant What the Code Interpreter Tool Is Designed For Running Python-Based Analysis Through the OpenAI SDK Uploading Datasets for Analysis Analyzing CSV and Spreadsheet Data Generating Charts and Data Summaries Handling Generated Files and Downloadable Artifacts Building a Data-Analysis Assistant Combining Code Execution with Structured Outputs Validating Generated Calculations and Results Security and Sandbox Considerations for Code Execution Understanding Multimodal Input with the OpenAI SDK Sending Images to a Model Image Analysis from URLs and Uploaded Files Extracting Text and Information from Screenshots Building an Image-Question-Answering Application Combining Image Input with Structured Output Analyzing Multiple Images in One Request Handling Image Quality and Input Limitations Designing Multimodal Prompts for Reliable Results Building a Practical Vision-Powered Python Application Understanding Speech-to-Text and Text-to-Speech Workflows Transcribing Audio with the OpenAI SDK Working with Uploaded Audio Files Handling Timestamps and Transcription Metadata Building a Meeting Transcription Workflow Generating Spoken Responses from Text Handling Long Audio and Processing Failures Combining Audio with Text and Tool Calling Building an End-to-End Python Voice Application What Embeddings Are and When to Use Them Generating Embeddings With the OpenAI API Preparing Text for Embedding Comparing Vectors With Cosine Similarity Building a Simple Semantic Search Engine in Python Storing Embeddings in a Database Metadata Filtering for Semantic Search Chunking Strategies for Better Retrieval Evaluating Semantic Search Quality Building a Document Similarity Application When Batch Processing Makes Sense Designing Large-Volume AI Processing Pipelines Using Asynchronous Python with the OpenAI SDK Running Concurrent Requests Safely Controlling Concurrency and Avoiding Rate Limits Tracking Batch Job Progress Handling Partial Failures in Bulk Workloads Retrying Failed Items Without Duplicating Successful Work Designing Resumable AI Processing Jobs Building a Production Batch-Processing Pipeline Batch Processing Makes Sense Large-Scale AI Processing Pipelines Async Python with OpenAI SDK Safe Concurrent Requests Concurrency & Rate Limits Batch Progress Tracking Partial Failure Handling Safe Retry Handling Resumable AI Jobs Production Batch Pipeline System–User Data Separation Reusable App Instructions Prompt Templates & Variables Extraction & Classification Prompts Summarization & Transformation Prompts Explicit Output Requirements Prompt Version Management Prompt Testing & Evaluation Reusable Python Prompt Library API Key Security Secure API Key Storage Secure Secret Management Prompt Injection Prevention Trusted vs. Untrusted Content Tool Argument Validation Sensitive Data Handling Secure Logging AI Action Authorization Production AI Security Checklist Why AI Applications Need Evaluation Beyond Unit Tests Unit Testing OpenAI SDK Integration Code Mocking API Responses in Python Tests Testing Structured Outputs Against Schemas Testing Tool-Calling Workflows Building a Small Evaluation Dataset Measuring Accuracy, Consistency, and Failure Rates Regression Testing Prompts and Model Changes Human Evaluation Versus Automated Evaluation Creating a Repeatable Evaluation Pipeline AI Request Monitoring Token Cost Management Usage Metrics Design Reducing Model Calls Prompt & Context Optimization Model Selection & Optimization AI Caching Strategies Interactive Latency Optimization Usage Dashboards & Budget Alerts Performance & Cost Checklist Every API Call Starts Fresh Fixing API Statelessness Server-Side Conversation Memory Limits of Response Chaining What We're Building Conversation Memory Challenges Preparing an OpenAI SDK Application for Deployment Environment-Specific Configuration for Development and Production Deploying a Python AI Service with Docker Container Health Checks and Startup Configuration Managing Secrets in Cloud Deployments Background Workers for Long-Running AI Tasks Queues and Asynchronous Job Architectures Scaling AI Workloads Horizontally Monitoring Production Incidents and Failures Production Deployment Checklist for OpenAI SDK Applications Reusable OpenAI Service Classes AI Client Dependency Injection Typed AI Responses Python Configuration Management AI Request Decorators Centralized AI Error Handling Clean SDK Abstractions Reusable OpenAI Utilities Internal AI Python Libraries SDK Integration Maintenance Production AI Chatbot Document Q&A System Web Research Assistant Customer Support Agent AI Data Analysis Assistant Image Analysis App Meeting Transcription & Summary Semantic Document Search Multi-Tool AI Agent Production OpenAI SDK App Why "It Looked Fine When I Tested It" Isn't Enough Timing Note Status Note Pre-Decision Status Note Current Availability Note
Ask about this post
AI Ask about this post

Ask questions about Reusable OpenAI Utilities and get answers drawn from it.

Signed-in readers only.