AI Client Dependency Injection

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

Dependency Injection for AI Clients

Every unit in this course has tested application logic using fake, injected clients rather than real API calls — a FakeClient with a .responses.create() method that returns a canned object, checked with assert and print("PASS: ..."). That pattern was introduced quietly, as a testing convenience. This lesson generalizes it into a first-class architectural principle for a whole application: dependency injection (DI).

What Dependency Injection Is

Dependency injection means a piece of code receives the objects it depends on from the outside, rather than creating them itself. For an OpenAI-backed application, the "dependency" is almost always the SDK client (or a service class wrapping it, as in Lesson 1).

Without DI:

from openai import OpenAI


class Summarizer:
    def __init__(self) -> None:
        self._client = OpenAI()  # created internally — hard-coded dependency

    def summarize(self, text: str) -> str:
        response = self._client.responses.create(
            model="gpt-5.6-terra",
            input=f"Summarize:\n\n{text}",
        )
        return response.output_text

With DI:

from openai import OpenAI


class Summarizer:
    def __init__(self, client: OpenAI) -> None:
        self._client = client  # received from outside — injected dependency

    def summarize(self, text: str) -> str:
        response = self._client.responses.create(
            model="gpt-5.6-terra",
            input=f"Summarize:\n\n{text}",
        )
        return response.output_text

The difference looks small — one line moves from inside the class to its constructor signature — but it changes who controls the dependency. In the first version, Summarizer can never be tested, configured, or reused without a real OpenAI() client and, by extension, a real API key and network access. In the second version, anything that satisfies the shape .responses.create(...) -> object with .output_text can be passed in: a real client, a fake client, a client pointed at a different base URL, or a client with custom retry settings.

Why Dependency Injection Matters for AI Clients

AI clients are a particularly important place to apply DI because they are:

  • Expensive and slow to call for real. A test suite that hits the live API on every run is slow, costs money, and produces different results every time depending on model behavior.
  • A source of nondeterminism. Model outputs are not guaranteed to be identical between calls, which makes assertions on exact output unreliable if you're calling the real API.
  • A single point of configuration. API keys, base URLs, timeouts, and retry policy are typically set once and should not be duplicated across every class that happens to need a client.

DI solves all three: tests inject a fake client with fixed, deterministic output; the API key and connection settings live in exactly one place (wherever the real client is constructed); and any class that needs a client just declares that need in its constructor, without knowing where the client comes from.

Constructor Injection

The style used above — passing the dependency as a constructor argument — is called constructor injection, and it is the default choice for most Python applications:

class TicketRouter:
    def __init__(self, classifier: "TicketClassifierService") -> None:
        self._classifier = classifier

    def route(self, ticket_text: str) -> str:
        category = self._classifier.classify_ticket(ticket_text)
        queues = {
            "billing": "billing-queue",
            "technical": "tech-queue",
            "account": "account-queue",
        }
        return queues.get(category, "general-queue")

TicketRouter does not create a TicketClassifierService itself, and it does not know whether that service is backed by a real or fake client. It only knows the service's public interface (classify_ticket). This is the essence of DI: dependencies are declared as parameters, and something else — application startup code, or a test — decides what to supply.

Composition at the Application's Entry Point

If every class receives its dependencies from outside, something still has to construct the real objects somewhere. That "somewhere" should be as close to the application's entry point as possible — a main() function, a web framework's startup hook, or a small bootstrap.py module — never scattered throughout business logic.

from openai import OpenAI


def build_ticket_router() -> TicketRouter:
    client = OpenAI()  # reads OPENAI_API_KEY from the environment
    classifier = TicketClassifierService(client=client, model="gpt-5.6-terra")
    return TicketRouter(classifier=classifier)


def main() -> None:
    router = build_ticket_router()
    queue = router.route("I was charged twice this month.")
    print(f"Routed to: {queue}")


if __name__ == "__main__":
    main()

