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
- 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.
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
- 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.
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
- Expected generate to return a ChatResult, but got {type(chat
- Expected invoke to return an AIMessage, but got {type(messag
- Expected content to be a string.
- invalid IP address
- Failed to resolve hostname '{hostname}': {e}
AI-assisted analysis of langchain-ai/langchain@e32fa9a52e (2026-08-14).
Data as JSON: /api/errors/37acf301bcefff14.
Report an issue: GitHub.