{"record":{"id":"8d84c5b835041944","repo":"BerriAI/litellm","slug":"missing-model-or-messages-8d84c5","errorCode":null,"errorMessage":"Missing model or messages","messagePattern":"Missing model or messages","errorType":"exception","errorClass":"OpenAIError","httpStatus":422,"severity":"error","filePath":"litellm/llms/openai/completion/handler.py","lineNumber":58,"sourceCode":"        custom_llm_provider: str,\n        logging_obj: LiteLLMLoggingObj,\n        optional_params: dict,\n        print_verbose: Callable | None = None,\n        api_base: str | None = None,\n        acompletion: bool = False,\n        litellm_params=None,\n        logger_fn=None,\n        client=None,\n        organization: str | None = None,\n        headers: dict | None = None,\n    ):\n        try:\n            if headers:\n                optional_params = {**optional_params, \"extra_headers\": headers}\n            if headers is None:\n                headers = self.validate_environment(api_key=api_key)\n            if model is None or messages is None:\n                raise OpenAIError(status_code=422, message=\"Missing model or messages\")\n\n            # don't send max retries to the api, if set\n\n            provider_config: Final = ProviderConfigManager.get_provider_text_completion_config(\n                model=model,\n                provider=LlmProviders(custom_llm_provider),\n            )\n\n            data: Final = provider_config.transform_text_completion_request(\n                model=model,\n                messages=messages,\n                optional_params=optional_params,\n                headers=headers,\n            )\n            max_retries: Final = data.pop(\"max_retries\", 2)\n            ## LOGGING\n            logging_obj.pre_call(\n                input=messages,","sourceCodeStart":40,"sourceCodeEnd":76,"githubUrl":"https://github.com/BerriAI/litellm/blob/6c2dcb801bf2b75c18f1bb24140e7cf57465cc4d/litellm/llms/openai/completion/handler.py#L40-L76","documentation":"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.","triggerScenarios":"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.","commonSituations":"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.","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."],"exampleFix":"# before\nlitellm.text_completion(model=None, prompt=\"hi\")  # model never resolved\n\n# after\nmodel = os.environ.get(\"OPENAI_MODEL\")\nif model is None:\n    raise ValueError(\"OPENAI_MODEL not set\")\nlitellm.text_completion(model=model, prompt=\"hi\")","handlingStrategy":"type-guard","validationCode":"if not model or not messages:\n    raise ValueError(f\"model and messages are required (got model={model!r})\")","typeGuard":"def has_model_and_messages(model, messages) -> bool:\n    return isinstance(model, str) and bool(model) and messages is not None","tryCatchPattern":null,"preventionTips":["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"],"tags":["openai","text-completion","input-validation"],"backgroundTag":null,"analyzedSha":"6c2dcb801bf2b75c18f1bb24140e7cf57465cc4d","analyzedAt":"2026-08-15T07:12:03.035Z","schemaVersion":2},"datasetVersion":"2026-08-15T22:17:37.221Z"}