{"record":{"id":"babceee8bf242cbb","repo":"langchain-ai/langchain","slug":"saving-an-example-selector-is-not-currently-suppor-babcee","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_with_templates.py","lineNumber":238,"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 to a file.\n\n        Args:\n            file_path: The path to save the prompt 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","sourceCodeStart":220,"sourceCodeEnd":240,"githubUrl":"https://github.com/langchain-ai/langchain/blob/e32fa9a52eab3b61ad7a45399bfde59b3e580fc4/libs/core/langchain_core/prompts/few_shot_with_templates.py#L220-L240","documentation":"`FewShotPromptWithTemplates.save()` raises this `ValueError` when the prompt has an `example_selector` set. Serialization to disk only supports prompts whose examples are inlined; example selectors (e.g. `LengthBasedExampleSelector`, `SemanticSimilarityExampleSelector`) hold arbitrary state (vector stores, embedders) that the save format cannot represent.","triggerScenarios":"Calling `prompt.save('file.json')` (or `prompt.save_prompt`) on a `FewShotPromptWithTemplates` instance constructed with a non-None `example_selector` argument.","commonSituations":"Building a few-shot prompt with dynamic example selection, then trying to persist it to YAML/JSON for deployment; migrating older LangChain code that saved plain `FewShotPromptTemplate` objects and assuming selectors also serialize.","solutions":["Drop the selector before saving: rebuild a `FewShotPromptWithTemplates` with an explicit `examples=[...]` list and save that.","Serialize with `langchain_core.load.dumpd`/`dumps` if the selector itself is serializable, and reload via `load`/`loads`.","Persist the selector's configuration separately (e.g. the examples list plus selector type/params) and reconstruct the prompt at load time."],"exampleFix":"// before\nprompt = FewShotPromptTemplateWithTemplates(\n    example_selector=LengthBasedExampleSelector(...),\n    ...\n)\nprompt.save('few_shot.json')  # ValueError\n\n// after\nprompt = FewShotPromptWithTemplates(\n    examples=examples,  # inline list instead of selector\n    ...\n)\nprompt.save('few_shot.json')","handlingStrategy":"validation","validationCode":"# before saving, check selector presence\nif getattr(prompt, 'example_selector', None):\n    raise ValueError(\n        'Cannot save prompt with an example_selector; inline examples first.'\n    )\nprompt.save('prompt.json')","typeGuard":"from langchain_core.prompts.few_shot_with_templates import FewShotPromptWithTemplates\n\ndef is_savable_few_shot(p) -> bool:\n    return isinstance(p, FewShotPromptWithTemplates) and p.example_selector is None","tryCatchPattern":"try:\n    prompt.save(path)\nexcept ValueError as e:\n    if 'example selector' in str(e).lower():\n        # fall back: save a selector-free copy\n        FewShotPromptWithTemplates(\n            examples=prompt.examples, example_prompt=prompt.example_prompt,\n            suffix=prompt.suffix, prefix=prompt.prefix,\n            input_variables=prompt.input_variables,\n        ).save(path)\n    else:\n        raise","preventionTips":["Treat example selectors as runtime-only state; never design persistence flows that assume they serialize.","Prefer langchain_core.load.dumpd/dumps for prompt round-tripping."],"tags":["prompts","few-shot","serialization","valueerror"],"backgroundTag":null,"analyzedSha":"e32fa9a52eab3b61ad7a45399bfde59b3e580fc4","analyzedAt":"2026-08-14T18:42:09.092Z","schemaVersion":2},"datasetVersion":"2026-08-15T22:17:37.221Z"}