{"record":{"id":"a9d178ac37d45c47","repo":"langchain-ai/langchain","slug":"invalid-input-type-type-model-input-must-be-a","errorCode":null,"errorMessage":"Invalid input type {type(model_input)}. Must be a PromptValue, str, or list of BaseMessages.","messagePattern":"Invalid input type (.+?)\\. Must be a PromptValue, str, or list of BaseMessages\\.","errorType":"exception","errorClass":"ValueError","httpStatus":null,"severity":"error","filePath":"libs/core/langchain_core/language_models/chat_models.py","lineNumber":460,"sourceCode":"\n    @property\n    @override\n    def OutputType(self) -> Any:\n        \"\"\"Get the output type for this `Runnable`.\"\"\"\n        return AnyMessage\n\n    def _convert_input(self, model_input: LanguageModelInput) -> PromptValue:\n        if isinstance(model_input, PromptValue):\n            return model_input\n        if isinstance(model_input, str):\n            return StringPromptValue(text=model_input)\n        if isinstance(model_input, Sequence):\n            return ChatPromptValue(messages=convert_to_messages(model_input))\n        msg = (  # type: ignore[unreachable]\n            f\"Invalid input type {type(model_input)}. \"\n            \"Must be a PromptValue, str, or list of BaseMessages.\"\n        )\n        raise ValueError(msg)\n\n    @override\n    def invoke(\n        self,\n        input: LanguageModelInput,\n        config: RunnableConfig | None = None,\n        *,\n        stop: list[str] | None = None,\n        **kwargs: Any,\n    ) -> AIMessage:\n        config = ensure_config(config)\n        return cast(\n            \"AIMessage\",\n            cast(\n                \"ChatGeneration\",\n                self.generate_prompt(\n                    [self._convert_input(input)],\n                    stop=stop,","sourceCodeStart":442,"sourceCodeEnd":478,"githubUrl":"https://github.com/langchain-ai/langchain/blob/e32fa9a52eab3b61ad7a45399bfde59b3e580fc4/libs/core/langchain_core/language_models/chat_models.py#L442-L478","documentation":"`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.","triggerScenarios":"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`.","commonSituations":"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.","solutions":["Wrap a single message in a list: `model.invoke([msg])` instead of `model.invoke(msg)`.","Convert dict-based message payloads with `convert_to_messages` or construct proper `BaseMessage` objects before calling.","Materialize generators/iterables into a list before passing them in.","Type-annotate call sites as `LanguageModelInput` so static checkers catch mistakes."],"exampleFix":"# before\nresp = model.invoke(SystemMessage(content=\"hi\"))\n\n# after\nresp = model.invoke([SystemMessage(content=\"hi\")])","handlingStrategy":"type-guard","validationCode":"from collections.abc import Sequence\nfrom langchain_core.prompt_values import PromptValue\nif not isinstance(model_input, (str, PromptValue, Sequence)):\n    model_input = [model_input]  # wrap single messages","typeGuard":"def is_valid_chat_input(value: object) -> bool:\n    return isinstance(value, (str, PromptValue)) or isinstance(value, Sequence)","tryCatchPattern":"try:\n    resp = model.invoke(user_input)\nexcept ValueError as e:\n    if \"Invalid input type\" in str(e):\n        resp = model.invoke([user_input])\n    else:\n        raise","preventionTips":["Always wrap single messages in a list.","Annotate variables passed to `invoke` as `LanguageModelInput`.","Never pass raw state dicts; extract `state[\"messages\"]` first."],"tags":["input-validation","messages","type-mismatch"],"backgroundTag":null,"analyzedSha":"e32fa9a52eab3b61ad7a45399bfde59b3e580fc4","analyzedAt":"2026-08-14T18:42:09.092Z","schemaVersion":2},"datasetVersion":"2026-08-15T22:17:37.221Z"}