Careeroza
Output Format
Ask for a Format Your Code Can Read
If code reads the reply, say exactly what format to use: one word, a bullet list, key: value lines, or JSON. Describe it and show it. (The medium level covers guaranteed JSON with schemas.)
Parsing a Formatted Reply
Example
We ask the model for key: value lines. The text below is a stand-in for the model's reply; the parser checks every required field is present:
prompt = """Extract details from the review. Reply in exactly this format:
product: <product name>
rating: <1-5>
issue: <short phrase or none>"""
model_reply = """product: Stride Pro
rating: 2
issue: sole came off after 2 weeks"""
def parse(reply, required=("product", "rating", "issue")):
data = {}
for line in reply.strip().splitlines():
key, sep, value = line.partition(":")
if sep:
data[key.strip().lower()] = value.strip()
missing = [k for k in required if k not in data]
if missing:
raise ValueError(f"missing fields: {missing}")
data["rating"] = int(data["rating"])
return data
print(parse(model_reply))
try:
parse("The product is Stride Pro and it is bad.")
except ValueError as e:
print("bad reply ->", e)Output
{'product': 'Stride Pro', 'rating': 2, 'issue': 'sole came off after 2 weeks'}
bad reply -> missing fields: ['product', 'rating', 'issue']Tip: Always validate the model's output in code. When it fails, retry the call or fall back to a safe default.