esc
  • WorkflowsPages
  • Case studiesPages
  • Machine Learning FoundationsLearn
  • AI EngineeringLearn
  • AI Guardrails & Red TeamingLearn
  • UI/UXLearn
  • Design SystemsLearn
  • Production System DesignLearn
  • AI papersPages
  • Interview KitPages
  • Book a callContact
  • Download ResumeContact
Let’s talk

I’ll build the agent for it.

What Is Improper Output Handling?

What improper output handling is, where a model's reply can do harm in SQL, web pages and shell commands, and how to fix it, explained simply for your AI interview.

In short

Improper output handling is when an app uses an AI model's reply without checking it first, so the text can do harm in a database, a web page or a shell. OWASP lists it as LLM10 in its 2026 Top 10. The fix is to treat the reply like any user's input.

12 min readIntermediateCommon · estimateFacts checked 26 November 2026

Say it out loud

Read it once, then say it without looking. Start with the short one; use the long one when they ask you to go deeper.

Improper output handling means my app uses an AI model's reply without checking it first. The reply might go into a database query, a web page or a shell command, and a hidden instruction can make it carry harm. OWASP lists it as LLM10 in its 2026 list. My fix is to treat the reply like any user's input: allow-list the values, encode the text for where it goes, and bind SQL values as data.

What is improper output handling, in plain words?

An AI model writes text, and your app then does something with it. The text might go into a database query, appear on a web page, run as a command, or fill an email template. Improper output handling is when the app uses that text without checking it first.

Picture a bank clerk who types whatever a customer writes on a form straight into the core system, without checking any field. One unexpected line can change far more than the form was meant to change. An AI reply does the same when an app uses it unchecked.

A made-up but realistic case: a credit analyst asks an assistant for overdue loans in Pune. A trick hidden in a loan note gets copied into the reply, and the app pastes that reply into a database query. The query then also returns loans from other branches.

Key words

Model output
The text an AI model writes back to your app.
Downstream
Any later part of your app, or another system, that uses the text.
Encode
Turn text into a safe form for its place, so a tag shows as words, not as HTML.
Allow-list
A short list of values you accept. Everything else is refused.

Where does model output go wrong?

  • SQL

    The reply becomes a query

    An assistant writes the search for a loan report. Pasted in unchecked, extra text in the reply changes which rows come back.

  • Web page

    The reply runs as HTML

    A claims chatbot shows its reply on the customer's page as raw HTML. An image tag in the reply with an onerror handler then runs script in the browser.

  • Shell

    The reply runs as a command

    A log-search agent turns a request into a shell command. Run as written, one extra command can delete files.

OWASP's 2026 entry lists the same kinds of harm: SQL injection from LLM-written queries, scripts that run in a browser, and shell commands. One example is code that gets built and deployed with no human review.

Two ways model output gets used

Model reply

text the model wrote

Passed straight on

Used as SQL or HTML

no check on the way

Checked first

Check and encode

allowed values, escaped text

Attack runs

data leaks or code runs

Safe use

the reply is only text

  1. 1The reply goes straight into a query or a web page.
  2. 2Hidden commands then run inside the database or the browser.
  3. 3Otherwise the same reply is checked and encoded first.
  4. 4What is left is plain text, so the attack has no route into the query or page.
The same model reply takes two paths. Used as it is, it can carry an attack into a query or a page. Checked and encoded first, it stays plain text. Follow the numbered arrows for the order.

A quieter case: some chat screens turn Markdown images into pictures on their own. If the model writes an image link whose web address carries private data, the browser fetches that address, and the data leaves in the request. OWASP names this as an outside request the screen makes automatically.

This is different from a wrong answer. OWASP keeps a separate risk for output that is incorrect or misleading, called misinformation. Improper output handling is about what the app does with the text.

  • “Treat the model as any other user, adopting a zero-trust approach, and apply proper input validation on responses coming from the model to backend functions.”

    OWASP, LLM10:2026

  • “XSS happens when an application puts untrusted data into a page in a way that lets the browser treat it as code.”

    OWASP, XSS Prevention Cheat Sheet

What does the bad case look like in code?

Here is a small sketch using SQLite, an in-memory database. The reply carries the trick from the loan note. The unsafe line pastes the reply into the SQL text, so the query returns every Pune loan, plus any loan over 30 days overdue from any branch. In a real app the allow-list check comes first and rejects the reply before any query runs. The sketch runs the unsafe line only to show the damage, and binding the reply as a value means the database never reads it as SQL.

Python
import sqlite3

db = sqlite3.connect(":memory:")
db.execute("CREATE TABLE loans (id INTEGER, branch TEXT, amount INTEGER, overdue_days INTEGER)")
db.executemany("INSERT INTO loans VALUES (?, ?, ?, ?)", [
    (1, "Pune", 250000, 45),
    (2, "Pune", 90000, 0),
    (3, "Chennai", 410000, 120),
])
ALLOWED = {"Pune", "Chennai"}
QUERY = "SELECT id, amount FROM loans WHERE branch = ? AND overdue_days > 30"

# The assistant was asked for overdue Pune loans. A trick hidden in a loan note
# was copied into its reply.
model_reply = "Pune' OR '1'='1"

