{"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":"exception","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/77b7c6c40c0c5aa5fbcb1d6a1825ac39ca8829b8/litellm/llms/bytez/chat/transformation.py#L117-L153","documentation":"Bytez's validate_environment raises this generic Exception when the messages kwarg is empty (falsy). It is a request-shape guard: Bytez needs at least one message to build the chat request. Note it fires before the api_key check in the same method, so an empty-messages call reports this rather than the missing-key error.","triggerScenarios":"litellm.completion(model=\"bytez/...\", messages=[]) or messages=None; templates/chat-history slicing that accidentally yields an empty list (e.g., trimming context too aggressively); passing conversation history that failed to populate from a DB.","commonSituations":"Dynamic prompt builders trimming history to zero turns; early-exit branches that still call the LLM; None default leaking through instead of a user message.","solutions":["Ensure at least one message, typically a user turn, before calling: guard with `if not messages: append fallback`.","Fix the upstream slicing/filtering that emptied the list.","Add a unit test on your prompt builder asserting non-empty messages."],"exampleFix":"# before\nmessages = [m for m in history if keep(m)]  # can become []\nlitellm.completion(model=\"bytez/...\", messages=messages)\n\n# after\nmessages = [m for m in history if keep(m)] or [{\"role\": \"user\", \"content\": \"Hello\"}]\nlitellm.completion(model=\"bytez/...\", messages=messages)","handlingStrategy":"validation","validationCode":"def has_messages(messages) -> bool:\n    return isinstance(messages, list) and len(messages) > 0\n\nassert has_messages(messages), \"refusing to call bytez with empty messages\"","typeGuard":"from typing import Any\n\ndef is_nonempty_message_list(messages: Any) -> bool:\n    return (\n        isinstance(messages, list)\n        and len(messages) > 0\n        and all(isinstance(m, dict) and m.get(\"role\") and \"content\" in m for m in messages)\n    )","tryCatchPattern":"try:\n    litellm.completion(model=\"bytez/org/model\", messages=messages)\nexcept Exception as e:\n    if \"messages\" in str(e) and not messages:\n        messages = [{\"role\": \"user\", \"content\": \"Hello\"}]\n        litellm.completion(model=\"bytez/org/model\", messages=messages)\n    else:\n        raise","preventionTips":["Guard prompt builders: return a fallback user message instead of an empty list.","Assert non-empty messages in tests of your chat pipeline."],"tags":["bytez","messages","validation","empty-input"],"backgroundTag":"empty-messages-array","analyzedSha":"77b7c6c40c0c5aa5fbcb1d6a1825ac39ca8829b8","analyzedAt":"2026-08-18T11:44:31.656Z","contentChangedAt":null,"schemaVersion":2},"datasetVersion":"2026-09-23T08:17:48.524Z"}