Input Output Handling

22 patterns for this goal

Agents receive input from users and upstream systems, and produce output that downstream systems and users consume. Input-output-handling failures occur when input is not validated, output is not sanitized, encodings mismatch, or edge cases in data format (null bytes, special characters, timezones) are not handled, resulting in silent corruption, injection vulnerabilities, or downstream failures that are hard to trace back to input issues.

Key Takeaways

  1. Input Validation Bypass Is Silent and Cascading: Agents that don’t validate input accept invalid data (oversized strings, wrong types, malformed dates) that propagates downstream, causing failures in other agents, databases, or external systems. The original bad input is invisible in later failure messages.

  2. Output Hallucination in Structured Formats Is Catastrophic: When agents generate structured output (JSON, CSV, XML), hallucination (inventing fields or values) produces syntactically valid but semantically wrong output. Downstream systems accept and process the garbage because it’s valid format.

  3. Encoding Mismatches Are Silent: Input or output in different encodings (UTF-8 vs UTF-16, single-byte vs multi-byte) cause character corruption or rejection. Data containing non-ASCII characters is especially vulnerable to encoding bugs.

  4. Edge Cases in Data Format Escape Validation: Timezone ambiguity, null bytes, quote escaping, locale-specific formatting, and recursion limits are individually rare but collectively common. A comprehensive input/output validation strategy must explicitly handle each category.

Scope

Input-output-handling failures cluster into five categories:

  • Input Validation Gaps: Input is oversized, malformed, contains invalid characters, or has wrong schema; agent accepts and processes it anyway. (input-size-not-validated, input-schema-evolution, input-validation-bypass, input-recursion-limit)
  • Format & Encoding Issues: Input or output uses unexpected encoding (UTF-8 vs UTF-16), locale (en-US vs de-DE), or timezone, causing misinterpretation. (input-encoding-mismatch, input-locale-mismatch, input-timezone-ambiguity, output-encoding-issues)
  • Special Character Handling: Input contains special characters (null bytes, quotes, backslashes) that agent doesn’t escape properly, causing injection or truncation. (input-null-bytes-injection, input-special-character-handling, output-quote-escaping-failure, output-sanitization-bypass)
  • Output Errors: Output is truncated, hallucinated, inconsistent, or doesn’t match promised format; downstream systems consume garbage. (output-hallucination-in-structured-format, output-truncation-silent, output-inconsistency, output-format-not-validated)
  • Default Assumptions: Agent assumes input has a default value, or doesn’t validate output bounds, or makes implicit assumptions about data type conversions. (input-default-value-assumption, output-length-not-enforced, output-precision-loss, output-type-coercion-failure)

When Input-Output-Handling Matters

  1. Data Pipeline Transformations: Agents that transform data from one format to another (JSON to CSV, XML to database, user input to query). One agent’s output is the next agent’s input; corruption propagates through the pipeline.

  2. External API Integration: Agents calling external APIs that have strict input requirements and return structured output that must be parsed. Encoding mismatches or format violations cause silent API failures.

  3. User-Facing Systems: Agents that directly consume user input (forms, text, file uploads). User input is maximally unconstrained; validation must be aggressive.

Cross-Pattern Insight

Input-output-handling is fundamentally about explicit contracts at system boundaries. An agent that receives input has no way to know if the input is valid without validation. The previous agent that produced the output has no way to know if downstream agents will accept it without explicit format negotiation. A robust approach requires: (1) input validation at every boundary (validate type, size, encoding, format, required fields); (2) explicit format and encoding negotiation (agree on UTF-8 encoding, timezone-aware date strings, JSON as format); (3) output validation to ensure the agent’s output matches the promised format; (4) error handling for validation failures (don’t silently truncate or corrupt, explicitly reject invalid input); and (5) regular audits of sample input/output to catch edge cases that validation missed. Without input validation, encoding negotiation, output validation, error handling, and sample auditing, garbage input and output propagate through systems silently, and failures manifest far downstream.

Frequently Asked Questions

