langchain-ai/langchain · error · ValueError

Only one of 'examples' and 'example_selector' should be prov

Error message

Only one of 'examples' and 'example_selector' should be provided

What it means

Pydantic model_validator error on FewShotPromptTemplate / FewShotChatMessagePromptTemplate (the _FewShotPromptTemplateMixin validator) raised when both `examples` and `example_selector` are set. The class needs exactly one source of examples because formatting would be ambiguous otherwise.

Source

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

    def check_examples_and_selector(cls, values: dict[str, Any]) -> Any:
        """Check that one and only one of `examples`/`example_selector` are provided.

        Args:
            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.

View on GitHub (pinned to e32fa9a52e)

Solutions

  1. Delete one of the two: keep `examples` for static lists, or `example_selector` for dynamic selection.
  2. If you need a fallback, implement the fallback inside a custom BaseExampleSelector instead of setting both fields.

Example fix

# before
FewShotPromptTemplate(
    ..., examples=[...],
    example_selector=LengthBasedExampleSelector(...),
)

# after
FewShotPromptTemplate(
    ..., example_selector=LengthBasedExampleSelector(...),
)
Defensive patterns

Strategy: validation

Validate before calling

def exactly_one_example_source(examples, example_selector):
    return (examples is not None) ^ (example_selector is not None)

Prevention

When it happens

Trigger: `FewShotPromptTemplate(..., examples=[...], example_selector=LengthBasedExampleSelector(...))` at construction time (sync or async usage; validation runs on init).

Common situations: Starting with a static examples list, later adding a SemanticSimilarityExampleSelector for dynamic selection and forgetting to remove the list; copying config objects and merging fields.

Related errors


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