Skip to content
masterclassBuddyTech Buzz5 fresh blogs · 5:00 AM IST

AI & ML · Const

Few-shot Learning

Few-shot learning steers a language model with a handful of input/output examples in the prompt. The model infers the pattern and applies it to a new task input.

How it works

Few-shot learning guides a language model with a small set of input/output demonstrations included in the prompt. The model infers the demonstrated pattern and applies it to a new request, commonly with the examples placed in a developer message.

Syntax

developer message containing input/output demonstrations

Returns

The model's response to a new input, generated according to the demonstrated pattern.

Key points

  • A small demonstration set can redirect a general-purpose model toward a task it was not explicitly trained to perform.
  • The technique works by putting a limited number of paired inputs and desired outputs directly into the prompt.
  • The model infers the task pattern from the demonstrations rather than receiving a separately trained task-specific model.
  • After inferring the pattern, the model applies it to the new user request.
  • For API integrations, place the demonstrations in a developer message.

Common mistakes

  • Use varied demonstrations that cover the possible input patterns; narrow examples may not steer the model reliably across the task.

Example: Few-shot sentiment classification

from openai import OpenAI

client = OpenAI()
response = client.responses.create(
    model="gpt-4.1",
    input=[
        {"role":"developer","content":"Classify sentiment.\n\nExamples:\nInput: I love it.\nOutput: positive\n\nInput: It broke.\nOutput: negative"},
        {"role":"user","content":"The service was excellent."}
    ]
)
print(response.output_text)

The developer message supplies demonstrations, and the user message contains a new input for the model to classify.

When to use it

Few-shot classification

Classify incoming text into labels by showing labeled examples.

Structured transformation

Convert inputs into a target format by demonstrating representative transformations.

Prompt-based task steering

Teach a response style or task procedure without fine-tuning.

Interview questions

QWhat is few-shot learning?

Few-shot learning steers a model by placing several input/output demonstrations in the prompt instead of fine-tuning it. The model infers the pattern represented by those demonstrations and uses it for a new input.

Follow-up: How should the demonstrations be selected? Include a diverse range of possible inputs paired with the desired outputs.

QWhere should few-shot examples be placed?

The examples are usually supplied as part of a developer message in the API request. The new task input is then presented separately for the model to process using the demonstrated pattern.

Follow-up: Why use a developer message? It is the documented location OpenAI recommends for these examples in an API request.

QHow does few-shot learning differ from fine-tuning?

Few-shot learning uses a handful of examples in the prompt, while fine-tuning trains the model with many more examples than can fit in the prompt. Fine-tuning can therefore improve performance across a wider range of tasks.

Follow-up: What is the main practical trade-off? Prompt demonstrations avoid training but consume prompt space; fine-tuning requires a training workflow but supports more examples.

  • prompting
  • developer message
  • fine-tuning
  • input/output examples

Source: GPT Best Practices. Checked 2026-10-15.