The Corner Shop's inbox has six messages. The support team wants a queue: what each message is about, how urgent it is and which order it concerns. This project puts the whole module together: a schema, structured outputs, your own checks, a retry with the error and a fallback.
Shape is not truth: with structured outputs the API keeps every reply to your schema. The schema fixes the shape of a reply. It can't know if the values make sense. Here is the idea on another value, the stars of a product review:
schema: "stars": {"type": "integer"}
reply: {"stars": 9} fits the schema, but the scale is 1 to 5
So your code checks what a schema can't: a range, or a value that must exist in your data. When a check fails, you send the model's reply back with the problems and it tries again. Some problems no retry can fix, because the mistake is in the message, not in the model. After three attempts the program returns a fallback ticket: a safe default marked needs_human, with the reason. One bad message never stops the others.
When your program is done, it prints the queue, most urgent first. The check also reads what you sent: ten calls to the model, and a schema with the three fields, the four categories and an order_id that can be null.
3 shipping 7 | THIRD EMAIL!!! Where is order 7?! Answer me NOW! 3 other None | Order 99 never arrived. Please refund me. needs a human: order 99 does not exist 2 refund 4 | Order 4 arrived with a cracked lid. I want my money back. 2 shipping None | I ordered on May 12 and my package still isn't here. 1 product None | Do you sell the teapot in blue? 1 other None | Thank you, the plant looks great on my desk!
Your task: the program already asks the model about each message and prints one line per ticket. Describe the three fields in SCHEMA and send it with output_config. Return the fallback ticket after three failed attempts. Write the two checks in check: an urgency from 1 to 3, and an order that exists in ORDER_IDS. Send the problems back on a retry, then sort the queue by urgency.
Assignment
Follow the Coding Tutorial to turn the six inbox messages into a checked, sorted support queue.
Hint
A schema changes nothing until it is sent with the call in ask. The fallback goes below the for loop, still inside triage. The two messages of the retry go at the end of the loop's body, not inside the if. Sort after the loop that fills queue, with reverse=True, because 3 is the most urgent.
Free plan: 30 tutor messages. PRO is unlimited.
