How to Mock an LLM Client in Python

Create a simple mock LLM client that returns a canned completion for testing or development without a real API.

Easy Python 3.9+ Aug 9, 2026 AI & LLM integration patterns 17 views 0 copies

Python code

15 lines
Python 3.9+
from dataclasses import dataclass


@dataclass
class MockLLMClient:
    canned_response: str = "This is a canned completion."

    def complete(self, prompt: str) -> str:
        return f"{self.canned_response} [to: {prompt[:20]}]"


if __name__ == "__main__":
    client = MockLLMClient()
    result = client.complete("Tell me about Python")
    print(result)

Output

stdout
This is a canned completion. [to: Tell me about Python]

How it works

The MockLLMClient is a dataclass that holds a canned_response string. The complete method simulates an LLM call by returning the canned response plus a truncated version of the input prompt. By default, the canned response is a generic string, but it can be overridden when instantiating the client. This pattern is useful for unit tests or offline development where a real LLM API is unavailable or undesirable.

Common mistakes

  • Forgetting to override the default canned response in tests, leading to unexpected assertions
  • Not truncating the prompt, causing long inputs to inflate the expected output
  • Using a real LLM client in tests, making them slow and dependent on external APIs
  • Assuming mock clients handle streaming or other LLM features without implementing them

Variations

  1. Use a dictionary to return different responses based on prompt keywords
  2. Add a `temperature` or `max_tokens` parameter to simulate configurable generation

Real-world use cases

  • Unit testing code that calls an LLM by replacing the client with a deterministic mock.
  • Developing features locally without an API key or network access.
  • Simulating LLM responses in CI pipelines to ensure consistency and speed.

Sponsored

Run this sample

Open the browser IDE to tweak the example and see results without installing anything.

Open editor

More from AI & LLM integration patterns

Related tutorials and quizzes for this topic.