langchain-ai/langchain · error · ValueError

One of 'examples' and 'example_selector' should be provided

Error message

One of 'examples' and 'example_selector' should be provided

What it means

Pydantic model_validator error on the few-shot prompt mixin raised when neither `examples` nor `example_selector` is provided at construction. At least one example source is required; both None is invalid.

Source

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

            values: The values to check.

        Returns:
            The values if they are valid.

        Raises:
            ValueError: If neither or both `examples` and `example_selector` are
                provided.
            ValueError: If both `examples` and `example_selector` are provided.
        """
        examples = values.get("examples")
        example_selector = values.get("example_selector")
        if examples and example_selector:
            msg = "Only one of 'examples' and 'example_selector' should be provided"
            raise ValueError(msg)

        if examples is None and example_selector is None:
            msg = "One of 'examples' and 'example_selector' should be provided"
            raise ValueError(msg)

        return values

    def _get_examples(self, **kwargs: Any) -> list[dict[str, Any]]:
        """Get the examples to use for formatting the prompt.

        Args:
            **kwargs: Keyword arguments to be passed to the example selector.

        Returns:
            List of examples.

        Raises:
            ValueError: If neither `examples` nor `example_selector` are provided.
        """
        if self.examples is not None:
            return self.examples
        if self.example_selector is not None:

View on GitHub (pinned to e32fa9a52e)

Solutions

  1. Provide `examples=[{...}, ...]` as a static list at construction.
  2. Or provide an `example_selector` (e.g. LengthBasedExampleSelector, SemanticSimilarityExampleSelector) built over your example bank.
  3. If you truly want zero examples, pass examples=[] (empty list) rather than omitting it.

Example fix

# before
FewShotPromptTemplate(example_prompt=ex_prompt, suffix="{input}")

# after
FewShotPromptTemplate(
    example_prompt=ex_prompt, suffix="{input}", examples=[{"input": "hi"}]
)
Defensive patterns

Strategy: validation

Validate before calling

def has_example_source(kwargs):
    return "examples" in kwargs or "example_selector" in kwargs

Prevention

When it happens

Trigger: `FewShotPromptTemplate(example_prompt=..., suffix=...)` with no examples/example_selector kwarg. Note the validator truthiness check: an empty examples list [] is falsy but not None, so it passes; None for both fails.

Common situations: Omitting the kwarg entirely, explicitly passing examples=None while intending to set them later (post-init assignment bypasses the validator but then _get_examples raises later).

Related errors


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