BerriAI/litellm · error · OpenAIError
Missing model or messages
Error message
Missing model or messages
What it means
The OpenAI text-completion handler validates that both `model` and `messages` are provided before building the request. If either is None, it raises OpenAIError 422 'Missing model or messages' immediately — this handler is the legacy /v1/completions text path, which still requires the messages/prompt source to be present.
Source
Thrown at litellm/llms/openai/completion/handler.py:58
custom_llm_provider: str,
logging_obj: LiteLLMLoggingObj,
optional_params: dict,
print_verbose: Callable | None = None,
api_base: str | None = None,
acompletion: bool = False,
litellm_params=None,
logger_fn=None,
client=None,
organization: str | None = None,
headers: dict | None = None,
):
try:
if headers:
optional_params = {**optional_params, "extra_headers": headers}
if headers is None:
headers = self.validate_environment(api_key=api_key)
if model is None or messages is None:
raise OpenAIError(status_code=422, message="Missing model or messages")
# don't send max retries to the api, if set
provider_config: Final = ProviderConfigManager.get_provider_text_completion_config(
model=model,
provider=LlmProviders(custom_llm_provider),
)
data: Final = provider_config.transform_text_completion_request(
model=model,
messages=messages,
optional_params=optional_params,
headers=headers,
)
max_retries: Final = data.pop("max_retries", 2)
## LOGGING
logging_obj.pre_call(
input=messages,View on GitHub (pinned to 6c2dcb801b)
Solutions
- Check the calling code: ensure the model string resolved (not None) and messages are passed.
- Default model from env: `model = model or os.environ['OPENAI_MODEL']` with explicit failure if unset.
- If you meant to send a raw prompt, use the appropriate text-completion parameter form rather than messages=None.
Example fix
# before
litellm.text_completion(model=None, prompt="hi") # model never resolved
# after
model = os.environ.get("OPENAI_MODEL")
if model is None:
raise ValueError("OPENAI_MODEL not set")
litellm.text_completion(model=model, prompt="hi") Defensive patterns
Strategy: type-guard
Validate before calling
if not model or not messages:
raise ValueError(f"model and messages are required (got model={model!r})") Type guard
def has_model_and_messages(model, messages) -> bool:
return isinstance(model, str) and bool(model) and messages is not None Prevention
- Resolve model names from config with explicit KeyError on missing keys
- Assert required args at the top of wrapper functions
- Fail fast on None params instead of relying on the library's 422
When it happens
Trigger: Calling the text-completion entrypoint (openai text completion, e.g. via `litellm.completion` internals or provider text-completion routes) with model=None (e.g. a variable that failed to resolve) or messages=None.
Common situations: Programmatic callers building params dynamically where the model name variable is None (unset env/config), or wrappers that pass prompt strings on a path that expects messages. Almost always a caller bug, not a server issue.
Related errors
- Braintrust API error: {e.response.text}
- Failed to connect to Braintrust API: {str(e)}
- Failed to parse Braintrust API response: {str(e)}
- error_text
- Unclassified keys in {PRICES_PATH.name}: {', '.join(unclassi
AI-assisted analysis of BerriAI/litellm@6c2dcb801b (2026-08-15).
Data as JSON: /api/errors/8d84c5b835041944.
Report an issue: GitHub.