{"record":{"id":"a2bc41d3712d8575","repo":"langchain-ai/deepagents","slug":"system-prompt-must-be-str-or-none-got-type-syste","errorCode":null,"errorMessage":"system_prompt must be str or None, got {type(system_prompt).__name__}","messagePattern":"system_prompt must be str or None, got (.+?)","errorType":"validation","errorClass":"TypeError","httpStatus":null,"severity":"error","filePath":"libs/deepagents/deepagents/middleware/memory.py","lineNumber":236,"sourceCode":"                turns (memory content would otherwise shift after every update\n                and invalidate the prefix cache).\n\n                No-ops on non-Anthropic models; Bedrock and Vertex wrappers do\n                not qualify.\n            system_prompt: System-prompt fragment template. Must contain a\n                `{agent_memory}` slot for runtime memory substitution. Pass\n                `None` to skip appending entirely (memory is still loaded\n                into `state[\"memory_contents\"]`).\n\n        Raises:\n            TypeError: If `system_prompt` is not `str` or `None`.\n            ValueError: If `system_prompt` is a string missing the\n                `{agent_memory}` format slot.\n        \"\"\"\n        if system_prompt is not None:\n            if not isinstance(system_prompt, str):\n                msg = f\"system_prompt must be str or None, got {type(system_prompt).__name__}\"\n                raise TypeError(msg)\n            if \"{agent_memory}\" not in system_prompt:\n                msg = \"system_prompt must contain the `{agent_memory}` format slot\"\n                raise ValueError(msg)\n        self._backend = backend\n        self.sources = sources\n        self._add_cache_control = add_cache_control\n        self.system_prompt = system_prompt\n\n    def _format_agent_memory(self, contents: dict[str, str], template: str = MEMORY_SYSTEM_PROMPT) -> str:\n        \"\"\"Format memory with locations and contents paired together.\n\n        Substitutes loaded memory into the `{agent_memory}` slot of the\n        supplied template.\n\n        Args:\n            contents: Dict mapping source paths to content.\n            template: Surrounding template; must contain `{agent_memory}`.\n","sourceCodeStart":218,"sourceCodeEnd":254,"githubUrl":"https://github.com/langchain-ai/deepagents/blob/a1af029e6e73cb17c36bff823d227747b28e91e1/libs/deepagents/deepagents/middleware/memory.py#L218-L254","documentation":"MemoryMiddleware's `system_prompt` must be a str or None. A value of any other type (dict, list, object, etc.) raises TypeError in `__init__`. The prompt is later used for string formatting, so non-string values cannot be accepted.","triggerScenarios":"Passing system_prompt=<non-string>, e.g. a dict of prompt parts, a LangChain prompt template object, or bytes, to MemoryMiddleware(system_prompt=...).","commonSituations":"Reusing an existing prompt-template object (e.g. ChatPromptTemplate or a config dict) instead of rendering it to a string first.","solutions":["Pass a plain string (containing the `{agent_memory}` slot) or None","Render your template object to a string before passing it, e.g. template.format(...) or str(...)"],"exampleFix":"// before\nmw = MemoryMiddleware(system_prompt=ChatPromptTemplate.from_messages([...]))\n// after\nmw = MemoryMiddleware(system_prompt=\"... {agent_memory} ...\")","handlingStrategy":"type-guard","validationCode":"def check_system_prompt(p):\n    if p is not None and not isinstance(p, str):\n        raise TypeError(f\"system_prompt must be str or None, got {type(p).__name__}\")\n    return p","typeGuard":"def is_str_or_none(v) -> bool:\n    return v is None or isinstance(v, str)","tryCatchPattern":"try:\n    mw = MemoryMiddleware(system_prompt=prompt)\nexcept TypeError as e:\n    prompt = str(prompt)  # or render your template to a string\n    mw = MemoryMiddleware(system_prompt=prompt)","preventionTips":["Add type annotations (str | None) where prompts are configured","Render template objects to strings before passing them to middleware","Reject non-string prompts at config-load time"],"tags":["python","type-error","memory","prompt"],"backgroundTag":"invalid-argument-type","analyzedSha":"a1af029e6e73cb17c36bff823d227747b28e91e1","analyzedAt":"2026-08-29T11:43:24.718Z","schemaVersion":2},"datasetVersion":"2026-08-29T12:17:43.993Z"}