Analyseur d’adresses postales
Les formulaires de paiement, les CRM, les outils d’expédition et les migrations de données ont tous besoin d’adresses sous forme de champs structurés, et non d’un seul bloc de texte libre. Ce prompt découpe une adresse postale en rue, ville, état ou province, code postal, pays et type d’adresse, pour qu’elle puisse être validée, géocodée ou imprimée sur une étiquette sans découpage fragile de chaînes. Il est délibérément indépendant du pays : le motif du code postal accepte lettres, chiffres, espaces et traits d’union, couvrant des formats allant des codes ZIP américains aux codes postaux canadiens et britanniques sans prétendre valider les règles d’un seul pays. Les pièges sont bien réels : les numéros d’appartement et de suite se cachent dans la ligne de rue, les cases postales n’ont pas de rue du tout, et les noms d’état arrivent abrégés, en toutes lettres, ou absents. Quand un élément est absent, ce schéma refuse de deviner — un champ manquant signifie aucune sortie, et non une ville hallucinée.
Prompt système
You are a postal address parser. Read the address below and return a single JSON object that matches the provided schema exactly. Split the address into street, city, state_or_province, postal_code, and country. The country must be the two-letter ISO country code in uppercase, for example US, CA, GB, FR. Classify address_type as residential for a home address, commercial for a business or office address, or po_box when the address is a post office box. The postal_code is the code exactly as written in the address, keeping its letters, digits, spaces, and hyphens as they appear. Rules: - Output only the JSON object, with no extra text, no markdown fences, and no commentary. - Extract the best value present in the address. Do not invent a city, postal code, or any other field that is not in the address. - If a required field cannot be determined from the address, do not produce any output. - Every text field must be non-empty. The country code must be exactly two uppercase letters.
Schéma JSON brut
{
"$schema": "http://json-schema.org/draft-07/schema#",
"title": "PostalAddress",
"type": "object",
"additionalProperties": false,
"required": ["street", "city", "state_or_province", "postal_code", "country", "address_type"],
"properties": {
"street": {
"type": "string",
"minLength": 1
},
"city": {
"type": "string",
"minLength": 1
},
"state_or_province": {
"type": "string",
"minLength": 1
},
"postal_code": {
"type": "string",
"minLength": 1,
"pattern": "^[A-Za-z0-9]+(?:[ -][A-Za-z0-9]+)*$"
},
"country": {
"type": "string",
"pattern": "^[A-Z]{2}$"
},
"address_type": {
"type": "string",
"enum": ["residential", "commercial", "po_box"]
}
}
}
Zod (TypeScript)
import { z } from "zod";
export const PostalAddressSchema = z.strictObject({
street: z.string().min(1),
city: z.string().min(1),
state_or_province: z.string().min(1),
postal_code: z.string().min(1).regex(/^[A-Za-z0-9]+(?:[ -][A-Za-z0-9]+)*$/),
country: z.string().regex(/^[A-Z]{2}$/),
address_type: z.enum(["residential", "commercial", "po_box"]),
});
export type PostalAddress = z.infer<typeof PostalAddressSchema>;
Pydantic v2 (Python)
from pydantic import BaseModel, ConfigDict, Field, field_validator
from typing import Literal
class PostalAddress(BaseModel):
model_config = ConfigDict(extra="forbid")
street: str = Field(..., min_length=1)
city: str = Field(..., min_length=1)
state_or_province: str = Field(..., min_length=1)
postal_code: str = Field(..., min_length=1, pattern=r"^[A-Za-z0-9]+(?:[ -][A-Za-z0-9]+)*$")
country: str = Field(..., pattern=r"^[A-Z]{2}$")
address_type: Literal["residential", "commercial", "po_box"]
@field_validator("street", "city", "state_or_province", "postal_code", "country", mode="before")
@classmethod
def _strip_text(cls, v):
if not isinstance(v, str):
return v
return v.strip()