{"record":{"id":"49986ddab39b0b25","repo":"langchain-ai/langchain","slug":"saving-an-example-selector-is-not-currently-suppor","errorCode":null,"errorMessage":"Saving an example selector is not currently supported","messagePattern":"Saving an example selector is not currently supported","errorType":"exception","errorClass":"ValueError","httpStatus":null,"severity":"error","filePath":"libs/core/langchain_core/prompts/few_shot.py","lineNumber":258,"sourceCode":"\n    @deprecated(\n        since=\"1.2.21\",\n        removal=\"2.0.0\",\n        alternative=\"Use `dumpd`/`dumps` from `langchain_core.load` to serialize \"\n        \"prompts and `load`/`loads` to deserialize them.\",\n    )\n    def save(self, file_path: Path | str) -> None:\n        \"\"\"Save the prompt template to a file.\n\n        Args:\n            file_path: The path to save the prompt template to.\n\n        Raises:\n            ValueError: If `example_selector` is provided.\n        \"\"\"\n        if self.example_selector:\n            msg = \"Saving an example selector is not currently supported\"\n            raise ValueError(msg)\n        return super().save(file_path)\n\n\nclass FewShotChatMessagePromptTemplate(\n    BaseChatPromptTemplate, _FewShotPromptTemplateMixin\n):\n    \"\"\"Chat prompt template that supports few-shot examples.\n\n    The high level structure of produced by this prompt template is a list of messages\n    consisting of prefix message(s), example message(s), and suffix message(s).\n\n    This structure enables creating a conversation with intermediate examples like:\n\n    ```txt\n    System: You are a helpful AI Assistant\n\n    Human: What is 2+2?\n","sourceCodeStart":240,"sourceCodeEnd":276,"githubUrl":"https://github.com/langchain-ai/langchain/blob/e32fa9a52eab3b61ad7a45399bfde59b3e580fc4/libs/core/langchain_core/prompts/few_shot.py#L240-L276","documentation":"Raised by FewShotPromptTemplate.save() when the template uses an `example_selector`. Serialization of example selectors is not implemented, so saving to YAML/JSON is refused rather than producing a silently incomplete artifact.","triggerScenarios":"`prompt.save(\"prompt.yaml\")` on a FewShotPromptTemplate constructed with example_selector=LengthBasedExampleSelector(...) (or any BaseExampleSelector). The examples=[...] variant saves fine.","commonSituations":"Moving from static examples to a selector (e.g. semantic search over examples) while keeping a save-to-file step in a config-generation pipeline or notebook.","solutions":["If you need serializable prompts, fall back to the static `examples` list and drop example_selector.","Otherwise keep the selector in code and serialize only the surrounding pieces (example_prompt, suffix, prefix), rebuilding the selector at load time.","Wrap save() in try/except ValueError and log a pointer to the rebuild strategy."],"exampleFix":"# before\nprompt = FewShotPromptTemplate(..., example_selector=selector)\nprompt.save(\"prompt.yaml\")\n\n# after\nprompt = FewShotPromptTemplate(..., examples=selector.examples)\nprompt.save(\"prompt.yaml\")","handlingStrategy":"try-catch","validationCode":"def can_save(prompt):\n    return not prompt.example_selector","typeGuard":null,"tryCatchPattern":"try:\n    prompt.save(path)\nexcept ValueError as e:\n    if \"example selector\" in str(e):\n        # rebuild from selector.examples as a static list, then save\n        prompt.examples = prompt.example_selector.examples\n        prompt.example_selector = None\n        prompt.save(path)\n    else:\n        raise","preventionTips":["Don't call save() on selector-based few-shot templates; serialize the pieces and rebuild the selector at load time.","Gate save() behind a check for example_selector in pipeline code."],"tags":["prompts","few-shot","serialization"],"backgroundTag":null,"analyzedSha":"e32fa9a52eab3b61ad7a45399bfde59b3e580fc4","analyzedAt":"2026-08-14T18:42:09.092Z","schemaVersion":2},"datasetVersion":"2026-08-15T17:31:12.345Z"}