← Blog

Validate Model Armor Sanitize Response Before Delivery

Parminder Singh
Parminder Singh··7 min read
Summarize with AI

Model Armor sanitizeModelResponse accepts model output under a named template and returns structured sanitization evidence. A safe Vertex response path validates transport, invocation status, aggregate and per-filter match states, optional transformed content, and local policy before releasing bytes to a user or agent.

Platform & Architectureai-securityllm-securitypolicy-enforcementinline-enforcementforensic-auditzero-trust
Validate Model Armor Sanitize Response Before Delivery

A successful Vertex inference produces model output awaiting application approval. The output still has to pass sanitizeModelResponse under the selected Model Armor template. That second HTTP exchange returns a sanitizationResult with aggregate status and filter-specific evidence. Delivery should wait until the application validates the complete result and applies its own route policy.

I want this path to be boring: buffer, inspect, decide, record, release. A browser that paints the first tokens before screening completes has already crossed the control boundary.

TL;DR

  • Send the completed model output to the regional :sanitizeModelResponse operation before releasing it to the authenticated caller.
  • Validate HTTP success, invocationResult, aggregate filterMatchState, and the enabled filter results as separate conditions.
  • Preserve Model Armor's native evidence and keep the application's delivery disposition in a separate field. That disposition can be deliver, hold, redact, or block.
  • Quarantine malformed or contradictory states, plus partial and unknown states, under a versioned per-route policy.

The response screen is a second decision point

Google's Model Armor overview places response screening after the LLM generates output and before the application sends that output to the user. Model Armor can return a response or sanitized response plus descriptions of triggered and untriggered filters. That architecture creates two distinct records: the Vertex inference result and the Model Armor screening result.

Correlate them with an internal operation ID. Retain the Vertex destination, model or endpoint identifier, response digest, Model Armor template resource, screening attempt, and final application disposition. The caller should receive content only after the operation reaches an approved terminal state.

The AI audit log schema supplies common identity and policy fields. Add provider-native Model Armor evidence without flattening it into a single safe: true value. An aggregate state helps route the decision, while individual filter objects explain which configured checks ran and what each one returned.

The HTTP request binds output to a template

Google's sanitizeModelResponse REST reference defines a POST operation on a complete template resource. Its required request field is modelResponseData. The schema also permits an associated userPrompt and multi-language detection metadata. It also permits a streaming mode.

For a non-streaming text route, use this request blueprint:

  • Method: POST
  • Origin: https://modelarmor.LOCATION.rep.googleapis.com
  • Path: /v1/projects/PROJECT_ID/locations/LOCATION/templates/TEMPLATE_ID:sanitizeModelResponse
  • Authentication: approved Google Cloud OAuth bearer token
  • Media type: application/json
  • Required body field: modelResponseData
  • Optional correlation input: userPrompt, subject to route data policy

The sanitization guide requires regional endpoints for prompt and response sanitization. It shows text under modelResponseData.text and returns a structured sanitizationResult.

Sending the original prompt can improve checks that depend on prompt-response context, but it also duplicates sensitive content into another request. Make that field an explicit route choice. Record whether it was included, omitted, hashed, or transformed.

Transport success opens result validation

Begin validation with the HTTP envelope and its status. A non-success status means the application lacks a valid sanitization decision. Classify authentication failure, permission denial, quota rejection, timeout, unavailable service, and malformed request separately. Each class can have a different retry owner, but a route requiring response screening should withhold the model output.

After HTTP success, parse the body with a strict size limit and schema checks. The guide's examples show sanitizationResult.invocationResult, aggregate filterMatchState, and nested filterResults. A response can therefore arrive over a successful HTTP exchange while a filter execution reports a problem. Keep transport and filter execution in different fields.

My blunt opinion is that treating HTTP 200 as permission to display the model output is negligent. The entire purpose of the second call is to inspect the result inside that envelope.

Reject duplicate JSON keys before semantic parsing if the selected parser permits them. Hold an absent sanitizationResult, invalid enum types, oversized body, or structure that exceeds the route's nesting limit. Preserve a protected copy of the raw provider response where evidence policy permits.

Aggregate and per-filter states need reconciliation

The aggregate filterMatchState gives a top-level result. Nested filter results can include their own execution and match states. Evaluate both layers before choosing a disposition. An aggregate match should enter the route's configured block or hold path. A configured transformed-content path is another option. A clean aggregate still needs evidence that every required filter produced an acceptable execution state.

Build a required-filter set from the template version approved for the route. Then compare the returned filter families with that set. Missing evidence for a required filter belongs in a named hold state. Unknown filter families should remain verbatim for review. Silently dropping a new field makes provider evolution invisible.

