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 before-validator on FewShotPromptWithTemplates raised when both `examples` and `example_selector` are provided at construction. Exactly one example source must be chosen, mirroring FewShotPromptTemplate's rule.

Source

Thrown at libs/core/langchain_core/prompts/few_shot_with_templates.py:72

    @classmethod
    def get_lc_namespace(cls) -> list[str]:
        """Get the namespace of the LangChain object.

        Returns:
            `["langchain", "prompts", "few_shot_with_templates"]`
        """
        return ["langchain", "prompts", "few_shot_with_templates"]

    @model_validator(mode="before")
    @classmethod
    def check_examples_and_selector(cls, values: dict[str, Any]) -> Any:
        """Check that one and only one of examples/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

    @model_validator(mode="after")
    def template_is_valid(self) -> Self:
        """Check that prefix, suffix, and input variables are consistent."""
        if self.validate_template:
            input_variables = self.input_variables
            expected_input_variables = set(self.suffix.input_variables)
            expected_input_variables |= set(self.partial_variables)
            if self.prefix is not None:
                expected_input_variables |= set(self.prefix.input_variables)
            missing_vars = expected_input_variables.difference(input_variables)
            if missing_vars:

View on GitHub (pinned to e32fa9a52e)

Solutions

  1. Remove one of the two keys from the constructor call / config file.
  2. Prefer example_selector when dynamic selection is needed; keep examples for static lists.

Example fix

# before
FewShotPromptWithTemplates(
    ..., examples=[...], example_selector=selector
)

# after
FewShotPromptWithTemplates(..., example_selector=selector)
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: `FewShotPromptWithTemplates(..., examples=[...], example_selector=...)` — validation runs in mode='before', so it fires even before field coercion, on the raw input dict.

Common situations: Refactoring FewShotPromptTemplate configs into the WithTemplates variant and carrying both fields over; YAML-driven prompt configs where both keys are present.

Related errors


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