Skip to main content
Few-shot prompting lets you teach a model the desired input→output mapping by providing a small set of concrete examples instead of a long explicit instruction. This technique is ideal when examples come from external sources (CSV, database, etc.) and you want the model to infer the transformation from patterns in those examples. Below is a concise, corrected, and cleaned-up example using LangChain to build a few-shot chat prompt template, format it for a new input, and invoke a chat model.

Prerequisites

  • Python 3.8+
  • LangChain installed: pip install langchain
  • OpenAI (or compatible) credentials set in the environment
Useful references:

Imports and environment

Set your OpenAI API key in the environment and import the required classes:

1) Define examples

Create a small list of example input/output pairs. In this demo, outputs are exact reversals of the inputs:

2) Create the example prompt

Construct a prompt template that defines how each example should be formatted for the chat. Each example is represented as a human message followed by an AI message:

3) Build the few-shot message template

This collates all examples into a single few-shot block that can be inserted into a larger prompt sequence:

4) Inspect the few-shot formatted content

You can inspect the generated few-shot block to verify formatting:
Expected formatted output:

5) Assemble the final chat prompt template

Combine a system instruction with the few-shot examples block and a human placeholder for the runtime input:

6) Format messages for a new input

Format the concrete message sequence for runtime inputs (for example, "Brazil"). This produces the actual messages that will be sent to the model:

7) Invoke the chat model

Create a chat model instance and call it with the formatted messages. The model should generalize the reversal mapping from the few-shot examples and reverse the runtime input:

Why this works

  • The few-shot block provides concrete input/output pairs that demonstrate the intended transformation without an explicit instruction like “reverse the string”.
  • The system role (“You are a linguistic specialist.”) nudges the model toward language-focused behaviors.
  • At runtime you only supply the new input; the model infers the mapping from the examples.
Few-shot prompting is especially effective when you want the model to generalize from a small set of representative examples (e.g., rows from a CSV or entries from a database). Choose examples that cover the variations you expect the model to handle.

Notes and best practices

  • Use at least a few examples (3+ is a reasonable starting point) so the model can identify consistent patterns.
  • Ensure examples are representative of expected inputs and edge cases.
  • You can reuse the same template structure to teach different mappings by swapping the examples list or adjusting the system role.
  • For larger or more complex transformations, combine few-shot examples with a brief instruction in the system role to improve reliability.

Quick reference

Links and references:

Watch Video

Practice Lab