Bird
0
0

You receive user input as a nested dictionary with possible unsafe strings. How do you sanitize all string values safely in Python?

hard📝 Application Q8 of 15
Agentic AI - Agent Safety and Guardrails
You receive user input as a nested dictionary with possible unsafe strings. How do you sanitize all string values safely in Python?
AUse input() function to re-enter sanitized data
BRecursively traverse the dictionary and replace unsafe characters in each string
CIgnore nested strings and sanitize only top-level keys
DConvert the dictionary to string and sanitize once
Step-by-Step Solution
Solution:
  1. Step 1: Understand nested data structure

    Nested dictionaries require recursive traversal to reach all string values.
  2. Step 2: Apply sanitization to each string

    Replacing unsafe characters in each string ensures full sanitization.
  3. Final Answer:

    Recursively traverse the dictionary and replace unsafe characters in each string -> Option B
  4. Quick Check:

    Sanitize nested strings recursively [OK]
Quick Trick: Use recursion to sanitize nested data [OK]
Common Mistakes:
  • Sanitizing only top-level keys
  • Converting whole dict to string
  • Re-entering data manually

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Agentic AI Quizzes