{"record":{"id":"0d3bc14fbfe6752b","repo":"langchain-ai/langchain","slug":"prompt-self-does-not-support-saving","errorCode":null,"errorMessage":"Prompt {self} does not support saving.","messagePattern":"Prompt (.+?) does not support saving\\.","errorType":"exception","errorClass":"NotImplementedError","httpStatus":null,"severity":"error","filePath":"libs/core/langchain_core/prompts/base.py","lineNumber":412,"sourceCode":"            ValueError: If the prompt has partial variables.\n            ValueError: If the file path is not json or yaml.\n            NotImplementedError: If the prompt type is not implemented.\n\n        Example:\n            ```python\n            prompt.save(file_path=\"path/prompt.yaml\")\n            ```\n        \"\"\"\n        if self.partial_variables:\n            msg = \"Cannot save prompt with partial variables.\"\n            raise ValueError(msg)\n\n        # Fetch dictionary to save. Preserve deprecated `dict()` overrides until\n        # `dict()` is removed.\n        prompt_dict = self._dict_for_compat()\n        if \"_type\" not in prompt_dict:\n            msg = f\"Prompt {self} does not support saving.\"\n            raise NotImplementedError(msg)\n\n        # Convert file to Path object.\n        save_path = Path(file_path)\n\n        directory_path = save_path.parent\n        directory_path.mkdir(parents=True, exist_ok=True)\n\n        resolved_path = save_path.resolve()\n        if resolved_path.suffix == \".json\":\n            with resolved_path.open(\"w\", encoding=\"utf-8\") as f:\n                json.dump(prompt_dict, f, indent=4)\n        elif resolved_path.suffix.endswith((\".yaml\", \".yml\")):\n            with resolved_path.open(\"w\", encoding=\"utf-8\") as f:\n                yaml.dump(prompt_dict, f, default_flow_style=False)\n        else:\n            msg = f\"{save_path} must be json or yaml\"\n            raise ValueError(msg)\n","sourceCodeStart":394,"sourceCodeEnd":430,"githubUrl":"https://github.com/langchain-ai/langchain/blob/e32fa9a52eab3b61ad7a45399bfde59b3e580fc4/libs/core/langchain_core/prompts/base.py#L394-L430","documentation":"save() raises NotImplementedError when the prompt's serialized dict (from _dict_for_compat) lacks the '_type' key. '_type' identifies the prompt class for deserialization; a custom or third-party prompt type that does not implement the legacy dict()/_type convention cannot be round-tripped through save/load.","triggerScenarios":"Calling .save() on a custom subclass of BasePromptTemplate (or StringPromptTemplate) that overrides formatting without exposing a '_type' in its dict representation.","commonSituations":"Users with bespoke prompt classes (e.g. custom FewShotPrompt variants or framework-provided templates outside core) attempting the documented prompt.save(file_path=...) flow.","solutions":["Use one of the built-in serializable types (PromptTemplate, ChatPromptTemplate) for anything you need to save","For custom prompts, implement your own serialization (pickle-free, e.g. Pydantic model_dump) instead of save()","Add a '_type' entry to the dict your custom class produces, mirroring built-ins, if you also implement a matching loader"],"exampleFix":"# before\nclass MyPrompt(BasePromptTemplate):\n    ...\nMyPrompt(...).save(\"p.yaml\")  # NotImplementedError\n\n# after\n# serialize via Pydantic instead\ndata = my_prompt.model_dump()\nPath(\"p.json\").write_text(json.dumps(data, default=str))","handlingStrategy":"fallback","validationCode":"def is_serializable_prompt(prompt) -> bool:\n    return \"_type\" in prompt._dict_for_compat()","typeGuard":null,"tryCatchPattern":"try:\n    prompt.save(path)\nexcept NotImplementedError:\n    # custom prompt: fall back to your own serialization\n    path.write_text(prompt.model_dump_json())","preventionTips":["Prefer built-in PromptTemplate/ChatPromptTemplate for anything that must round-trip via save/load","Write a custom serializer for custom prompt classes instead of relying on save()"],"tags":["prompts","serialization","custom-class","core"],"backgroundTag":null,"analyzedSha":"e32fa9a52eab3b61ad7a45399bfde59b3e580fc4","analyzedAt":"2026-08-14T18:42:09.092Z","schemaVersion":2},"datasetVersion":"2026-08-15T17:31:12.345Z"}