BerriAI/litellm · error · Exception

Mock completion response failed - {e}

Error message

Mock completion response failed - {e}

What it means

Wrapper exception from the mock-response path of completion: when mock_response is supplied, litellm builds a fake ModelResponse instead of calling a provider, and any exception raised while doing so (a mock callable that throws, unexpected mock_return shape, failing logging hooks) is re-raised as Exception('Mock completion response failed - {e}'). openai.APIError is the one exception re-raised unchanged.

Source

Thrown at litellm/main.py:977

            _, custom_llm_provider, _, _ = litellm.utils.get_llm_provider(model=model)
            model_response._hidden_params["custom_llm_provider"] = custom_llm_provider
        except Exception:
            # dont let setting a hidden param block a mock_respose
            pass

        if logging is not None:
            logging.post_call(
                input=messages,
                api_key="my-secret-key",
                original_response="my-original-response",
            )

        return model_response

    except Exception as e:
        if isinstance(e, openai.APIError):
            raise e
        raise Exception(f"Mock completion response failed - {e}")


_OPENAI_DEFAULT_API_BASE: Final = "https://api.openai.com/v1"


def _resolve_openai_api_base(api_base: str | None) -> str:
    """Effective OpenAI base a chat request will hit: arg > global > env > default. The bridge gate
    and the ``_complete_custom_openai`` chat handler MUST resolve this identically, or a custom base
    set via ``litellm.api_base`` or ``OPENAI_BASE_URL``/``OPENAI_API_BASE`` is invisible to the gate,
    which then misreads it as the default OpenAI endpoint and bridges a request the backend can't serve."""
    return (
        api_base
        or litellm.api_base
        or get_secret_str("OPENAI_BASE_URL")
        or get_secret_str("OPENAI_API_BASE")
        or _OPENAI_DEFAULT_API_BASE
    )

View on GitHub (pinned to 77b7c6c40c)

Solutions

  1. Read the inner error after the dash -- it names the real exception from your mock; fix that (usually a missing kwarg or wrong return type)
  2. Make mock callables defensive: accept **kwargs and return a plain string or the documented mock structure
  3. Pin/upgrade litellm deliberately so mock-path kwargs your callable depends on do not change mid-project
  4. If you did not intend mocking, remove the mock_response kwarg leaking in from test fixtures

Example fix

# before
litellm.completion(model='gpt-4o', messages=m, mock_response=lambda **kw: kw['tools'][0])  # KeyError -> Mock completion response failed

# after: defensive mock
litellm.completion(model='gpt-4o', messages=m, mock_response=lambda **kw: 'mocked text')
Defensive patterns

Strategy: try-catch

Validate before calling

# keep mock callables total: never index kwargs, always return a plain value
mock = lambda **kw: 'mocked response'  # instead of kw['messages'][0]

Type guard

def is_safe_mock(mock) -> bool:
    return isinstance(mock, str) or (callable(mock) and 'kwargs' in mock.__code__.co_varnames)

Try / catch

try:
    resp = litellm.completion(model='gpt-4o', messages=m, mock_response=my_mock)
except Exception as e:
    if str(e).startswith('Mock completion response failed'):
        # the suffix after ' - ' is your mock's real exception; fix the mock, not litellm
        pytest.fail(f'broken mock fixture: {e}')
    raise

Prevention

When it happens

Trigger: Passing mock_response as a callable (lambda **kwargs: ...) that itself raises -- e.g. it indexes a kwarg the mock path did not pass; or mock_return values whose type the mock builder cannot serialize into ModelResponse.

Common situations: Unit tests with handwritten mock callables that assume kwargs which changed between litellm versions; mocks shared across sync and async paths; a custom logging callback failing inside post_call during tests.

Related errors


AI-assisted analysis of BerriAI/litellm@77b7c6c40c (2026-08-18). Data as JSON: /api/errors/3c7b66a5da964a62. Report an issue: GitHub.