Contradictory fixtures expose unsafe parser assumptions. Start with NO_MATCH_FOUND at the aggregate level and MATCH_FOUND inside a nested result. Add a top-level match paired with clean nested results, followed by an invocation failure beside a clean aggregate. Finish with a missing filter object. The local validator should stop delivery and retain each inconsistency.

The review screen should make this visible. Put the aggregate state in a white header card. Beneath it, show each filter on its own row with execution state, match state, confidence or details when present, and the exact template resource. A single green badge hides too much.

Sanitized content needs provenance

Some template configurations can inspect or redact sensitive values and may otherwise sanitize them. If Model Armor returns transformed content, retain separate digests for the original model output and delivered output. Record the transformation source and the exact policy version that accepted delivery.

Never overwrite the original buffer in place before evidence is committed. Use one variable for the Vertex bytes and another for the Model Armor result. Use a separate reference for delivered content. Access to the original should follow the route's restricted retention policy. Routine logs and metrics labels should exclude raw prompt and response text. Support tickets should exclude that text as well.

A match result and a transformation are different facts. A route may permit a properly redacted response while another route blocks any response that triggered a sensitive-data rule. The application owns that disposition. Model Armor supplies the screening result and any returned sanitized representation configured by the service.

The per-route AI policies guide shows how route policy can bind data class, model destination, caller role, and acceptable outcomes without rewriting provider evidence.

Streaming changes the release contract

Google documents streaming response sanitization modes in the sanitization guide. The application still has to define when bytes become visible to the caller. Buffered screening is the clearest release boundary because no model output leaves before the terminal sanitization decision.

A real-time streaming design needs a documented exposure model. Record each screened chunk, its order, any withheld suffix, the terminal screening state, and the number of bytes already delivered if a later failure occurs. Downstream tool execution should wait for an accepted terminal response unless a separate policy explicitly governs incremental actions.

For high-impact routes, I prefer buffering. The extra complexity of retracting content from a human screen or an agent context is usually worse than the latency saved. Once a line appears in a chat pane, a screenshot or browser extension may already have copied it. A consuming agent may have copied it too.

Test client disconnects as well. A disconnect after Vertex inference but before Model Armor completion should cancel delivery and complete or explicitly abandon the evidence record. A retry must receive a new attempt identifier linked to the original operation.

Release fixtures cover every terminal path

A production test suite should use captured schema-shaped fixtures plus authenticated calls in an approved Google Cloud test project. Cover the exact template and regional host used by production.

Include these cases:

  • Clean response with every required filter executed
  • Aggregate match with one named filter match
  • Redacted or transformed response, when configured
  • Filter execution failure inside an HTTP success
  • Unknown aggregate enum and unknown nested enum
  • Missing required filter evidence
  • Contradictory aggregate and nested match states
  • Timeout before any sanitization result, or an oversized or malformed provider body
  • Streaming failure after one accepted chunk

Assert the client response, released byte count, retry behavior, evidence record, and downstream-action suppression for each case. The safe idempotent LLM retries guide explains how to distinguish a completed provider decision from an ambiguous transport outcome.

DeepInspect

DeepInspect can inspect Model Armor results on authenticated user or agent HTTP traffic deliberately routed through its proxy before a Vertex response is delivered. It can bind the result to caller identity and route policy, hold malformed or disallowed states, and write a per-decision record that preserves the native provider evidence.

Model Armor remains the screening service. The application owns buffering, user messaging, raw-content retention, human review, and downstream actions outside the routed exchange. Direct Vertex calls, local inference, STDIO tool traffic, and any response path that bypasses the proxy remain outside DeepInspect's boundary.

Book a technical deep dive at deepinspect.ai.

Frequently asked questions

What does sanitizeModelResponse accept?

The REST method requires modelResponseData. Google's schema also lists an optional associated userPrompt and multi-language detection metadata. It also lists a streaming mode. Use the regional service host matching the template location.

Does a successful HTTP response mean the model output is safe to deliver?

No. HTTP success only means the Model Armor exchange returned a body. The application still has to validate invocation status, aggregate match state, required filter execution, nested results, and its own route policy before delivery.

Should the application store the raw model response?

Retention depends on the route's data policy. At minimum, keep protected correlation evidence and separate digests for original and delivered content. Store raw content only in an approved system with access controls and retention limits appropriate to its classification.

Can sanitizeModelResponse provide identity-aware authorization?

It provides content-screening evidence under a Model Armor template. Enterprise authorization still needs trusted caller identity, role, data class, destination, and route policy at the gateway. Keep those facts beside the native screening result and attribute them to the gateway decision.