langchain-ai/langchain · error · ValueError

messages list cannot be empty.

Error message

messages list cannot be empty.

What it means

`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.

Source

Thrown at libs/core/langchain_core/language_models/fake_chat_models.py:391

class ParrotFakeChatModel(BaseChatModel):
    """Generic fake chat model that can be used to test the chat model interface.

    * Chat model should be usable in both sync and async tests

    """

    @override
    def _generate(
        self,
        messages: list[BaseMessage],
        stop: list[str] | None = None,
        run_manager: CallbackManagerForLLMRun | None = None,
        **kwargs: Any,
    ) -> ChatResult:
        if not messages:
            msg = "messages list cannot be empty."
            raise ValueError(msg)
        return ChatResult(generations=[ChatGeneration(message=messages[-1])])

    @property
    def _llm_type(self) -> str:
        return "parrot-fake-chat-model"

View on GitHub (pinned to e32fa9a52e)

Solutions

  1. Ensure at least one message reaches the model: default to `[HumanMessage(content="")]` when the list is empty.
  2. Fix the upstream graph/prompt so empty conversations short-circuit before the LLM call.
  3. Skip the LLM node entirely when history is empty.

Example fix

# before
resp = model.invoke(state["messages"])  # [] -> ValueError

# after
msgs = state["messages"] or [HumanMessage(content="hello")]
resp = model.invoke(msgs)
Defensive patterns

Strategy: validation

Validate before calling

if not messages:
    messages = [HumanMessage(content="fallback prompt")]
resp = parrot_model.invoke(messages)

Try / catch

try:
    resp = parrot_model.invoke(messages)
except ValueError as e:
    if "cannot be empty" in str(e):
        resp = parrot_model.invoke([HumanMessage(content="hello")])
    else:
        raise

Prevention

When it happens

Trigger: 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).

Common situations: LangGraph nodes passing `state["messages"]` when the state is empty; list comprehensions/generators that filter out all messages; tests exercising empty-input edge cases.

Related errors


AI-assisted analysis of langchain-ai/langchain@e32fa9a52e (2026-08-14). Data as JSON: /api/errors/37acf301bcefff14. Report an issue: GitHub.