langchain-ai/langchain · error · ValueError
Saving an example selector is not currently supported
Error message
Saving an example selector is not currently supported
What it means
`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.
Source
Thrown at libs/core/langchain_core/prompts/few_shot_with_templates.py:238
@deprecated(
since="1.2.21",
removal="2.0.0",
alternative="Use `dumpd`/`dumps` from `langchain_core.load` to serialize "
"prompts and `load`/`loads` to deserialize them.",
)
def save(self, file_path: Path | str) -> None:
"""Save the prompt to a file.
Args:
file_path: The path to save the prompt to.
Raises:
ValueError: If `example_selector` is provided.
"""
if self.example_selector:
msg = "Saving an example selector is not currently supported"
raise ValueError(msg)
return super().save(file_path)
View on GitHub (pinned to e32fa9a52e)
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.
Example fix
// before
prompt = FewShotPromptTemplateWithTemplates(
example_selector=LengthBasedExampleSelector(...),
...
)
prompt.save('few_shot.json') # ValueError
// after
prompt = FewShotPromptWithTemplates(
examples=examples, # inline list instead of selector
...
)
prompt.save('few_shot.json') Defensive patterns
Strategy: validation
Validate before calling
# before saving, check selector presence
if getattr(prompt, 'example_selector', None):
raise ValueError(
'Cannot save prompt with an example_selector; inline examples first.'
)
prompt.save('prompt.json') Type guard
from langchain_core.prompts.few_shot_with_templates import FewShotPromptWithTemplates
def is_savable_few_shot(p) -> bool:
return isinstance(p, FewShotPromptWithTemplates) and p.example_selector is None Try / catch
try:
prompt.save(path)
except ValueError as e:
if 'example selector' in str(e).lower():
# fall back: save a selector-free copy
FewShotPromptWithTemplates(
examples=prompt.examples, example_prompt=prompt.example_prompt,
suffix=prompt.suffix, prefix=prompt.prefix,
input_variables=prompt.input_variables,
).save(path)
else:
raise Prevention
- 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.
When it happens
Trigger: Calling `prompt.save('file.json')` (or `prompt.save_prompt`) on a `FewShotPromptWithTemplates` instance constructed with a non-None `example_selector` argument.
Common situations: 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.
Related errors
- Saving an example selector is not currently supported
- Cannot save prompt with partial variables.
- Prompt {self} does not support saving.
- {save_path} must be json or yaml
- Only one of 'examples' and 'example_selector' should be prov
AI-assisted analysis of langchain-ai/langchain@e32fa9a52e (2026-08-14).
Data as JSON: /api/errors/babceee8bf242cbb.
Report an issue: GitHub.