{"record":{"id":"37acf301bcefff14","repo":"langchain-ai/langchain","slug":"messages-list-cannot-be-empty","errorCode":null,"errorMessage":"messages list cannot be empty.","messagePattern":"messages list cannot be empty\\.","errorType":"exception","errorClass":"ValueError","httpStatus":null,"severity":"error","filePath":"libs/core/langchain_core/language_models/fake_chat_models.py","lineNumber":391,"sourceCode":"\nclass ParrotFakeChatModel(BaseChatModel):\n    \"\"\"Generic fake chat model that can be used to test the chat model interface.\n\n    * Chat model should be usable in both sync and async tests\n\n    \"\"\"\n\n    @override\n    def _generate(\n        self,\n        messages: list[BaseMessage],\n        stop: list[str] | None = None,\n        run_manager: CallbackManagerForLLMRun | None = None,\n        **kwargs: Any,\n    ) -> ChatResult:\n        if not messages:\n            msg = \"messages list cannot be empty.\"\n            raise ValueError(msg)\n        return ChatResult(generations=[ChatGeneration(message=messages[-1])])\n\n    @property\n    def _llm_type(self) -> str:\n        return \"parrot-fake-chat-model\"\n","sourceCodeStart":373,"sourceCodeEnd":397,"githubUrl":"https://github.com/langchain-ai/langchain/blob/e32fa9a52eab3b61ad7a45399bfde59b3e580fc4/libs/core/langchain_core/language_models/fake_chat_models.py#L373-L397","documentation":"`ValueError` from `ParrotFakeChatModel._generate`: it echoes the last input message back, which requires a non-empty `messages` list. Calling it with `[]` has nothing to echo, so it fails fast rather than returning an empty result.","triggerScenarios":"Invoking `ParrotFakeChatModel` (or a subclass) with an empty messages list — `model.invoke([])` or an internal pipeline passing zero messages (e.g. empty history and empty prompt merged away).","commonSituations":"LangGraph nodes passing `state[\"messages\"]` when the state is empty; list comprehensions/generators that filter out all messages; tests exercising empty-input edge cases.","solutions":["Ensure at least one message reaches the model: default to `[HumanMessage(content=\"\")]` when the list is empty.","Fix the upstream graph/prompt so empty conversations short-circuit before the LLM call.","Skip the LLM node entirely when history is empty."],"exampleFix":"# before\nresp = model.invoke(state[\"messages\"])  # [] -> ValueError\n\n# after\nmsgs = state[\"messages\"] or [HumanMessage(content=\"hello\")]\nresp = model.invoke(msgs)","handlingStrategy":"validation","validationCode":"if not messages:\n    messages = [HumanMessage(content=\"fallback prompt\")]\nresp = parrot_model.invoke(messages)","typeGuard":null,"tryCatchPattern":"try:\n    resp = parrot_model.invoke(messages)\nexcept ValueError as e:\n    if \"cannot be empty\" in str(e):\n        resp = parrot_model.invoke([HumanMessage(content=\"hello\")])\n    else:\n        raise","preventionTips":["Default to a placeholder `HumanMessage` when message lists can be empty.","Short-circuit graph nodes on empty state before calling models.","Validate non-empty messages in shared pipeline helpers."],"tags":["fake-model","testing","empty-input","validation"],"backgroundTag":null,"analyzedSha":"e32fa9a52eab3b61ad7a45399bfde59b3e580fc4","analyzedAt":"2026-08-14T18:42:09.092Z","schemaVersion":2},"datasetVersion":"2026-08-15T17:31:12.345Z"}