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 before-validator on FewShotPromptWithTemplates raised when neither `examples` nor `example_selector` is given. This legacy template class (extra='forbid') requires exactly one example source up front.

Source

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

        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:
                msg = (
                    f"Got input_variables={input_variables}, but based on "
                    f"prefix/suffix expected {expected_input_variables}"
                )

View on GitHub (pinned to e32fa9a52e)

Solutions

  1. Pass examples=[...] (even [] if you want none) at construction.
  2. Or pass an example_selector instance.
  3. Note extra='forbid': any additional unknown kwarg will also fail, so check config keys too.

Example fix

# before
FewShotPromptWithTemplates(
    example_prompt=PromptTemplate(...), suffix="{input}"
)

# after
FewShotPromptWithTemplates(
    example_prompt=PromptTemplate(...), suffix="{input}", examples=[]
)
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: `FewShotPromptWithTemplates(example_prompt=..., suffix=...)` with the examples kwarg omitted, or both explicitly None.

Common situations: Porting old langchain prompt YAML to core; minimal-construction attempts that plan to assign examples later.

Related errors


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