Input validation and sanitization help make sure the data going into a machine learning system is safe and correct. This stops errors and keeps the system working well.
0
0
Input validation and sanitization in Agentic Ai
Introduction
When receiving user data like text or numbers before using it in a model
When cleaning data from external sources to avoid bad or harmful input
Before feeding data into a machine learning pipeline to prevent crashes
When building AI agents that interact with users or other systems
When you want to avoid security risks like code injection or corrupted data
Syntax
Agentic_ai
def validate_input(data): # Check if data meets rules if not isinstance(data, str): raise ValueError('Input must be a string') if len(data) == 0: raise ValueError('Input cannot be empty') return data def sanitize_input(data): # Remove unwanted characters clean_data = data.strip().lower() return clean_data
Validation checks if input is the right type and format.
Sanitization cleans the input to remove or fix bad parts.
Examples
This checks if the input is a non-empty string and passes it through.
Agentic_ai
validate_input('Hello World')This removes spaces around the text and converts it to lowercase.
Agentic_ai
sanitize_input(' Hello World! ')This raises an error because the input is not a string.
Agentic_ai
validate_input(123)Sample Program
This program checks a list of inputs. It validates each input to be a non-empty string, then cleans it by trimming spaces and making it lowercase. It prints the cleaned input or an error message.
Agentic_ai
def validate_input(data): if not isinstance(data, str): raise ValueError('Input must be a string') if len(data) == 0: raise ValueError('Input cannot be empty') return data def sanitize_input(data): clean_data = data.strip().lower() return clean_data # Example usage inputs = [' Hello AI ', '', 42, 'Goodbye!'] for i, item in enumerate(inputs): try: valid = validate_input(item) clean = sanitize_input(valid) print(f'Input {i}: Valid and sanitized -> "{clean}"') except ValueError as e: print(f'Input {i}: Error - {e}')
OutputSuccess
Important Notes
Always validate before sanitizing to catch wrong data early.
Sanitization depends on your use case; for example, removing HTML tags if needed.
Good input handling improves model reliability and security.
Summary
Input validation checks if data is the right type and format.
Input sanitization cleans data to remove unwanted parts.
Both steps help keep machine learning systems safe and working well.
