Bird
Raised Fist0
Agentic AIml~20 mins

Output filtering and safety checks in Agentic AI - Practice Problems & Coding Challenges

Choose your learning style10 modes available

Start learning this pattern below

Jump into concepts and practice - no test required

or
Recommended
Test this pattern10 questions across easy, medium, and hard to know if this pattern is strong
Challenge - 5 Problems
🎖️
Output Safety Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
🧠 Conceptual
intermediate
2:00remaining
Why is output filtering important in AI systems?

Imagine you have an AI assistant that answers questions. Why should the system include output filtering and safety checks before showing answers to users?

ATo ensure the AI only shares safe, accurate, and appropriate information with users.
BTo make the AI respond faster by skipping complex answers.
CTo allow the AI to generate any content without restrictions.
DTo reduce the AI's memory usage during processing.
Attempts:
2 left
💡 Hint

Think about what could happen if the AI shares harmful or wrong information.

Predict Output
intermediate
2:00remaining
What is the output of this safety check code snippet?

Given this Python code that filters out unsafe words from AI output, what will be printed?

Agentic AI
unsafe_words = ['badword', 'danger']
output = 'This is a safe message.'
filtered_output = ' '.join(word for word in output.split() if word.lower() not in unsafe_words)
print(filtered_output)
AThis is a safe message.
BThis is a message.
CThis is a safe.
Dbadword danger
Attempts:
2 left
💡 Hint

Check if any words in the output match the unsafe words list.

Model Choice
advanced
2:00remaining
Which model architecture best supports output safety checks?

You want to build an AI that can self-monitor and filter its own outputs for safety. Which model design helps most?

AA convolutional neural network trained on images.
BA pipeline combining a language model with a separate safety classifier module.
CA single large language model without any internal safety layers.
DA simple linear regression model.
Attempts:
2 left
💡 Hint

Think about separating generation and safety checking tasks.

Hyperparameter
advanced
2:00remaining
Which hyperparameter adjustment can reduce unsafe outputs in text generation?

When generating text with a language model, which hyperparameter change helps reduce risky or harmful content?

AIncreasing temperature to a high value like 1.5.
BUsing a batch size of 64.
CIncreasing max token length to 1000.
DSetting temperature to a low value like 0.2.
Attempts:
2 left
💡 Hint

Lower temperature makes outputs more focused and less random.

🔧 Debug
expert
2:00remaining
Why does this output filtering code fail to block unsafe words?

Review this Python code meant to block unsafe words from AI output. Why does it fail to filter 'Danger'?

Agentic AI
unsafe_words = ['danger']
output = 'This is a Danger.'
filtered_output = ' '.join(word for word in output.split() if word.lower() not in unsafe_words)
print(filtered_output)
ABecause the code uses lower() but does not apply it to output words.
BBecause the unsafe_words list is empty.
CBecause 'Danger.' includes punctuation, it does not match 'danger' exactly.
DBecause join() is used incorrectly.
Attempts:
2 left
💡 Hint

Check how punctuation affects string matching.

Practice

(1/5)
1. What is the main purpose of output filtering in AI systems?
easy
A. To stop unsafe or unwanted AI results from reaching users
B. To speed up the AI model training process
C. To increase the size of the AI model
D. To add more data to the training set

Solution

  1. Step 1: Understand output filtering

    Output filtering is designed to prevent unsafe or unwanted content from being shown to users.
  2. Step 2: Compare options

    Only To stop unsafe or unwanted AI results from reaching users describes stopping unsafe or unwanted results, which matches the purpose of output filtering.
  3. Final Answer:

    To stop unsafe or unwanted AI results from reaching users -> Option A
  4. Quick Check:

    Output filtering = stopping unsafe results [OK]
Hint: Output filtering blocks bad or unsafe AI outputs [OK]
Common Mistakes:
  • Confusing filtering with training speed
  • Thinking filtering adds data
  • Assuming filtering changes model size
2. Which of the following is a correct way to check if an AI output contains a banned word in Python?
easy
A. if output_text.contains(banned_word):
B. if output_text == banned_word:
C. if output_text.index(banned_word):
D. if banned_word in output_text:

