Menu
CoddyTech

Structured Triage

Part of the Calling Models From Code section of Coddy's AI Engineering journey. Lesson 17 of 22.

challenge icon

Challenge

Medium

A 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.

  1. Add import json under the first line, and a SCHEMA dict after the client: an object with category (a string, enum "billing", "shipping", "account", "technical", "other"), urgent (a boolean) and summary (a string), all three required, "additionalProperties": False.
  2. Before the loop, create an empty list results.
  3. In the loop, send the ticket itself as the message (remove Summarize: ) and add output_config={"format": {"type": "json_schema", "schema": SCHEMA}}.
  4. Parse the text block with json.loads, append the dict to results, 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 immediately

Try 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