mock_response makes completion and acompletion return a real ChatCompletion without sending a request. No API key is needed, so your test suite stays offline.
Mock a stream
Withstream=True, the mock returns a stream of chunks split on spaces. Async streams work too.
Rehearse failures
Pass an exception to have it raised as given. Use this to test retry and fallback logic:Limits of mocking
- The model string is not resolved, so a mock does not catch a wrong model name. Use
routehub.get_llm_provider(model)to check one. - Usage counts are rough estimates.
embeddingandresponseshave nomock_response. For embeddings, passclient=a fake OpenAI client instead.