An inventory agent asks an LLM for a JSON reorder decision and passes the fields to a typed helper. This time the model returned units as a string. What happens when the snippet runs?
import json
def reorder_qty(units: int, packs: int) -> int:
return units * packs
out = json.loads('{"units": "12", "packs": 3}') # LLM output
print(reorder_qty(out["units"], out["packs"]))Show the answer and explanation
Correct answer: B, It prints 121212: annotations are not enforced at runtime, so "12" * 3 repeats the string, and the agent receives a wrong quantity without any exception being raised.
Explanation Python
Type hints are metadata. CPython exposes them through __annotations__ but never checks them when a function is called. reorder_qty("12", 3) therefore runs the body with a string, and str * int is valid Python: it repeats the string. The function returns "121212", and an agent that trusts it might order 121,212 units, with no exception anywhere.
Static checkers such as mypy or pyright cannot catch this either, because json.loads returns Any: the wrong type only exists at runtime, in data produced by the model.
The fix is to validate at the boundary where untrusted data enters:
from pydantic import BaseModel
class Reorder(BaseModel):
units: int
packs: int
r = Reorder.model_validate_json(raw) # "12" -> 12, "twelve" -> ValidationErrorIn its default (lax) mode pydantic coerces a clean numeric string like "12" to an int and raises a ValidationError for values like "twelve"; strict mode rejects strings for int fields altogether. Alternatives are a JSON Schema check or the provider's structured-output mode, still followed by validation and a retry when it fails.
Why the distractors fail: plain annotations never trigger conversion or call-time checks (pydantic coerces "12" only because a model or @validate_call validates the data explicitly), and str * int is defined (it is repetition), which is exactly why the bug is silent.
Interview tip: describe LLM output as untrusted input: parse it, validate it against a schema, apply business range checks (e.g. units between 1 and 500), and retry or escalate to a human when validation fails.