How can an agent validate that input is the right size without rejecting legitimate large inputs? Set a maximum input size based on the agent’s computational budget, not a fixed number. For example, an agent that can process 100,000 tokens within its time budget should reject input larger than that. Communicate the limit to upstream systems. If legitimate inputs exceed the limit, either increase the limit or split processing into multiple requests.

Why is output hallucination in structured formats harder to detect than output errors? Because syntactically valid output passes basic format checks (valid JSON, valid CSV) but contains inverted or invented fields. A downstream parser accepts it without error. The semantic correctness of the output (does the content match what was requested?) is not verified by format validation. Mitigations: (1) use schemas with required fields and type checking, (2) validate a sample of output against a ground truth, (3) check for suspicious patterns (e.g., all-zero numeric fields, repeated values that shouldn’t be repeated).

What is the difference between timezone ambiguity and locale mismatch? Timezone ambiguity occurs when a time string doesn’t specify timezone (e.g., “2024-01-01 12:00”) and different systems assume different timezones. Locale mismatch occurs when dates/times/numbers use locale-specific formatting (e.g., “01/12/2024” means January 12 in US, December 1 in EU). Both must be handled explicitly: use UTC for all internal timestamps, communicate timezone with date strings (“2024-01-01T12:00Z”), and use locale-independent formats.

How can an agent avoid silent truncation of output? Validate output length before returning it. If the output is required to fit in a certain number of characters or tokens, check length and reject (or truncate explicitly, indicating truncation) rather than silently cutting off. For structured output, validate that all required fields are present. For numeric output, validate precision (decimal places) against specification.

What should an agent do if input validation fails? Fail fast and explicitly. Return a clear error message indicating what validation failed (size, encoding, schema, required field missing) and what the constraint is. Do not silently truncate, convert, or corrupt input. Do not proceed with invalid input hoping it will be handled downstream. Downstream agents will not know to validate what the sending agent accepted.

Failure Patterns

PatternDescription
Input Default Value AssumptionAgent assumes missing input field has a default value; upstream system didn’t provide default, causing silent misinterpretation.
Input Encoding MismatchInput is in UTF-16 but agent expects UTF-8, or vice versa, causing character corruption.
Input Locale MismatchInput date/number uses locale-specific formatting (en-US vs de-DE); agent misinterprets or rejects it.
Input Null Bytes InjectionInput contains null bytes that truncate strings or cause C-string vulnerabilities.
Input Recursion LimitNested input structure (nested arrays, objects, or references) exceeds agent’s recursion limit, causing stack overflow or timeout.
Input Schema EvolutionUpstream system adds or removes input fields; downstream agent doesn’t adapt, causing schema mismatch.
Input Size Not ValidatedAgent accepts input that exceeds its computational budget or memory capacity.
Input Special Character HandlingInput contains quotes, backslashes, or other special characters that aren’t properly escaped.
Input Timezone AmbiguityInput timestamp doesn’t specify timezone; agent and upstream system assume different timezones.
Input Validation BypassInput validation is disabled, bypassed, or incomplete; invalid input is processed.
Output Encoding IssuesOutput is encoded in a different encoding than downstream system expects.
Output Format Not ValidatedOutput is not checked to match promised format (JSON schema, CSV headers, XML structure).
Output Hallucination in Structured FormatAgent generates plausible-looking but false fields in structured output (JSON, CSV).
Output InconsistencyMultiple invocations of the same agent with the same input produce different outputs.
Output Injection VulnerabilityOutput contains unsanitized user input that’s then interpreted as code or commands downstream.
Output Length Not EnforcedOutput is longer than downstream system can accept (character limit, token limit, file size).
Output Ordering NondeterminismOutput ordering is inconsistent across runs (e.g., JSON object field order, list order).
Output Precision LossOutput converts high-precision numeric values to lower precision (float32 to float16), losing information.
Output Quote Escaping FailureOutput contains quotes or backslashes that aren’t properly escaped for CSV/JSON/SQL.
Output Sanitization BypassOutput sanitization is incomplete; dangerous content (HTML, SQL, shell commands) escapes and is executed.
Output Truncation SilentOutput is truncated to fit a size limit without indicating truncation occurred.
Output Type Coercion FailureOutput is silently converted to a different type (string to number, boolean to int) losing information or causing type errors downstream.

