langchain-ai/deepagents · error · TypeError
system_prompt must be str or None, got {type(system_prompt).
Error message
system_prompt must be str or None, got {type(system_prompt).__name__} What it means
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.
Source
Thrown at libs/deepagents/deepagents/middleware/memory.py:236
turns (memory content would otherwise shift after every update
and invalidate the prefix cache).
No-ops on non-Anthropic models; Bedrock and Vertex wrappers do
not qualify.
system_prompt: System-prompt fragment template. Must contain a
`{agent_memory}` slot for runtime memory substitution. Pass
`None` to skip appending entirely (memory is still loaded
into `state["memory_contents"]`).
Raises:
TypeError: If `system_prompt` is not `str` or `None`.
ValueError: If `system_prompt` is a string missing the
`{agent_memory}` format slot.
"""
if system_prompt is not None:
if not isinstance(system_prompt, str):
msg = f"system_prompt must be str or None, got {type(system_prompt).__name__}"
raise TypeError(msg)
if "{agent_memory}" not in system_prompt:
msg = "system_prompt must contain the `{agent_memory}` format slot"
raise ValueError(msg)
self._backend = backend
self.sources = sources
self._add_cache_control = add_cache_control
self.system_prompt = system_prompt
def _format_agent_memory(self, contents: dict[str, str], template: str = MEMORY_SYSTEM_PROMPT) -> str:
"""Format memory with locations and contents paired together.
Substitutes loaded memory into the `{agent_memory}` slot of the
supplied template.
Args:
contents: Dict mapping source paths to content.
template: Surrounding template; must contain `{agent_memory}`.
View on GitHub (pinned to a1af029e6e)
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(...)
Example fix
// before
mw = MemoryMiddleware(system_prompt=ChatPromptTemplate.from_messages([...]))
// after
mw = MemoryMiddleware(system_prompt="... {agent_memory} ...") Defensive patterns
Strategy: type-guard
Validate before calling
def check_system_prompt(p):
if p is not None and not isinstance(p, str):
raise TypeError(f"system_prompt must be str or None, got {type(p).__name__}")
return p Type guard
def is_str_or_none(v) -> bool:
return v is None or isinstance(v, str) Try / catch
try:
mw = MemoryMiddleware(system_prompt=prompt)
except TypeError as e:
prompt = str(prompt) # or render your template to a string
mw = MemoryMiddleware(system_prompt=prompt) Prevention
- 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
When it happens
Trigger: Passing system_prompt=<non-string>, e.g. a dict of prompt parts, a LangChain prompt template object, or bytes, to MemoryMiddleware(system_prompt=...).
Common situations: Reusing an existing prompt-template object (e.g. ChatPromptTemplate or a config dict) instead of rendering it to a string first.
Understand the failure class
Background: Invalid argument type errors: "must be of type string", "expected X, got Y", and ERR_INVALID_ARG_TYPE explained — this error's family across 15 libraries.
Related errors
- -32002
- SHELL_ALLOW_ALL should not be used with ShellAllowListMiddle
- interpreter_ptc must be False, 'safe', 'all', or a list of t
- {name} must be a table
- max_retries must be an int, got {type(max_retries).__name__}
AI-assisted analysis of langchain-ai/deepagents@a1af029e6e (2026-08-29).
Data as JSON: /api/errors/a2bc41d3712d8575.
Report an issue: GitHub.