# Unsafe: the reply becomes part of the SQL text.
unsafe = f"SELECT id, amount FROM loans WHERE branch = '{model_reply}' AND overdue_days > 30"
print("Unsafe:", db.execute(unsafe).fetchall())

# Safer: check the reply against the list of branches we allow.
if model_reply not in ALLOWED:
    print("Rejected:", repr(model_reply), "is not a known branch")

# Also safe: the reply is passed as a value, so it can't change the query.
print("Bound as a value:", db.execute(QUERY, (model_reply,)).fetchall())

Output

Unsafe: [(1, 250000), (2, 90000), (3, 410000)]
Rejected: "Pune' OR '1'='1" is not a known branch
Bound as a value: []

The first line of output is the damage: all three loans come back, Chennai included, when only Pune's overdue loans were asked for. The sketch is simplified, but a real app would also log every rejected reply.

How do you stop it?

Treat the reply as untrusted input, then check it at each place it goes. The steps below work together, and no single one is enough on its own.

Checks before model output is used

Model reply

text nobody has checked yet

Your code, in order

Allow-list values

only known branches or actions

Encode for the place

escape HTML, bind SQL values

Least access

tools reach only what the task needs

Only data gets through

a bad reply has far less room to do harm

  1. 1The reply reaches your code first, not the database or page.
  2. 2Values that pass the list move on to encoding.
  3. 3Whatever passes is escaped or bound for the place it goes.
  4. 4Tools then get only the access the task needs, so any harm stays small.
Your code checks the reply before anything uses it. Allowed values come first, then encoding for the place it goes, then tools with the least access. Only plain data gets through.
  1. 1

    Allow-list what the model may choose

    If the reply should be a branch, a status or a date, check it against a short list or a strict format. Refuse everything else.

  2. 2

    Encode for each place

    A web page, a query and a shell each need their own safe form. Escape text for the page, and never paste model text into HTML as is.

  3. 3

    Bind SQL values as data

    Keep the shape of the query in code, and send the model's words only as values. The database then never reads them as SQL. Binding covers values only, not table or column names, so allow-list those.

  4. 4

    Don't run what the model writes

    Shell commands and generated code run in a sandbox with no production keys, after an allow-list check and a person's approval. OWASP warns against code that is deployed with no review.

  5. 5

    Turn off automatic fetching

    Don't let the chat screen load images or links from model text by default. Allow only the hosts you trust.

  6. 6

    Log and watch the output

    Keep the replies and the rejected ones, so odd patterns show up early.

Tip

Asked how you'd prevent it? Say that your code checks the reply before the database, page or shell sees it, and that the model never decides what runs on its own.

How would you let an assistant write SQL for analysts?

A bank wants analysts to ask questions in plain English and get numbers back from its loan database. Your answer should cover these points:

  • Read-only copy

    The assistant queries a read-only copy, so a bad query can't change a row.

  • Fixed tables

    It may use only a fixed list of tables and columns. Anything else is refused.

  • Limits on every query

    Each query gets a row limit and a time limit, so one bad query can't tie up the database.

  • Values, not HTML

    Results appear as a table of values, never as HTML written by the model.

  • Person signs off

    Writes, deletes and schema changes need a person's approval first.

Be honest about the limit. Those steps cannot make the model's SQL correct. They make a wrong query safe to run.

What does the interviewer want to hear?

A strong answer covers

  • A plain definition: the app uses model output without checking it, and the text can do harm.
  • Where it lands: SQL, a web page, a shell, an email or a file path.
  • Treat the model like an untrusted user, with zero trust.
  • Fixes by place: allow-lists, encoding, bound SQL values and sandboxes.
  • OWASP's place: LLM10 in 2026, LLM05 in 2025, and how it differs from misinformation.
  • An honest limit: the model can still be wrong, so the aim is to limit the damage.

Common traps

  • Trap"The model is our own AI, so its output is safe."

    BetterThe reply can be steered by text anyone can write. Check it like any user input.

  • Trap"Escaping the output fixes it everywhere."

    BetterEach place needs its own safe form. HTML, SQL and shell differ, so encode for each one.

  • Trap"Our prompt says to only return a branch name."

    BetterA prompt is a request, not a check. Validate the value in code before you use it.

Can you spot the danger?

Spot the danger

Your claims chatbot shows the model's reply on the customer's page. Which reply can make the customer's browser run a script?

Quick check

Your agent turns a request into a shell command that searches server logs. What is the safest first step?

Can you answer these from memory?

Flash cards · tap a card to flip it

1 / 6

What will they ask next?

  1. Q201What is prompt injection?12 min
  2. Q209How do you build defence in depth against injection?13 min
  3. Q213What is sensitive information disclosure?12 min
  4. Q215What is data and model poisoning?13 min
  5. Q217What is unbounded consumption ("denial of wallet")?12 min
  6. Q221What is red teaming?10 min

Learn it in depth

Last updated on 26 November 2026

Building an AI feature that writes queries, pages or commands?

I design and build AI agents and the guardrails around them, including the checks between a model's reply and the systems it touches. Book a short call and tell me what yours needs to do.