Skip to main content
Taught by Tech Leads

Master pipelines, cloud & AI to become an operational Data Engineer.

DataScientist.fr
Image de Understanding and Using Example Selectors in LangChain
Artificial Intelligence
LLM
Python

Understanding and Using Example Selectors in LangChain

Photo de Romain DE LA SOUCHÈRE

Tech Lead, CTO AXI Technologies

Published on 16 mai 2025 · 10 min of reading

In the field of development with large language models (LLMs), the relevance of prompts largely determines the quality of the results obtained. Example Selectors play a crucial role in constructing suitable and intelligent prompts. These components allow for the dynamic selection of the best examples to include in a prompt based on the provided input, while adhering to the constraints imposed by the model's context window size.
Whether you are building a conversational agent, a question-answering application, a translation interface, or a classification system, Example Selectors enable you to tailor your examples on the fly to improve the model's understanding. They address a central challenge: how to provide the model with sufficient relevant context without unnecessarily cluttering it?
In this article, we will dive deep into the universe of Example Selectors from LangChain. We will detail the existing types, their internal workings, the use cases in which they excel, and how to integrate them into your dynamic prompt flows. We will also explore how to create your own custom selection logic for specific scenarios, especially when no pre-existing solution meets your needs. Finally, we will discuss the integration possibilities with versioned datasets like those of LangSmith to further push the automation of example selection.

What is an Example Selector?

An ExampleSelector is an interface provided by LangChain, whose main method is <code class="inline-code">select_examples(input_variables: Dict[str, str]) -> List[dict]. This method dynamically returns a list of examples to include in a prompt based on the input variables. The goal is to provide relevant examples without overloading the context used by the LLM, thereby maximizing the model's performance.
Each implementation of an Example Selector can have its own selection logic: semantic similarity, content length, lexical overlap, or any other relevant metric. LangChain provides several ready-to-use implementations, but you can also define your own selectors if your needs exceed standard cases.

Main types of Example Selectors

LangChain offers several built-in strategies for automatic example selection. Here are the most commonly used:

1. Similarity Selector

This selector chooses the examples closest to the input using vector embeddings. It relies on libraries like OpenAI Embeddings and vector search engines such as Chroma or FAISS. It is ideal when the understanding of overall meaning is more important than exact word matching.
python
This approach captures the semantic nuances between the input and the examples. It is particularly effective in applications requiring deep contextual understanding, such as during managing conversation history with LangChain.

2. MMR (Maximal Marginal Relevance) Selector

MMR selection combines semantic similarity with a diversity metric. This means that in addition to seeking the most relevant examples relative to the input, it tries to avoid selecting examples that are too similar to each other.
python
This method is ideal for creating balanced prompts that contain varied and complementary cases, thus improving the robustness of the model's responses.

3. Length-Based Selector

This selector evaluates the total size of the formatted examples and selects those that meet a length threshold. It is very useful for controlling the volume of the generated prompt, especially if you are working with models that have limited contexts.
python
Depending on the length of the input, this selector can dynamically adjust the number of included examples. This optimizes the use of context space without sacrificing quality.

4. N-gram Overlap Selector

The <code>NGramOverlapExampleSelector ranks examples based on the number of n-grams shared with the input. It relies on a BLEU score calculated via the NLTK library.
python
This approach is simple, fast, and very useful when the inputs have lexical structures similar to the available examples. It offers a good compromise between performance and computational cost.
You can easily enrich the pool of examples with new cases:
python

Creating a custom Example Selector

In some cases, you may want to create your own selection logic. Here is a simple example that selects an example based on the length of the input word. This can be useful for educational cases or highly controlled inputs.
python
Such a custom selector can also be enhanced to integrate additional criteria: confidence score, presence of keywords, etc.

Integration into a Few-Shot Prompt

All Example Selectors can be integrated into dynamic prompts via the <code>FewShotPromptTemplate class.
python
This allows for the complete automation of example selection and formatting for each new user request.

Using with LangSmith

LangSmith is a complementary platform to LangChain that allows for managing versioned datasets. These datasets can be used as a dynamic source of relevant examples through indexing and search functionalities.
python
This method allows for externalizing examples, ensuring their traceability, and facilitating their update without having to modify the application code.
You can even combine this with asynchronous chains using RunnableLambda to dynamically adapt examples to the calling context.

Conclusion

The Example Selectors from LangChain are a cornerstone in creating effective, scalable, and contextually appropriate prompts. Thanks to them, you can automate example selection, optimize the use of context space, and significantly improve the quality of responses produced by LLMs.
LangChain gives you the choice between selectors advocating semantic similarity, diversity, length optimization, or lexical overlap. And if no existing method suffices, you have the freedom to create your own strategies. By combining these tools with dynamic sources like LangSmith, you achieve a robust, customizable, and production-ready solution.

Want to go further?

This topic is part of our Generative AI for Developers course. Browse the full programme, or get it by email.

Share with

Photo de Romain DE LA SOUCHÈRE

Romain DE LA SOUCHÈRE

Tech Lead, CTO AXI Technologies

Expert Data Engineering et Cloud, Romain affiche plus de 11 ans d'expérience, dont plusieurs années comme Lead Developer sur des solutions Smart Building haute performance. Il y a conçu et mis en production des moteurs de traitement capables d'absorber des centaines de milliers de données de capteurs par minute, ainsi que des bases clusterisées gérant plus de 10 millions de données dynamiques. Certifié Microsoft Azure DevOps Engineer Expert, il maîtrise aussi bien le développement back-end (Python, C#) que le DevOps (Docker, Kubernetes, Terraform) et les agents LLM. Formateur en Python, cloud, DevOps et IA générative appliquée, il forme avec une obsession : Amener chaque apprenant à concevoir et déployer des architectures réellement scalables en production.

» Learn More

Associated trainings

All our trainings
Image de la formation Generative AI for Developers
Generative AI for Developers
50 hours
Intermediate
Guarantee

Associated articles

See all our articles