{"record":{"id":"2464835836df93d0","repo":"BerriAI/litellm","slug":"mock-error","errorCode":null,"errorMessage":"Mock error","messagePattern":"Mock error","errorType":"exception","errorClass":"Exception","httpStatus":null,"severity":"info","filePath":"litellm/litellm_core_utils/mock_functions.py","lineNumber":14,"sourceCode":"from ..types.utils import (\n    Embedding,\n    EmbeddingResponse,\n    ImageObject,\n    ImageResponse,\n    Usage,\n)\n\n\ndef mock_embedding(model: str, mock_response: list[float] | None):\n    if mock_response is None:\n        mock_response = [0.0] * 1536\n    elif mock_response == \"error\":\n        raise Exception(\"Mock error\")\n    return EmbeddingResponse(\n        model=model,\n        data=[Embedding(embedding=mock_response, index=0, object=\"embedding\")],\n        usage=Usage(prompt_tokens=10, completion_tokens=0),\n    )\n\n\ndef mock_image_generation(model: str, mock_response: str):\n    return ImageResponse(\n        data=[ImageObject(url=mock_response)],\n    )\n","sourceCodeStart":1,"sourceCodeEnd":26,"githubUrl":"https://github.com/BerriAI/litellm/blob/77b7c6c40c0c5aa5fbcb1d6a1825ac39ca8829b8/litellm/litellm_core_utils/mock_functions.py#L1-L26","documentation":"mock_embedding() implements LiteLLM's mock-response convention: when mock_response equals the literal string \"error\", it raises Exception('Mock error') instead of returning an embedding. This is deliberate error injection so you can exercise retries, Router fallbacks, cooldowns and alerting without spending provider tokens. Note the signature says list[float] | None yet \"error\" is the sentinel - and a None/default yields a zero vector of length 1536.","triggerScenarios":"litellm.embedding(model=\"text-embedding-3-small\", mock_response=\"error\") - typically inside Router(fallbacks=[...]) tests, proxy mock configs, or alerting drills.","commonSituations":"Testing retry/fallback behavior; a mock_response=\"error\" left in a copied config reaching a non-test environment; developers expecting mocks to always succeed and treating the raise as a bug.","solutions":["If the raise is unexpected, change mock_response - pass a list of floats (e.g. [0.0] * 1536) or None for default zero vectors","If testing failure paths, keep it and assert your fallback catches Exception with message 'Mock error'","Scope mock usage to test configuration only; never set mock_response in production proxy config","For chat-completion equivalents use litellm.completion(mock_response=\"error\") with the same convention"],"exampleFix":"# before (test fails because the call raises)\nresp = litellm.embedding(model=\"text-embedding-3-small\", mock_response=\"error\")\n\n# after (intentional failure-path test)\nfrom litellm import Router\nrouter = Router(model_list=[...], fallbacks=[{\"text-embedding-3-small\": [\"backup-model\"]}])\nresp = router.embedding(model=\"text-embedding-3-small\", mock_response=\"error\")  # fallback serves it\n\n# or, to succeed:\nresp = litellm.embedding(model=\"text-embedding-3-small\", mock_response=[0.0] * 1536)","handlingStrategy":"try-catch","validationCode":"mock = config.get(\"mock_response\")\nif mock == \"error\":\n    with pytest.raises(Exception, match=\"Mock error\"):\n        litellm.embedding(model=m, mock_response=mock)\n    return\nresp = litellm.embedding(model=m, mock_response=mock)","typeGuard":null,"tryCatchPattern":"try:\n    resp = litellm.embedding(model=m, mock_response=mock_setting)\nexcept Exception as e:\n    if str(e) == \"Mock error\":\n        resp = fallback_embedding_call()  # exercise the fallback path in tests\n    else:\n        raise","preventionTips":["Treat mock_response=\"error\" as a deliberate failure switch, never a bug","Gate mock settings behind an env flag (e.g. LITELLM_MOCK=1) so they cannot reach production","Use vectors ([0.0] * 1536) or None for success-path mocking"],"tags":["litellm","mock","testing","error-injection"],"backgroundTag":"mock-error-injection","analyzedSha":"77b7c6c40c0c5aa5fbcb1d6a1825ac39ca8829b8","analyzedAt":"2026-08-18T11:44:31.656Z","contentChangedAt":null,"schemaVersion":2},"datasetVersion":"2026-09-23T08:17:48.524Z"}