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.
Python code
15 linesfrom 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
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
- Use a dictionary to return different responses based on prompt keywords
- 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
More from AI & LLM integration patterns
- Cache LLM Completions by Hashing the Prompt in Python easy
- Chain of Thought Prompting in Python: Step-by-Step Reasoning Demo easy
- Circuit Breaker Pattern in Python for LLM API Calls medium
- Cosine Similarity to Retrieve Top K Chunks in Python easy
- Demonstrate Prompt Injection Bypass in Python easy
- How to Accumulate Streamed Tokens into a Final String in Python easy
Keep learning
Related tutorials and quizzes for this topic.