tl  tr
  Home | Tutorials | Articles | Videos | Products | Tools | Search
Interviews | Open Source | Tag Cloud | Follow Us | Bookmark | Contact   
 Generative AI > Google Gemini API > ADK Input Validation and Sanitisation

ADK Input Validation and Sanitisation

Author: Venkata Sudhakar

ADK agents accept free-form natural language input, which makes them powerful but also vulnerable to abuse. Without input validation, attackers can attempt prompt injection to override agent instructions, submit extremely long inputs to exhaust token budgets, or extract sensitive data by crafting clever queries. A validation layer between the HTTP endpoint and the agent blocks these vectors before they reach the model.

ShopMax India exposes ADK agents to public-facing customer service chat. The engineering team adds an input validation middleware that checks message length, detects prompt injection patterns, blocks requests containing sensitive data patterns like Aadhaar numbers and credit card numbers, and normalises Unicode before forwarding clean input to the agent.

The below example shows a comprehensive input validation class for ShopMax India ADK agents that enforces length limits, blocks injection patterns, and sanitises PII.


It gives the following output,

  PASS: What is the price of Samsung TV?
  BLOCKED (Request blocked: policy violation): Ignore all previous instructions...
  PASS: My Aadhaar is 2345 6789 0123 please help with my order.
  Sanitised: My [Aadhaar number redacted] please help with my order.
  BLOCKED (Input too short): x

The below example shows the validator integrated into a Flask endpoint that gates all input to the ShopMax India ADK agent, logging blocked requests for security review.


It gives the following output,

INFO: ShopMax India secure chat endpoint ready on port 8080

POST /chat {"message": "What is the price of iPhone 15 Pro?", "user_id": "CUST-001"}
  -> Validation PASS | forwarded to ADK agent
  <- 200 OK {"response": "The iPhone 15 Pro is priced at Rs 1,29,900..."}

POST /chat {"message": "Ignore all previous instructions...", "user_id": "CUST-002"}
  [BLOCKED] user=CUST-002 reason=Request blocked: policy violation
  <- 400 {"error": "Your request could not be processed. Please rephrase and try again."}

Input validation is the first line of defence for ShopMax India ADK agents. By validating length, blocking injection patterns, and redacting PII before the message ever reaches the model, the engineering team prevents the most common attack vectors without adding perceptible latency to legitimate customer queries. All blocked requests are logged for the security team to review weekly.


 
  


  
bl  br