LangChain’s few-shot prompting isn’t just about showing an LLM examples; it’s about subtly coercing its latent knowledge into a specific, reproducible output format.

Let’s see it in action. Imagine we want to extract structured data from unstructured text, like pulling out product names and their prices.

from langchain_core.prompts import FewShotPromptTemplate, PromptTemplate
from langchain_openai import OpenAI

# Define the examples
examples = [
    {
        "input": "The new SuperWidget costs $99.99.",
        "output": "Product: SuperWidget, Price: $99.99"
    },
    {
        "input": "Get the MegaGadget for only $49.50!",
        "output": "Product: MegaGadget, Price: $49.50"
    },
    {
        "input": "Introducing the UltraGizmo, available at $199.",
        "output": "Product: UltraGizmo, Price: $199"
    }
]

# Define the template for the examples
example_prompt = PromptTemplate(input_variables=["input", "output"], template="Input: {input}\nOutput: {output}")

# Define the overall prompt template
prompt = FewShotPromptTemplate(
    examples=examples,
    example_prompt=example_prompt,
    suffix="Input: {input}\nOutput:",
    input_variables=["input"]
)

# Initialize the LLM
llm = OpenAI(model="gpt-3.5-turbo-instruct", temperature=0)

# Format the prompt and get the response
formatted_prompt = prompt.format(input="We're selling the AwesomeDoodad for $75.")
print("--- Formatted Prompt ---")
print(formatted_prompt)
print("\n--- LLM Response ---")
response = llm.invoke(formatted_prompt)
print(response)

The formatted_prompt will look like this:

--- Formatted Prompt ---
Input: The new SuperWidget costs $99.99.
Output: Product: SuperWidget, Price: $99.99
Input: Get the MegaGadget for only $49.50!
Output: Product: MegaGadget, Price: $49.50
Input: Introducing the UltraGizmo, available at $199.
Output: Product: UltraGizmo, Price: $199
Input: We're selling the AwesomeDoodad for $75.
Output:

And the response will be:

--- LLM Response ---
Product: AwesomeDoodad, Price: $75.

This works by creating a "context window" of desired behavior. The LLM doesn’t learn a new skill; it recognizes a pattern within the provided examples and applies that pattern to the new, unseen input. The FewShotPromptTemplate orchestrates this by taking your list of examples, formatting each one using example_prompt, and then appending the final suffix which contains the actual input you want the LLM to process. The suffix is crucial because it mirrors the structure of the example outputs, guiding the LLM to complete the pattern.

The power here lies in its simplicity and its ability to steer the LLM without fine-tuning. You’re essentially demonstrating the task and the desired output format directly in the prompt. The temperature parameter on the LLM is also important; setting it to 0 makes the output deterministic, ensuring that for the same input and prompt, you get the same structured output every time, which is critical for reliable data extraction or task automation. If you wanted more creative or varied outputs, you’d increase the temperature.

What most people miss is how sensitive the LLM is to the exact phrasing and formatting of the examples. A slight variation in the "Output:" string, the inclusion or omission of a comma, or the order of elements can significantly alter the LLM’s ability to generalize. The examples act as a very precise, albeit implicit, schema definition. The LLM is not "understanding" the schema; it’s following the most statistically probable completion of the pattern you’ve laid out.

Once you’ve mastered few-shot prompting for structured output, the next logical step is to explore how to dynamically generate these examples based on user queries or database lookups, a technique often referred to as "retrieval-augmented generation" or dynamic few-shot learning.

Want structured learning?

Take the full Langchain course →