Structured Triage
Part of the Calling Models From Code section of Coddy's AI Engineering journey. Lesson 17 of 22.
Challenge
MediumA summary is text for a person. To sort tickets, your code needs fields it can read. In this step, replace the summary request with structured output.
- Add
import jsonunder the first line, and aSCHEMAdict after the client: an object withcategory(a string, enum"billing","shipping","account","technical","other"),urgent(a boolean) andsummary(a string), all three required,"additionalProperties": False. - Before the loop, create an empty list
results. - In the loop, send the ticket itself as the message (remove
Summarize:) and addoutput_config={"format": {"type": "json_schema", "schema": SCHEMA}}. - Parse the text block with
json.loads, append the dict toresults, and print#<number> <category> | urgent: <yes or no> | <summary>instead of the summary line.
Keep reading the input and printing Tickets: <count> as before. For example, for the input 1 and My parcel arrived damaged and I need a new one immediately. the program prints:
Tickets: 1
#1 shipping | urgent: yes | My parcel arrived damaged and I need a new one immediatelyTry it yourself
import coddy_ai as anthropic # in production: import anthropic
client = anthropic.Anthropic()
count = int(input())
tickets = [input() for _ in range(count)]
print(f"Tickets: {count}")
# For each ticket, ask the model to summarize it and print "#<number> " and the reply
for number, ticket in enumerate(tickets, start=1):
response = client.messages.create(
model="claude-opus-5-5",
max_tokens=200,
messages=[{"role": "user", "content": "Summarize: " + ticket}],
)
for block in response.content:
if block.type == "text":
print(f"#{number} {block.text}")
All lessons in Calling Models From Code
Practice on your own: Online Python compiler