Total: 22 patterns

  • Agent-Handoffs-Delegation — handoff payloads are input/output between agents; malformed handoffs are input-output-handling failures
  • Dependency-Management — API contract violations (wrong input format, unexpected output format) are input-output-handling failures
  • Data-Pipeline-Integration — schema evolution and encoding mismatches are pipeline integration failures that stem from input-output handling
  • Observability-Monitoring — input/output handling failures often go undetected; visibility into data flow is critical
  • Security — input injection, output sanitization, and type coercion are security concerns alongside functional correctness

Input Default Value Assumption

Frequency: Common
Category: Operations

An agent receives an input payload with a missing or null field and silently substitutes what it assumes is a "safe" default (zero, empty string, current date, `false`, the first enum value) instead of treating the absence as an error or asking for clarification. The assumed default is often wrong for the specific business context — a missing `discount_percent` treated as `0` when it should have blocked the order, or a missing `region` treated as `"US"` when the request originated elsewhere — and the agent proceeds to act on that fabricated value as if it were provided.

Input Encoding Mismatch

Frequency: Common
Category: Operations

An agent reads input bytes assuming one character encoding (typically UTF-8) while the actual source encoded the text differently (Latin-1/ISO-8859-1, Windows-1252, UTF-16, or a legacy code page), producing mojibake — visually garbled or silently wrong characters — in names, addresses, and free text. Because most bytes in Latin-1 and Windows-1252 are also valid (but differently-meaning) UTF-8 continuation sequences for a wide range of inputs, the decode frequently "succeeds" without throwing an error, so the corruption passes silently into storage and downstream processing.

Input Locale Mismatch

Frequency: Common
Category: Operations

An agent interprets a date, number, or currency value using the wrong locale convention — reading "03/04/2026" as March 4th when the source used day-month-year, or parsing "1.234,56" as one-point-two-three-four instead of one thousand two hundred thirty-four point five six. The value parses without error under the wrong locale's rules, so the agent proceeds confidently with a value that is silently different from what the source intended.

Input Null Bytes Injection

Frequency: Rare
Category: Operations

An agent accepts input containing embedded null bytes (`�`) — whether from malicious crafting, corrupted upstream data, or binary content misrouted into a text field — and passes it to a downstream layer (a C-based library, a filesystem call, a database driver, or a validation regex) whose string handling treats the null byte as a terminator. The agent's own validation logic sees the full string and approves it, but the consuming layer only sees the truncated prefix, creating a gap between what was validated and what was actually acted on.

Input Recursion Limit

Frequency: Occasional
Category: Operations

An agent's parser (JSON, XML, YAML, or a custom nested-structure format) receives an input with excessive nesting depth — either from a legitimately complex source, a buggy upstream serializer that loops, or a deliberately crafted payload — and the recursive-descent parsing logic exceeds the language runtime's call-stack limit or the parser's own recursion guard, crashing the process rather than rejecting the input gracefully. Because the crash happens inside the parsing library itself, it often takes down the whole request-handling worker rather than failing just the one bad input.

Input Schema Evolution

Frequency: Common
Category: Operations

An upstream system that feeds an agent changes its data schema — renaming a field, changing a type, adding a required field, deprecating an enum value — without a coordinated update to the agent's input parser, so the agent either silently misreads the new shape (treating a renamed field as missing and falling back to a default) or crashes on fields it no longer recognizes. Because the agent's own code didn't change, the failure looks like a regression with no corresponding commit, making it unusually hard to diagnose.

Input Size Not Validated

Frequency: Common
Category: Operations

An agent accepts an input payload (a document, a file upload, a JSON body, an attachment) without checking its size against any reasonable bound before processing it, so an unusually large input — whether legitimate, accidental, or adversarial — is loaded fully into memory, tokenized in full, or passed whole into a downstream call, causing memory pressure, request-latency spikes, or costly API usage that a small size check would have caught in microseconds.

Input Special Character Handling

