BerriAI/litellm · error · TextCompletionCodestralError
HTTPStatusError
Error message
HTTPStatusError - {e.response.text} What it means
TextCompletionCodestralError raised when the async POST to the Codestral text-completion endpoint returns an error status (raise_for_status fired). The message embeds the upstream response body, so the exact provider error — invalid model, bad credentials, malformed request — is visible. The upstream status code is preserved on the exception.
Solutions
- Read the embedded response text and status: 401 -> refresh the key, 404 -> fix the model name, 422 -> fix payload fields, 429 -> back off
- Verify the model identifier matches one currently offered on La Plateforme (e.g. codestral-latest)
- Add retry with exponential backoff for 429/5xx and cap concurrency
- Ensure the request body matches the text-completion schema (prompt, suffix, stop, temperature) — not chat messages
Example fix
# before
resp = await litellm.atext_completion(
model="text-completion-codestral", prompt="def fib(", temperature=2.5
)
# after
resp = await litellm.atext_completion(
model="text-completion-codestral", prompt="def fib(", temperature=0.7
) Defensive patterns
Strategy: try-catch
Try / catch
from litellm.llms.codestral.completion.handler import TextCompletionCodestralError
try:
resp = await litellm.atext_completion(model="text-completion-codestral", prompt=prompt)
except TextCompletionCodestralError as e:
if e.status_code == 429 or e.status_code >= 500:
await asyncio.sleep(2 ** attempt)
resp = await litellm.atext_completion(model="text-completion-codestral", prompt=prompt)
elif e.status_code == 401:
raise RuntimeError("Codestral API key invalid or expired") from e
elif e.status_code == 404:
raise RuntimeError("Unknown codestral model id") from e
else:
raise Prevention
- Verify model ids against La Plateforme before shipping (404s are cheap to prevent)
- Wrap calls in bounded retry with backoff for 429/5xx only
- Keep request bodies within the text-completion schema; do not send chat-only fields
When it happens
Trigger: POSTing to the Codestral completion API with an invalid model name, invalid/expired key (401), malformed request body (422), rate limit (429), or upstream 5xx.
Common situations: Model renamed/removed by Mistral (e.g. codestral-latest variants); expired API key; prompt exceeding context limits; bursts hitting rate limits; sending chat-format fields to the text-completion endpoint.
Understand the failure class
Background: "API error: {status}" and "HTTP 401/403/404/429/5xx" errors: non-2xx HTTP responses explained — this error's family across 27 libraries.
Related errors
- Missing CODESTRAL_API_Key - Please add CODESTRAL_API_Key to…
- api_base not set. Set api_base or litellm.api_base for…
- Batch record for /v1/completions is missing required…
- DataDogLLMObs: Unexpected response - status_code
- DeepEval logging error
AI-assisted analysis of BerriAI/litellm@77b7c6c40c (2026-08-18).
Data as JSON: /api/errors/4d2aa997872e5819.
Report an issue: GitHub.
Appendix: source
Thrown at litellm/llms/codestral/completion/handler.py:354
encoding,
api_key,
logging_obj,
stream,
data: dict,
optional_params: dict,
timeout: float | httpx.Timeout,
litellm_params=None,
logger_fn=None,
headers={},
) -> TextCompletionResponse:
async_handler: Final = get_async_httpx_client(
llm_provider=litellm.LlmProviders.TEXT_COMPLETION_CODESTRAL,
params={"timeout": timeout},
)
try:
response: Final = await async_handler.post(api_base, headers=headers, data=json.dumps(data))
except httpx.HTTPStatusError as e:
raise TextCompletionCodestralError(
status_code=e.response.status_code,
message=f"HTTPStatusError - {e.response.text}",
)
except Exception as e:
raise TextCompletionCodestralError(
status_code=500, message=f"{e}"
) # don't use verbose_logger.exception, if exception is raised
return self.process_text_completion_response(
model=model,
response=response,
model_response=model_response,
stream=stream,
logging_obj=logging_obj,
api_key=api_key,
data=data,
messages=messages,
print_verbose=print_verbose,
optional_params=optional_params,View on GitHub (pinned to 77b7c6c40c)