Skip to main content
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

With stream=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.
  • embedding and responses have no mock_response. For embeddings, pass client= a fake OpenAI client instead.