Frequency: Very Common
Category: Operations

An agent's input parsing or downstream rendering logic breaks when the input contains characters with structural meaning in some layer of the pipeline — quotes, backslashes, delimiters (commas, pipes, tabs), markup characters (`<`, `>`, `&`), or control characters — and that layer wasn't written to treat them as literal data. A customer name containing an apostrophe, a product description containing a comma inside a CSV field, or free text containing an unescaped `<` breaks parsing, corrupts a field boundary, or renders incorrectly, independent of any encoding issue.

Input Timezone Ambiguity

Frequency: Common
Category: Operations

An agent receives a timestamp or time-of-day value with no explicit timezone, or with a timezone abbreviation that is genuinely ambiguous (e.g. "CST" meaning Central Standard Time or China Standard Time), and interprets it using an assumed timezone — usually the server's local time, UTC, or the timezone of whichever user the agent most recently interacted with — that doesn't match what the source actually meant. The resulting timestamp is a valid, well-formed datetime that is simply wrong by however many hours separate the assumed and actual timezones.

Input Validation Bypass

Frequency: Occasional
Category: Operations

An agent's input validation rule checks the input's surface form (a regex, a length check, an allowlist match) but the check can be satisfied by an encoding, formatting, or representation variant that is semantically equivalent to a blocked value while syntactically different enough to slip past the check. Unicode homoglyphs, alternate encodings, case variations, whitespace insertion, or double-encoding let disallowed content — a blocked word, a malicious path, an injection payload — pass a validator that was written to catch only the literal, canonical form.

Output Encoding Issues

Frequency: Common
Category: Operations

An agent generates output text and serializes it in one encoding while the downstream consumer (an API client, a file writer, a terminal, an email client) expects or declares a different one, corrupting non-ASCII characters — accented letters, currency symbols, emoji, non-Latin scripts — before the text reaches its destination. Unlike an input encoding mismatch, the corruption is introduced by the agent's own serialization step rather than inherited from a source, and it typically affects every non-ASCII character the agent itself generates or passes through, not just specific fields.

Output Format Not Validated

Frequency: Common
Category: Operations

An agent produces output intended to conform to a specific schema (JSON with required fields, a fixed CSV column set, an API response contract) and hands it directly to a downstream consumer without verifying it actually matches that schema first. Because LLM-generated output is probabilistic rather than mechanically guaranteed, a small but nonzero fraction of responses have a missing field, wrong type, extra field, or malformed structure — and without a validation gate, that malformed output reaches the consumer exactly as if it were valid, causing it to fail unpredictably rather than being caught at the source.

Output Hallucination in Structured Format

Frequency: Common
Category: Operations

When an agent is required to produce output matching a fixed schema, it will sometimes fabricate a plausible-looking value for a field it has no actual basis for — inventing a tracking number, a confidence score, a source citation, or an ID — rather than leaving the field empty, marking it as unknown, or declining to complete the schema. The output is structurally valid and passes any format/type check, which makes the fabrication far more dangerous than a free-text hallucination: it looks exactly like a correctly-populated field to any downstream system or reviewer that trusts schema conformance as a proxy for correctness.

Output Inconsistency

Frequency: Common
Category: Operations

The same logical input, processed by an agent on separate occasions, produces output with a different structure, field set, ordering, or format each time — not because the underlying data changed, but because the generation process itself is nondeterministic and nothing constrains it to produce a stable shape. A consumer that parses the first call's output shape and hardcodes assumptions from it breaks on the next call, even though nothing about the request changed.

Output Injection Vulnerability

Frequency: Occasional
Category: Operations

An agent constructs a downstream command, query, or markup document by directly interpolating its own generated text (or text derived from user/tool input the agent passed through) into a SQL statement, shell command, or HTML page without parameterization or escaping. Because the interpolated content can contain characters with special meaning in the target language, it can alter the structure of the command rather than being treated as inert data — the classic injection pattern, but with the agent's own generated or relayed text as the injection vector instead of a raw user form field.

Output Length Not Enforced

Frequency: Common
Category: Operations

