{"record":{"id":"50dfadc6ea693fa3","repo":"langchain-ai/deepagents","slug":"system-prompt-must-be-str-or-none-got-type-syste-50dfad","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":"exception","errorClass":"TypeError","httpStatus":null,"severity":"error","filePath":"libs/deepagents/deepagents/middleware/summarization.py","lineNumber":1858,"sourceCode":"        system_prompt: str | None = None,\n    ) -> None:\n        \"\"\"Initialize with a reference to the summarization middleware.\n\n        Args:\n            summarization: The `SummarizationMiddleware` instance whose\n                summarization engine this tool will delegate to.\n            system_prompt: System-prompt fragment nudging the model to call\n                `compact_conversation`. Pass `None` to skip appending the\n                nudge entirely (the tool remains registered and callable\n                but the model is unlikely to discover it without an\n                external mention).\n\n        Raises:\n            TypeError: If `system_prompt` is not `str` or `None`.\n        \"\"\"\n        if system_prompt is not None and 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        self._summarization = summarization\n        self.system_prompt = system_prompt\n        self.tools: list[BaseTool] = [self._create_compact_tool()]\n\n    def _create_compact_tool(self) -> BaseTool:\n        \"\"\"Create the `compact_conversation` structured tool.\n\n        Returns:\n            A `StructuredTool` with both sync and async implementations.\n        \"\"\"\n        from langchain_core.tools import StructuredTool  # noqa: PLC0415\n\n        mw = self\n\n        def sync_compact(runtime: ToolRuntime) -> Command:\n            return mw._run_compact(runtime)\n\n        async def async_compact(runtime: ToolRuntime) -> Command:","sourceCodeStart":1840,"sourceCodeEnd":1876,"githubUrl":"https://github.com/langchain-ai/deepagents/blob/a1af029e6e73cb17c36bff823d227747b28e91e1/libs/deepagents/deepagents/middleware/summarization.py#L1840-L1876","documentation":"The `compact_conversation` tool middleware accepts an optional `system_prompt` that must be a `str` or `None`. Any other type (dict, bytes, list, etc.) is rejected with `TypeError` in `__init__`, guarding against silently coercing invalid prompt values.","triggerScenarios":"Constructing the compact/summarization tool middleware with `system_prompt` set to a non-string value, e.g. a dict of prompt parts or a `None`-like sentinel object.","commonSituations":"Loading prompts from YAML/JSON where they deserialize to dicts; concatenating prompt fragments into a list; forgetting to `.join()` or `str()` a computed value.","solutions":["Pass a plain string (or omit for the default)","Convert structured prompt configs to a string before construction (join sections)","Validate config-loaded prompt values are strings"],"exampleFix":"// before\nCompactTool(summarization=mw, system_prompt={\"intro\": \"...\", \"rules\": \"...\"})\n// after\nCompactTool(summarization=mw, system_prompt=\"intro...\\nrules...\")","handlingStrategy":"type-guard","validationCode":"if system_prompt is not None and not isinstance(system_prompt, str):\n    system_prompt = \"\\n\".join(str(p) for p in system_prompt.values()) if isinstance(system_prompt, dict) else str(system_prompt)","typeGuard":"def is_str_or_none(v: object) -> bool:\n    return v is None or isinstance(v, str)","tryCatchPattern":"try:\n    tool_mw = CompactTool(summarization=mw, system_prompt=prompt)\nexcept TypeError as e:\n    if \"system_prompt\" in str(e):\n        logger.error(\"system_prompt must be str or None, got %r\", type(prompt))\n    raise","preventionTips":["Coerce prompt configs to strings right after loading YAML/JSON","Type-annotate `system_prompt: str | None` in wrapper functions","Unit-test config loading to assert prompt fields deserialize as strings"],"tags":["python","summarization","typeerror","type-validation"],"backgroundTag":"wrong-argument-type","analyzedSha":"a1af029e6e73cb17c36bff823d227747b28e91e1","analyzedAt":"2026-08-29T11:43:24.718Z","schemaVersion":2},"datasetVersion":"2026-08-29T12:17:43.993Z"}