Advanced

Input and output safety

A public chatbot will be asked harmful things. Learn to filter input, block abuse, and steer replies, so your bot stays useful and safe for everyone.

What you will see

System prompt: Never share personal information. If asked something unsafe, politely decline.

About 2 min of reading

Try It

A public chatbot will be asked harmful things, so no single guard is enough. I check each incoming message against a block list and show how a firm system prompt steers the model to decline safe requests.

1

I check a user message against a block list of bad phrases and print whether each is allowed, showing the first line of defense against abuse.

def isSafe(text): blocked = ["spam-link", "buy now"] lowered = text.lower() for word in blocked: if word in lowered: return False return True print("Input and output safety") print("Safe user text: %s" % isSafe("What is my order status?")) print("Abusive text rejected: %s" % (not isSafe("buy now click spam-link")))

Before you run it, say out loud what it will print. Then open the answer.

What it prints
Input and output safety Safe user text: True Abusive text rejected: True
A single filter catches obvious abuse early but not clever phrasing. Good bots defend in depth: input checks, a firm system prompt, rate limits, and logging together.
2

I add explicit rules to the system prompt so the model knows to decline unsafe requests before the model even generates a reply.

messages = [] messages.append({ "role": "system", "content": "Never share personal information. " + "If asked something unsafe, politely decline." }) print("System prompt: %s" % messages[0]["content"])

Before you run it, say out loud what it will print. Then open the answer.

What it prints
System prompt: Never share personal information. If asked something unsafe, politely decline.
The model follows system-prompt rules better than raw input filtering alone, but it can still be pushed. Combine the firm prompt with input checks and rate limits.

Put it in order

Before you write anything, put these lines back in the right order. Click a line to move it up one step. This support step is easier than writing code from scratch, and it is here to make the last example stick.

Data Flow

User input is filtered, the system prompt steers the model, the reply is checked, and any abuse is rate-limited or logged.
1 User sends a message
↓
2 Input is checked against the block list
↓
3 The system prompt shapes the reply
↓
4 The model generates a response
↓
5 The reply is validated
↓
6 Abuse is rate-limited and logged
Line by line

Read the first example one line at a time. A line that prints shows what appears on screen; the rest set things up quietly.

def isSafe(text): sets nothing on screen
blocked = ["spam-link", "buy now"] sets nothing on screen
lowered = text.lower() sets nothing on screen
for word in blocked: sets nothing on screen
if word in lowered: sets nothing on screen
return False sets nothing on screen
return True sets nothing on screen
print("Input and output safety") Input and output safety
print("Safe user text: %s" % isSafe("What is my order status?")) Safe user text: True
print("Abusive text rejected: %s" % (not isSafe("buy now click spam-link"))) Abusive text rejected: True
Why this works
Why safety is layered

No single guard is enough. Attackers are creative, so good bots defend in depth:

  • Input filters catch obvious abuse early.
  • System prompts shape how the model itself responds.
  • Rate limits stop someone from spamming the bot.
  • Logging lets you spot and block repeat offenders later.

Each layer catches what the others miss. The goal is not to be perfect but to make abuse hard and visible. A safe bot earns trust, and trust keeps users coming back.

Examples

Filtering bad input

Never trust user input directly. Check it before sending it to the model:

function isSafe(text) {
  const blocked = ['spam-link', 'buy now'];
  return !blocked.some((word) => text.toLowerCase().includes(word));
}

app.post('/chat', (req, res) => {
  if (!isSafe(req.body.text)) {
    return res.status(400).send('Please keep it clean');
  }
  // safe to continue
});

A simple filter rejects obvious abuse before it reaches the model. For real products you layer several checks, each catching a different kind of bad input.

Steering the model

You can tell the model what not to do. Add explicit rules to the system prompt:

messages.push({
  role: 'system',
  content: 'Never share personal information. '
         + 'If asked something unsafe, politely decline.'
});

The model follows these instructions better than raw input filtering alone. Combining a firm system prompt with input checks makes a chatbot both helpful and responsible.

Words this lesson uses

Each one is defined here on this site. Open the entry for the full definition and the other words it points at.

← Tuning model behaviour
Previous Lesson
Tuning model behaviour
Next Lesson
Giving the bot knowledge
Giving the bot knowledge →