{"record":{"id":"12f9aec9f80d3005","repo":"BerriAI/litellm","slug":"kwarg-messages-must-be-an-array-of-messages-that","errorCode":null,"errorMessage":"kwarg `messages` must be an array of messages that follow the openai chat standard","messagePattern":"kwarg `messages` must be an array of messages that follow the openai chat standard","errorType":"validation","errorClass":"Exception","httpStatus":null,"severity":"error","filePath":"litellm/llms/bytez/chat/transformation.py","lineNumber":135,"sourceCode":"        self,\n        headers: dict,\n        model: str,\n        messages: list[AllMessageValues],\n        optional_params: dict,\n        litellm_params: dict,\n        api_key: str | None = None,\n        api_base: str | None = None,\n    ) -> dict:\n        headers.update(\n            {\n                \"content-type\": \"application/json\",\n                \"Authorization\": f\"Key {api_key}\",\n                \"user-agent\": f\"litellm/{version}\",\n            }\n        )\n\n        if not messages:\n            raise Exception(\"kwarg `messages` must be an array of messages that follow the openai chat standard\")\n\n        if not api_key:\n            raise Exception(\"Missing api_key, make sure you pass in your api key\")\n\n        return headers\n\n    def get_complete_url(\n        self,\n        api_base: str | None,\n        api_key: str | None,\n        model: str,\n        optional_params: dict,\n        litellm_params: dict,\n        stream: bool | None = None,\n    ) -> str:\n        encoded_model: Final = encode_url_path_segments(model, field_name=\"model\")\n        return f\"{API_BASE}/{encoded_model}\"\n","sourceCodeStart":117,"sourceCodeEnd":153,"githubUrl":"https://github.com/BerriAI/litellm/blob/6c2dcb801bf2b75c18f1bb24140e7cf57465cc4d/litellm/llms/bytez/chat/transformation.py#L117-L153","documentation":"Bytez's validate_environment asserts that the messages list is non-empty before building headers; an empty (or None) messages list raises this Exception. It is a client-side precondition mirroring the OpenAI chat contract, raised before any HTTP request is made.","triggerScenarios":"litellm.completion(model=\"bytez/...\", messages=[]) or messages=None — typically from dynamic conversation builders that produce an empty history (e.g. trimmed context, empty user input).","commonSituations":"Chat apps that trim messages aggressively until none remain; feeding an empty list when a user submits blank input; data pipelines batching conversations where some conversations are empty.","solutions":["Check messages is a non-empty list before calling and skip/short-circuit empty conversations.","Ensure at least one user message exists by construction in your chat loop.","Log the input right before the call to find where the empty list originates."],"exampleFix":"# before\nresp = litellm.completion(model=model, messages=session.get(\"messages\", []))\n\n# after\nmsgs = session.get(\"messages\", [])\nif not msgs:\n    return \"Please say something first.\"\nresp = litellm.completion(model=model, messages=msgs)","handlingStrategy":"validation","validationCode":"if not isinstance(messages, list) or len(messages) == 0:\n    raise ValueError(\"messages must be a non-empty list before calling the model\")","typeGuard":"def has_messages(messages: object) -> bool:\n    return isinstance(messages, list) and len(messages) > 0 and all(\n        isinstance(m, dict) and m.get(\"role\") and m.get(\"content\") is not None for m in messages\n    )","tryCatchPattern":"try:\n    litellm.completion(model=\"bytez/...\", messages=msgs)\nexcept Exception as e:\n    if \"must be an array of messages\" in str(e):\n        msgs = msgs or [{\"role\": \"user\", \"content\": fallback_prompt}]\n        litellm.completion(model=\"bytez/...\", messages=msgs)\n    else:\n        raise","preventionTips":["Short-circuit empty conversations in the chat loop before calling the model.","Add a request-sanitization layer asserting non-empty messages.","Never pass session.get('messages', []) straight to completion without a truthiness check."],"tags":["bytez","messages","validation","input"],"backgroundTag":null,"analyzedSha":"6c2dcb801bf2b75c18f1bb24140e7cf57465cc4d","analyzedAt":"2026-08-15T07:12:03.035Z","schemaVersion":2},"datasetVersion":"2026-08-15T22:17:37.221Z"}