{"record":{"id":"ff8b043fd033f8d9","repo":"langchain-ai/deepagents","slug":"system-prompt-must-contain-the-agent-memory-fo","errorCode":null,"errorMessage":"system_prompt must contain the `{agent_memory}` format slot","messagePattern":"system_prompt must contain the `(.+?)` format slot","errorType":"validation","errorClass":"ValueError","httpStatus":null,"severity":"error","filePath":"libs/deepagents/deepagents/middleware/memory.py","lineNumber":239,"sourceCode":"                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\n        Returns:\n            Formatted string with location+content pairs substituted into\n            the supplied template.","sourceCodeStart":221,"sourceCodeEnd":257,"githubUrl":"https://github.com/langchain-ai/deepagents/blob/a1af029e6e73cb17c36bff823d227747b28e91e1/libs/deepagents/deepagents/middleware/memory.py#L221-L257","documentation":"A string system_prompt for MemoryMiddleware must contain the `{agent_memory}` format slot, which is where retrieved memory contents get injected. A string without the slot raises ValueError because memory would never appear in the prompt.","triggerScenarios":"MemoryMiddleware(system_prompt=\"You are a helpful assistant.\") — any custom string missing the literal substring \"{agent_memory}\".","commonSituations":"Writing a custom system prompt and forgetting the injection point; copying a generic prompt from elsewhere; an editor or formatter stripping the braces.","solutions":["Add \"{agent_memory}\" somewhere in the prompt string where memory should be inserted","Pass system_prompt=None to use the default MEMORY_SYSTEM_PROMPT template"],"exampleFix":"// before\nmw = MemoryMiddleware(system_prompt=\"You are a helpful assistant.\")\n// after\nmw = MemoryMiddleware(system_prompt=\"You are a helpful assistant.\\n\\nYour memories:\\n{agent_memory}\")","handlingStrategy":"validation","validationCode":"def check_prompt_slot(p):\n    if isinstance(p, str) and \"{agent_memory}\" not in p:\n        raise ValueError(\"system_prompt must contain the `{agent_memory}` format slot\")\n    return p","typeGuard":"def has_memory_slot(p) -> bool:\n    return p is None or (isinstance(p, str) and \"{agent_memory}\" in p)","tryCatchPattern":"try:\n    mw = MemoryMiddleware(system_prompt=prompt)\nexcept ValueError as e:\n    if \"agent_memory\" in str(e):\n        prompt = prompt + \"\\n\\n{agent_memory}\"\n        mw = MemoryMiddleware(system_prompt=prompt)\n    else:\n        raise","preventionTips":["Keep the {agent_memory} slot in every custom memory prompt template","Add a unit test asserting the slot exists in your configured prompt","Prefer system_prompt=None (default template) unless customization is required"],"tags":["python","validation","memory","prompt-template"],"backgroundTag":"missing-format-placeholder","analyzedSha":"a1af029e6e73cb17c36bff823d227747b28e91e1","analyzedAt":"2026-08-29T11:43:24.718Z","schemaVersion":2},"datasetVersion":"2026-08-29T12:17:43.993Z"}