An agent generates output without any hard cap on its length, and a downstream consumer with an actual limit — a database column with a fixed `VARCHAR` size, a UI element with a character budget, an SMS/notification channel with a payload cap, a third-party API with a field-length restriction — receives output that exceeds it. Depending on the consumer, this either causes a hard rejection (a database `INSERT` failing, an API returning a 400) or, worse, a silent truncation somewhere in the chain that the agent itself has no visibility into and cannot compensate for.

Output Ordering Nondeterminism

Frequency: Occasional
Category: Operations

An agent returns a list or array whose element order varies from call to call for logically equivalent input, even though the consuming system depends on a stable order — for pagination cursors, for diffing successive results, for deterministic display, or for stable IDs derived from position. Because the list's *contents* are correct each time, the failure is easy to miss in isolated testing and only surfaces when two calls are compared against each other or when a consumer's assumption of stability is violated.

Output Precision Loss

Frequency: Occasional
Category: Operations

An agent generates or serializes a numeric value in a way that loses precision relative to the actual computed or intended value — rounding a currency amount that needed exact cent-level precision, formatting a large integer through a floating-point representation that can't represent it exactly, or truncating decimal places in a scientific/financial figure. The output looks like a reasonable number and passes any type check, but its value is subtly different from the correct one, and that difference compounds when the number feeds further calculation.

Output Quote Escaping Failure

Frequency: Very Common
Category: Operations

An agent generates text that must be embedded inside a structured format — a JSON string value, a CSV field, a shell argument, a string literal in generated code — and the content itself contains quote characters, apostrophes, or backslashes that need to be escaped for the target grammar. Because the model produces the escaped output as free-form text generation rather than by running a deterministic escaping function, it frequently gets the transform wrong: under-escaping (leaving a raw quote that terminates the string early), over-escaping (doubling an already-correct escape sequence), or escaping for the wrong target grammar entirely (JSON-escaping content destined for a shell command, or vice versa). The result is a downstream parse failure or a corrupted field, distinct from output injection in that no malicious input is required — the model breaks its own well-intentioned output on ordinary content like a customer's name containing an apostrophe.

Output Sanitization Bypass

Frequency: Occasional
Category: Operations

A pipeline runs agent-generated output through a sanitization step — a blocklist filter, an HTML-stripping function, a pattern-based scrubber — before it reaches a downstream consumer, and the sanitizer genuinely runs and genuinely modifies output that matches its rules. The gap is that the sanitizer's rules cover a specific, enumerable set of dangerous patterns rather than the full space of ways the same underlying danger can be represented: an encoded or obfuscated variant of a blocked pattern passes through untouched because it doesn't match the literal pattern the sanitizer looks for, or content that was safe at the moment it was sanitized becomes dangerous again after a later transformation step (minification, template re-interpolation, client-side re-parsing) that the sanitizer never accounted for. This is distinct from having no sanitization at all — the defense exists, runs, and has a real but incomplete coverage boundary that a sufficiently different-looking payload slips past.

Output Truncation Silent

Frequency: Very Common
Category: Operations

An agent's generated output — a chat completion, a streamed response, or a payload returned from a tool call — gets cut off mid-generation or mid-transmission (hitting a `max_tokens` cap, a proxy timeout, a streaming connection drop, or an intermediate buffer limit), and nothing in the pipeline detects or flags that the content is incomplete. The truncated fragment is syntactically plausible enough (a sentence that just stops, or JSON that's missing its closing braces) that it gets parsed, stored, or displayed as if it were the complete, intended output, rather than triggering a retry or an explicit incompleteness signal.

Output Type Coercion Failure

Frequency: Common
Category: Operations

An agent produces output whose values are of one type — a string, a loosely-formatted number, a mixed-case boolean word — and the downstream system consuming that output performs an implicit type coercion while deserializing or ingesting it, silently converting the value into something semantically different rather than rejecting it. Because the coercion happens inside the consumer's parsing/deserialization layer rather than inside the agent, the agent has no visibility into the mismatch and no chance to correct it; the corrupted value simply propagates into the receiving system as if it were correct.