{"record":{"id":"f5359d8bc58dc354","repo":"BerriAI/litellm","slug":"response-text-f5359d","errorCode":null,"errorMessage":"response.text","messagePattern":"response\\.text","errorType":"exception","errorClass":"TextCompletionCodestralError","httpStatus":null,"severity":"error","filePath":"litellm/llms/codestral/completion/handler.py","lineNumber":62,"sourceCode":"            self.response = response\n        else:\n            self.response = httpx.Response(status_code=status_code, request=self.request)\n        super().__init__(self.message)  # Call the base class constructor with the parameters it needs\n\n\nasync def make_call(\n    client: AsyncHTTPHandler,\n    api_base: str,\n    headers: dict,\n    data: str,\n    model: str,\n    messages: list,\n    logging_obj,\n):\n    response: Final = await client.post(api_base, headers=headers, data=data, stream=True)\n\n    if response.status_code != 200:\n        raise TextCompletionCodestralError(status_code=response.status_code, message=response.text)\n\n    completion_stream: Final = response.aiter_lines()\n    # LOGGING\n    logging_obj.post_call(\n        input=messages,\n        api_key=\"\",\n        original_response=completion_stream,  # Pass the completion stream for logging\n        additional_args={\"complete_input_dict\": data},\n    )\n\n    return completion_stream\n\n\nclass CodestralTextCompletion:\n    def __init__(self) -> None:\n        super().__init__()\n\n    def _validate_environment(","sourceCodeStart":44,"sourceCodeEnd":80,"githubUrl":"https://github.com/BerriAI/litellm/blob/6c2dcb801bf2b75c18f1bb24140e7cf57465cc4d/litellm/llms/codestral/completion/handler.py#L44-L80","documentation":"In the streaming text-completion path for Codestral, make_call posts to the API with stream=True and checks the status code before consuming the body. Any non-200 response (401 unauthorized, 422 malformed request, 429 rate limit, 5xx) is converted into a TextCompletionCodestralError whose message is the raw response body text. This happens during stream setup, before any chunk is yielded.","triggerScenarios":"Calling Codestral text completion (api.mistral.ai/v1/fim/completions style, via the codestral text-completion handler) in streaming mode when the API returns any non-200 status: expired API key (401), bad request payload (400/422), rate limiting (429), or upstream outage (5xx).","commonSituations":"Expired or revoked CODESTRAL_API_KEY; switching between the Codestral beta endpoint and GA endpoint with an incompatible model name; exceeding Mistral rate limits under load; passing a context longer than the model limit.","solutions":["Inspect the raised TextCompletionCodestralError.status_code and .message: the message is the raw body from Mistral and names the real cause.","401/403: refresh CODESTRAL_API_KEY with a valid Mistral/Codestral key.","429: add retry with exponential backoff (e.g. litellm's num_retries or tenacity) and reduce request rate.","400/422: validate the payload — model name must be a FIM model (codestral-latest), prompt formatted as text completion, max_tokens within limits.","5xx: retry with backoff; check status.mistral.ai."],"exampleFix":null,"handlingStrategy":"retry","validationCode":null,"typeGuard":null,"tryCatchPattern":"from litellm.exceptions import TextCompletionCodestralError\n\ntry:\n    stream = litellm.text_completion(\n        model=\"text-completion-codestral/codestral-latest\", prompt=p, stream=True\n    )\n    for tok in stream:\n        ...\nexcept TextCompletionCodestralError as e:\n    if e.status_code in (429, 500, 502, 503):\n        backoff_and_retry()  # transient\n    else:\n        raise  # 401/422 are permanent: fix key/payload","preventionTips":["Set litellm num_retries with exponential backoff for 429/5xx on Codestral calls.","Validate FIM payloads (prompt string, valid model, sane max_tokens) before sending.","Run a one-token smoke completion at startup to catch auth problems early."],"tags":["codestral","mistral","streaming","http-error","text-completion"],"backgroundTag":null,"analyzedSha":"6c2dcb801bf2b75c18f1bb24140e7cf57465cc4d","analyzedAt":"2026-08-15T07:12:03.035Z","schemaVersion":2},"datasetVersion":"2026-08-15T22:17:37.221Z"}