Solution

  1. Step 1: Recall Python syntax for substring check

    In Python, to check if a substring is in a string, use the 'in' keyword.
  2. Step 2: Evaluate options

    if banned_word in output_text: uses 'if banned_word in output_text:', which is correct. if output_text == banned_word: checks equality, not containment. if output_text.contains(banned_word): uses a method that doesn't exist in Python strings. if output_text.index(banned_word): uses index incorrectly and can cause errors.
  3. Final Answer:

    if banned_word in output_text: -> Option D
  4. Quick Check:

    Substring check in Python uses 'in' [OK]
Hint: Use 'in' keyword to check substring in Python strings [OK]
Common Mistakes:
  • Using equality instead of containment
  • Using non-existent string methods
  • Using index without error handling
3. Given this Python code snippet for filtering AI output:
output = "Hello user!"
banned_words = ["bad", "ugly"]
filtered = any(word in output for word in banned_words)
print(filtered)
What will be the printed output?
medium
A. False
B. True
C. Error
D. None

Solution

  1. Step 1: Understand the code logic

    The code checks if any banned word is in the output string using 'any()' with a generator expression.
  2. Step 2: Check banned words in output

    Output is "Hello user!". Neither "bad" nor "ugly" is in this string, so 'any()' returns False.
  3. Final Answer:

    False -> Option A
  4. Quick Check:

    None of banned words in output = False [OK]
Hint: If no banned words found, 'any' returns False [OK]
Common Mistakes:
  • Assuming 'any' returns True by default
  • Confusing 'any' with 'all'
  • Expecting an error from generator expression
4. This code is meant to filter AI output for banned words but causes an error:
output = "Safe text"
banned_words = ["bad", "ugly"]
for word in banned_words:
    if output.index(word):
        print("Banned word found")
        break
What is the error and how to fix it?
medium
A. Syntax error due to missing colon after for loop
B. index() raises ValueError if word not found; use 'in' instead
C. TypeError because output is not a list
D. No error; code works fine

Solution

  1. Step 1: Identify the error cause

    Using output.index(word) raises ValueError if word is not found in output string.
  2. Step 2: Suggest fix

    Replace 'output.index(word)' with 'word in output' to safely check containment without error.
  3. Final Answer:

    index() raises ValueError if word not found; use 'in' instead -> Option B
  4. Quick Check:

    index() error fixed by 'in' check [OK]
Hint: Use 'in' to check substring safely, not index() [OK]
Common Mistakes:
  • Ignoring ValueError from index()
  • Thinking output must be a list
  • Missing colons in loops (not in this code)
5. You want to build a safety filter that blocks AI outputs containing banned words or outputs longer than 100 characters. Which approach correctly combines these checks in Python?
hard
A. if banned_words in output or len(output) == 100: block_output()
B. if all(word in output for word in banned_words) and len(output) < 100: block_output()
C. if any(word in output for word in banned_words) or len(output) > 100: block_output()
D. if output.contains(banned_words) or output.length > 100: block_output()

Solution

  1. Step 1: Understand filtering conditions

    The filter should block if any banned word is present OR output length exceeds 100 characters.
  2. Step 2: Evaluate options for correct logic and syntax

    if any(word in output for word in banned_words) or len(output) > 100: block_output() uses 'any' to check banned words and 'or' for length > 100, which is correct. if all(word in output for word in banned_words) and len(output) < 100: block_output() uses 'all' and 'and' incorrectly. if output.contains(banned_words) or output.length > 100: block_output() uses invalid methods. if banned_words in output or len(output) == 100: block_output() uses wrong containment and equality checks.
  3. Final Answer:

    if any(word in output for word in banned_words) or len(output) > 100: block_output() -> Option C
  4. Quick Check:

    Use 'any' + 'or' for combined filter [OK]
Hint: Use 'any' with 'or' to combine banned words and length checks [OK]
Common Mistakes:
  • Using 'all' instead of 'any' for banned words
  • Using 'and' instead of 'or' to combine conditions
  • Using invalid string methods like contains()