This is sometimes called the composition root — the one place in the application where concrete objects are wired together. Everywhere else in the codebase, code depends only on abstractions (a client-shaped object, a service class's public methods), never on concrete construction details like OpenAI() or environment variables.

Testing With Injected Fakes

Because TicketRouter and TicketClassifierService both take their dependencies as constructor arguments, a test can assemble the whole chain using fakes, with no real client anywhere:

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_router_sends_billing_tickets_to_billing_queue() -> None:
    fake_client = FakeClient(canned_text="billing")
    classifier = TicketClassifierService(client=fake_client)
    router = TicketRouter(classifier=classifier)

    queue = router.route("Why was I charged twice?")

    assert queue == "billing-queue"
    print("PASS: billing category routes to billing-queue")


test_router_sends_billing_tickets_to_billing_queue()

Notice that this test exercises two real, unmodified classes (TicketClassifierService and TicketRouter) end to end — only the bottom-most dependency, the client, is faked. This is the general shape of DI-based testing: fake the boundary that talks to the outside world (the network), and let all of your own logic run for real.

Injection via Function Parameters

Not every dependency needs a class. A plain function can also receive its dependency as a parameter instead of importing a global client:

def summarize_with(client, text: str, model: str = "gpt-5.6-terra") -> str:
    response = client.responses.create(
        model=model,
        input=f"Summarize:\n\n{text}",
    )
    return response.output_text

This works well for small utility functions used in scripts or notebooks. As soon as a function starts accumulating multiple dependencies or gets called from many places with the same dependency, wrapping it in a class (Lesson 1) usually keeps the calling code cleaner, since a class's constructor stores the dependency once instead of every call site re-passing it.

Framework-Provided Dependency Injection

Web frameworks such as FastAPI provide a formal DI mechanism where a function declares its dependencies as parameters with default values, and the framework resolves and injects them automatically per request:

from fastapi import Depends, FastAPI

app = FastAPI()


def get_summarizer() -> Summarizer:
    return Summarizer(client=OpenAI())


@app.post("/summarize")
def summarize_endpoint(text: str, summarizer: Summarizer = Depends(get_summarizer)) -> dict:
    return {"summary": summarizer.summarize(text)}

Note: FastAPI's Depends mechanism is specific to that framework; the underlying principle — pass dependencies in rather than construct them inside the function — is the same manual pattern shown throughout this lesson, just automated by the framework.

This does not replace manual constructor injection; it automates the same idea at the web-framework layer, while the Summarizer class itself still uses plain constructor injection underneath, which is exactly why it remains just as testable in isolation.

Common Mistakes

Constructing the client deep inside business logic. If TicketClassifierService.classify_ticket() created its own OpenAI() client the first time it was called, no test could ever substitute a fake — the dependency would be invisible from the outside.

Passing configuration values instead of the dependency itself. Injecting an API key string and having each class build its own client from it re-introduces duplication and makes tests still need real credentials. Inject the constructed client (or service), not the raw configuration used to build it.

Over-injecting. Not everything needs to be injected — a pure function with no I/O (like a text-cleaning helper) gains nothing from DI and only adds noise. Reserve DI for dependencies that are expensive, external, or need to vary between production and tests.

Best Practices

Depend on the narrowest interface you need. If a class only ever calls client.responses.create(...), it does not need the full OpenAI type hint — accepting any object with that shape (see Lesson 3 on structural typing with Protocol) keeps fakes simple and decouples code from the SDK's concrete class.

Keep one composition root. Construct real clients and wire dependencies together in exactly one place per application (or one per entry point, if there are several) so it is always clear where production wiring happens.

Make fakes as small as possible. A fake client should implement only the methods your code actually calls — resist the temptation to build a full mock of the SDK when three lines of a hand-written fake class do the job clearly and legibly.

Write the test before or alongside the class. Since DI is what makes a class testable in the first place, treating "can I test this without a real API call?" as a design check while writing a class catches accidental hidden dependencies early.

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 AI Client Dependency Injection and get answers drawn from it.

Signed-in readers only.