Validate Bedrock Prompt Variables Before Converse
Amazon Bedrock Converse accepts promptVariables as a map when modelId identifies a Prompt management resource. Each documented variable value is a union whose current member is text. This guide validates the final JSON shape, binds variable names to an approved prompt version, rejects unsafe ambiguity, and avoids inventing AWS variable-name, map-size, or text-length limits.

Amazon Bedrock Converse accepts promptVariables as a map when modelId identifies a prompt resource from Prompt management. Each map value uses the PromptVariableValues union, and AWS currently documents text as its member. These API pages publish no maximum map size or text length. They also omit constraints on names. A reliable validator must enforce the documented JSON shape and use its own versioned application limits without presenting them as AWS limits.
I want that distinction visible in the error code. Provider schema failures and local policy failures need different names.
TL;DR
- Permit
promptVariablesonly when the selectedmodelIdis an approved Prompt management version ARN. - Validate each value as an object containing the documented
textunion member with a string value. - Bind allowed and required variable names to your reviewed prompt-version manifest, then reject missing, extra, duplicate, or ambiguous input before egress.
- Set local size and character limits for your application, while labeling them as local because the cited AWS pages publish no variable-name, map-size, text-length, or character-policy bounds.
Converse defines a map of union values
The Amazon Bedrock Converse request reference defines promptVariables as a string-to-PromptVariableValues object map. AWS says the field fills variables in a prompt from Prompt management and is ignored when modelId does not specify a prompt resource.
The PromptVariableValues datatype marks the object as a union. Only one union member can be specified, and the current page documents text as that member with a string value.
That gives the validator a precise structural job. The final serialized request should contain a JSON object at promptVariables. Each variable name maps to an object that contains exactly one recognized union member. The text member carries a JSON string. Reject arrays, scalar values, null, nested objects under text, unknown members, and objects that combine text with another member.
The structural contract ends there. The referenced pages state no maximum entry count, variable-name length, variable-name pattern, or text length for this map. Avoid copying limits from requestMetadata or another Bedrock field. They describe different API members.
Variable-name validation belongs to the prompt version
A valid JSON shape can still target the wrong variable. Keep a release manifest for each approved prompt version ARN. It should name the variables expected by that exact version and identify which are required by your application. It should also define the local data class plus size policy for each value.
AWS's Prompt management testing guide shows a versioned prompt ARN in modelId and a promptVariables map whose entries contain text. It also says managed prompts define their prompt template, while supplied messages are appended after the prompt's messages. That combination makes prompt identity part of variable validation.
Apply exact name matching before the request reaches Bedrock. Unicode normalization, case folding, trimming, or punctuation substitution can turn two caller inputs into one apparent name. Accept the canonical names in the reviewed manifest and reject every other spelling. The application should produce a stable local error that names the rejected field without reflecting sensitive values.
My blunt opinion is that accepting arbitrary variable names because Bedrock can ignore or reject them is lazy boundary design. The application already knows which prompt it intends to run. Its validator should know that prompt's input contract too.
The LangChain prompt injection guide owns the broader problem of untrusted text entering prompt construction. This guide owns the narrower runtime map contract.
The final serialized JSON is the enforcement point
Validate the bytes that will leave the gateway, not only a typed SDK object built earlier. Middleware can merge maps or adapters can rename fields. Custom serializers can emit duplicate JSON member names. Different parsers may retain the first duplicate or the last one, so reject duplicate members before a normal object parser erases the ambiguity.
A safe request path performs these checks:
- Resolve the route to one approved prompt version ARN and manifest.
- Parse caller input under a duplicate-rejecting JSON policy.
- Build a fresh
promptVariablesobject from allowed canonical names. - Validate every value object and its
textstring. - Apply documented local limits by UTF-8 byte count or Unicode scalar count, whichever your policy names.
- Serialize once, inspect the final request, and attach the Bedrock credential only after acceptance.
Store only the evidence your investigation requires. A value digest, length, classification, variable name, prompt version, and decision can prove which contract ran while reducing plaintext exposure in logs. Sensitive values need separate access and retention rules.
The LLM API parameter allowlisting guide provides the larger pattern for rebuilding outbound requests from an approved field set.
Local bounds need honest labels
Every production route needs resource limits even when the provider page publishes none for this field. Set a maximum map-entry count, per-value byte limit, total serialized-body limit, and permitted character policy based on the application's prompt and data handling requirements. Prefix related errors with a local namespace, such as PROMPT_VARIABLE_LOCAL_SIZE_LIMIT.
Do not write "Bedrock allows 20 variables" unless a current AWS source for this exact field says so. The current Converse and PromptVariableValues pages used here provide no such number. The same caution applies to variable-name length and text length.
Local character policy should remain narrow and explainable. A customer-support summary may permit normal Unicode text while rejecting control characters other than approved whitespace. A ticket identifier may use a smaller character set. These are application decisions, not universal prompt-safety rules.
Content controls need a separate decision after structural checks. Structural validation proves that a variable name and value fit the selected prompt contract. It cannot prove the text is truthful, safe, authorized for the model, or free of prompt injection. Data classification and prompt-content policy still run on the accepted text.
Release tests must mutate shape and meaning
Start with a request containing every required variable name and valid text. Inspect the final wire JSON and confirm that the approved prompt version ARN appears in the request path while the exact canonical names appear once in the body.
Then run destructive fixtures for names and values:
- Missing required variable name
- Unexpected variable name with a valid text object
- Variable name with wrong case or trailing whitespace
- Duplicate variable name in raw JSON
- Value set to a string instead of an object
textset tonull, a number, an array, or an object- Empty union object
- Unknown union member
- Object containing
textplus another member
Run route and size fixtures too. Test locally oversized text, an oversized aggregate body, valid variables paired with a different prompt version ARN, and promptVariables supplied with a non-prompt model ID.
The last case deserves a hard local rejection. AWS says the field is ignored without a prompt resource. Silent removal would hide a caller defect, while forwarding it would create a request whose apparent inputs differ from the operative prompt.
On the review screen, list variable names in one column and show type, byte length, classification, and decision beside each name. Keep plaintext behind a separate restricted control. The visual should make one wrong variable name obvious without turning the page into a prompt-data dump.
DeepInspect
DeepInspect can enforce this variable contract on authenticated user or agent HTTP traffic deliberately routed to Amazon Bedrock. It can bind the request to an approved prompt version, rebuild the allowed variable map, apply local limits, and record names, digests, classifications, and policy outcomes before egress.
The application owns the release manifest and supplies trustworthy caller context. AWS defines the runtime schema and processes the prompt. Direct SDK calls that bypass the proxy, local prompt rendering, STDIO paths, credential theft, and plaintext retained by another system remain outside DeepInspect's enforcement boundary.
Book a technical deep dive at deepinspect.ai.
Frequently asked questions
- What shape does each prompt variable value use?
AWS documents
PromptVariableValuesas a union with the current membertext. A valid entry therefore maps a variable name to an object whosetextmember contains a string. Validate the final JSON object rather than assuming an SDK type survived serialization unchanged.- Does AWS publish a maximum number of prompt variables?
The cited Converse and
PromptVariableValuesAPI pages publish no map-entry maximum forpromptVariables. Applications should impose their own reviewed bound for resource control and label it as local policy.- Should unknown variable names be forwarded to Bedrock?
A production gateway should compare variable names with the manifest for the approved prompt version and reject unknown input before egress. This produces a stable application contract and catches version drift without relying on downstream behavior.
- Does structural validation stop prompt injection?
Structural validation confirms the final JSON shape. It checks variable names and union members, along with value types. Prompt injection is a content and authorization problem that needs separate controls over untrusted text, tool access, data exposure, and output handling.