{"record":{"id":"559f2b67860e6368","repo":"langchain-ai/langchain","slug":"only-one-of-examples-and-example-selector-shou-559f2b","errorCode":null,"errorMessage":"Only one of 'examples' and 'example_selector' should be provided","messagePattern":"Only one of 'examples' and 'example_selector' should be provided","errorType":"validation","errorClass":"ValueError","httpStatus":null,"severity":"error","filePath":"libs/core/langchain_core/prompts/few_shot_with_templates.py","lineNumber":72,"sourceCode":"\n    @classmethod\n    def get_lc_namespace(cls) -> list[str]:\n        \"\"\"Get the namespace of the LangChain object.\n\n        Returns:\n            `[\"langchain\", \"prompts\", \"few_shot_with_templates\"]`\n        \"\"\"\n        return [\"langchain\", \"prompts\", \"few_shot_with_templates\"]\n\n    @model_validator(mode=\"before\")\n    @classmethod\n    def check_examples_and_selector(cls, values: dict[str, Any]) -> Any:\n        \"\"\"Check that one and only one of examples/example_selector are provided.\"\"\"\n        examples = values.get(\"examples\")\n        example_selector = values.get(\"example_selector\")\n        if examples and example_selector:\n            msg = \"Only one of 'examples' and 'example_selector' should be provided\"\n            raise ValueError(msg)\n\n        if examples is None and example_selector is None:\n            msg = \"One of 'examples' and 'example_selector' should be provided\"\n            raise ValueError(msg)\n\n        return values\n\n    @model_validator(mode=\"after\")\n    def template_is_valid(self) -> Self:\n        \"\"\"Check that prefix, suffix, and input variables are consistent.\"\"\"\n        if self.validate_template:\n            input_variables = self.input_variables\n            expected_input_variables = set(self.suffix.input_variables)\n            expected_input_variables |= set(self.partial_variables)\n            if self.prefix is not None:\n                expected_input_variables |= set(self.prefix.input_variables)\n            missing_vars = expected_input_variables.difference(input_variables)\n            if missing_vars:","sourceCodeStart":54,"sourceCodeEnd":90,"githubUrl":"https://github.com/langchain-ai/langchain/blob/e32fa9a52eab3b61ad7a45399bfde59b3e580fc4/libs/core/langchain_core/prompts/few_shot_with_templates.py#L54-L90","documentation":"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.","triggerScenarios":"`FewShotPromptWithTemplates(..., examples=[...], example_selector=...)` — validation runs in mode='before', so it fires even before field coercion, on the raw input dict.","commonSituations":"Refactoring FewShotPromptTemplate configs into the WithTemplates variant and carrying both fields over; YAML-driven prompt configs where both keys are present.","solutions":["Remove one of the two keys from the constructor call / config file.","Prefer example_selector when dynamic selection is needed; keep examples for static lists."],"exampleFix":"# before\nFewShotPromptWithTemplates(\n    ..., examples=[...], example_selector=selector\n)\n\n# after\nFewShotPromptWithTemplates(..., example_selector=selector)","handlingStrategy":"validation","validationCode":"def exactly_one_example_source(examples, example_selector):\n    return (examples is not None) ^ (example_selector is not None)","typeGuard":null,"tryCatchPattern":null,"preventionTips":["Validate prompt config dicts (exactly one of examples/example_selector) before constructing.","Use JSON schema oneOf for config-driven prompts to enforce mutual exclusion."],"tags":["prompts","few-shot","pydantic-validation"],"backgroundTag":null,"analyzedSha":"e32fa9a52eab3b61ad7a45399bfde59b3e580fc4","analyzedAt":"2026-08-14T18:42:09.092Z","schemaVersion":2},"datasetVersion":"2026-08-15T22:17:37.221Z"}