langchain-ai/langchain · error · ValueError

Invalid input type {type(model_input)}. Must be a PromptValu

Error message

Invalid input type {type(model_input)}. Must be a PromptValue, str, or list of BaseMessages.

What it means

`ValueError` from `BaseChatModel._convert_input`: the `invoke`/`generate` input must be a `PromptValue`, a `str`, or a `Sequence` (list) of messages. Any other Python object (dict, generator, single `BaseMessage`, `None`, int...) hits the unreachable-typed fallback and is rejected.

Source

Thrown at libs/core/langchain_core/language_models/chat_models.py:460

    @property
    @override
    def OutputType(self) -> Any:
        """Get the output type for this `Runnable`."""
        return AnyMessage

    def _convert_input(self, model_input: LanguageModelInput) -> PromptValue:
        if isinstance(model_input, PromptValue):
            return model_input
        if isinstance(model_input, str):
            return StringPromptValue(text=model_input)
        if isinstance(model_input, Sequence):
            return ChatPromptValue(messages=convert_to_messages(model_input))
        msg = (  # type: ignore[unreachable]
            f"Invalid input type {type(model_input)}. "
            "Must be a PromptValue, str, or list of BaseMessages."
        )
        raise ValueError(msg)

    @override
    def invoke(
        self,
        input: LanguageModelInput,
        config: RunnableConfig | None = None,
        *,
        stop: list[str] | None = None,
        **kwargs: Any,
    ) -> AIMessage:
        config = ensure_config(config)
        return cast(
            "AIMessage",
            cast(
                "ChatGeneration",
                self.generate_prompt(
                    [self._convert_input(input)],
                    stop=stop,

View on GitHub (pinned to e32fa9a52e)

Solutions

  1. Wrap a single message in a list: `model.invoke([msg])` instead of `model.invoke(msg)`.
  2. Convert dict-based message payloads with `convert_to_messages` or construct proper `BaseMessage` objects before calling.
  3. Materialize generators/iterables into a list before passing them in.
  4. Type-annotate call sites as `LanguageModelInput` so static checkers catch mistakes.

Example fix

# before
resp = model.invoke(SystemMessage(content="hi"))

# after
resp = model.invoke([SystemMessage(content="hi")])
Defensive patterns

Strategy: type-guard

Validate before calling

from collections.abc import Sequence
from langchain_core.prompt_values import PromptValue
if not isinstance(model_input, (str, PromptValue, Sequence)):
    model_input = [model_input]  # wrap single messages

Type guard

def is_valid_chat_input(value: object) -> bool:
    return isinstance(value, (str, PromptValue)) or isinstance(value, Sequence)

Try / catch

try:
    resp = model.invoke(user_input)
except ValueError as e:
    if "Invalid input type" in str(e):
        resp = model.invoke([user_input])
    else:
        raise

Prevention

When it happens

Trigger: Calling `chat_model.invoke(...)` / `generate(...)` with input that is not `str`, `PromptValue`, or a sequence — typical cases: a bare `SystemMessage` (not wrapped in a list), a dict like `{"messages": [...]}`, a generator expression, or `None`.

Common situations: Passing a single message instead of `[message]`; feeding LangGraph state dicts directly to `.invoke`; migrating code that expected dicts; passing `None` from an upstream empty branch.

Related errors


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