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

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.

Source

Thrown at libs/core/langchain_core/prompts/few_shot.py:258

    @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 template to a file.

        Args:
            file_path: The path to save the prompt template 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)


class FewShotChatMessagePromptTemplate(
    BaseChatPromptTemplate, _FewShotPromptTemplateMixin
):
    """Chat prompt template that supports few-shot examples.

    The high level structure of produced by this prompt template is a list of messages
    consisting of prefix message(s), example message(s), and suffix message(s).

    This structure enables creating a conversation with intermediate examples like:

    ```txt
    System: You are a helpful AI Assistant

    Human: What is 2+2?

View on GitHub (pinned to e32fa9a52e)

Solutions

  1. If you need serializable prompts, fall back to the static `examples` list and drop example_selector.
  2. Otherwise keep the selector in code and serialize only the surrounding pieces (example_prompt, suffix, prefix), rebuilding the selector at load time.
  3. Wrap save() in try/except ValueError and log a pointer to the rebuild strategy.

Example fix

# before
prompt = FewShotPromptTemplate(..., example_selector=selector)
prompt.save("prompt.yaml")

# after
prompt = FewShotPromptTemplate(..., examples=selector.examples)
prompt.save("prompt.yaml")
Defensive patterns

Strategy: try-catch

Validate before calling

def can_save(prompt):
    return not prompt.example_selector

Try / catch

try:
    prompt.save(path)
except ValueError as e:
    if "example selector" in str(e):
        # rebuild from selector.examples as a static list, then save
        prompt.examples = prompt.example_selector.examples
        prompt.example_selector = None
        prompt.save(path)
    else:
        raise

Prevention

When it happens

Trigger: `prompt.save("prompt.yaml")` on a FewShotPromptTemplate constructed with example_selector=LengthBasedExampleSelector(...) (or any BaseExampleSelector). The examples=[...] variant saves fine.

Common situations: 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.

Related errors


AI-assisted analysis of langchain-ai/langchain@e32fa9a52e (2026-08-14). Data as JSON: /api/errors/49986ddab39b0b25. Report an issue: GitHub.