𝗧𝗵𝗲 𝗡𝘂𝗹𝗹 𝗜𝗻𝗽𝘂𝘁 𝗧𝗵𝗮𝘁 𝗕𝗿𝗼𝗸𝗲 𝗠𝘆 𝗣𝗿𝗼𝗱𝘂𝗰𝘁𝗶𝗼𝗻 𝗔𝗴𝗲𝗻𝘁
The demo ran perfectly for three weeks. Every test input worked. Every output went to the right place. I thought the system was reliable.
Then a supplier sent an email with an empty subject line.
The agent expected a string to extract an order reference. Instead, it received a null value. It did not crash. That would have been better. It generated a fake order reference that looked real. The downstream system processed it. Nobody noticed for four hours.
Demos use inputs you expect. Production uses inputs you do not expect.
I run the agent operation at aienterprise.dk. I saw the full trace. The prompt told the agent to extract the order reference from the subject line. This works if the subject line exists.
If the subject line is missing, a large language model fills the gap. It invents something that looks correct. This is not random noise. It is structured noise. It is dangerous because it looks right. You can catch a failure. You cannot easily catch a confident, wrong answer.
I did not retrain the model. I did not change the prompt. I added a guard before the model call.
Now, a simple check runs first. It asks: is the subject field present and non-empty? If the answer is no, the message goes to a hold queue for a human. The agent never sees the bad input.
This guard is twelve lines of code. It is the most important thing I built this year.
The pattern is simple. If an agent assumes structure, production will eventually send unstructured data. The fix is not a smarter model. The fix is a boundary. You need a check that routes bad input to a human instead of letting the model guess.
Reliability is the only feature. A demo shows an agent can do a task. Production shows an agent does the task at 3 AM on bad input. Only the second part matters to your customers.
My agent now processes 200 operations per day without issues. The hold queue triggers twice a week. A human reviews the odd data. I learn what production looks like.
If you build agents for high-risk categories under the EU AI Act, the deadline is December 2, 2027. This includes employment, biometrics, and education. A system that guesses on bad input will fail an audit. This guard is a compliance minimum.
Reliability is not a feature you add later.
L'input null che ha rotto il mio agente in produzione e come l'ho risolto
Stavo gestendo un agente in produzione che sembrava funzionare perfettamente. Tutto andava liscio, le risposte erano accurate e il sistema era stabile. Poi, improvvisamente, tutto è andato in crash.
L'errore era semplice, ma devastante:
TypeError: expected string or bytes-like object, got 'NoneType'
Il colpevole: un input null
In un sistema basato su LLM (Large Language Models), tendiamo a dare per scontato che se un utente invia un messaggio, quel messaggio contenga del testo. Tuttavia, in un ambiente di produzione reale, i dati non sono sempre puliti.
Il problema è nato da un'integrazione con un'API esterna. Il mio agente si aspettava di ricevere una stringa che contenesse il contesto della conversazione, ma a causa di un bug nell'API a monte, quel valore è arrivato come null (o None in Python).
Quando il mio codice ha tentato di manipolare quella stringa (ad esempio, per pulirla o passarla al prompt), il sistema è andato in crash perché non può eseguire operazioni su un oggetto di tipo NoneType.
Come l'ho risolto: Validazione e Schemi
La soluzione non è stata semplicemente aggiungere un if input is not None, ma implementare una strategia di validazione più robusta.
1. Utilizzo di Pydantic per la validazione dei dati
Inveve di passare dati grezzi attraverso il sistema, ho iniziato a utilizzare Pydantic per definire schemi rigorosi per ogni input e output. Con Pydantic, posso definire esattamente cosa mi aspetto e come gestire i valori mancanti.
from pydantic import BaseModel, Field, validator
from typing import Optional
class AgentInput(BaseModel):
user_query: str = Field(..., min_length=1)
context: Optional[str] = ""
@validator('context')
def context_must_be_string(cls, v):
if v is None:
return ""
return v
In questo modo, se l'API a monte restituisce null per il campo context, Pydantic lo converte automaticamente in una stringa vuota, evitando il TypeError.
2. Gestione degli errori "fail-safe"
Ho anche aggiunto dei blocchi try-except specifici per gestire i casi in cui la validazione fallisce, permettendo all'agente di rispondere con un messaggio di errore predefinito invece di interrompere l'intero processo.
Lezioni imparate
- Non fidarti mai dell'input: Anche se pensi che un dato provenga da una fonte affidabile, trattalo sempre come potenzialmente corrotto o incompleto.
- Usa schemi forti: Strumenti come Pydantic sono essenziali per costruire agenti AI affidabili.
- Monitoraggio: Implementare un monitoraggio degli errori che catturi specificamente i
TypeErrorpuò aiutarti a identificare problemi di schema prima che diventino critici.
Costruire agenti AI è facile; costruire agenti AI pronti per la produzione è un'altra storia.