Skip to article frontmatterSkip to article content
Site not loading correctly?

This may be due to an incorrect BASE_URL configuration. See the MyST Documentation for reference.

These exercises practice retrieving what the Module 8 reading explained. They aren’t graded. Work each one on paper first, then check your answer, and only then confirm it with code from Lab 8 if you like. The dispatch schema is the one in Lab 8’s setup: queue is one of repair, billing, or account, summary is a string of 1–80 characters, both are required, and no other field is allowed. The answers are in the collapsed blocks; open one only after you have an answer of your own.

Worked example: a record that fails, and one that misleads

Ticket T13 says “Where can I pay the remaining balance?”, and its label is billing. Write one record that the dispatch schema rejects, and one that the schema accepts but that routes T13 to the wrong queue.

  1. To fail the schema, break one rule. {"queue": "payments," "summary": "Pay remaining balance"} names a queue that isn’t in the enum. Leaving out summary, or adding a field such as "urgent": true, would fail too.

  2. To pass the schema, every rule must hold: a queue from the enum, a summary of 1–80 characters, and nothing else. {"queue": "account", "summary": "Pay remaining balance"} holds all of them.

  3. The second record is still wrong, because T13’s label is billing. Only a comparison with the label finds that.

Exercise P8.1 (LO-8.1, ★, about 10 minutes)

Write one record that fails the dispatch schema, and one that passes the schema but misroutes a ticket about a broken brake.

Hint 1

The first record has to break a rule the schema states. The second has to keep every rule.

Hint 2

For the second record, choose a queue from the enum that isn’t repair.

Answer
  • Fails the schema: {"queue": "repair"}. It has no summary, which required demands, so jsonschema rejects it with 'summary' is a required property.

  • Passes, but misroutes: {"queue": "account", "summary": "Broken brake"}. Every schema rule holds, but a broken brake is a mechanical repair, so its queue is repair. Only a comparison with a labeled answer, as in Module 7, catches it.

A common mistake is a second record such as {"queue": "brakes", "summary": "Broken brake"}. brakes isn’t in the enum, so this record fails the schema too.

Worked example: a read-only tool

The handbook’s section H04 says the shop has two loan bicycles, due back by 17:00 the same day. Define a tool that reports how many are free right now.

  1. Name it for what it does: count_free_loan_bikes. It only reads, and its name says what it returns.

  2. Describe it in at least three sentences: “Return how many of the shop’s two loan bicycles are free right now, as a whole number from 0 to 2. Use it when a customer asks whether they can borrow a bicycle while theirs is repaired. It doesn’t reserve a bicycle, and it says nothing about when a loaned one comes back. A rider must sign the loan form before collecting one.”

  3. Give it an input schema: it needs no input, so the schema is {"type": "object", "properties": {}, "additionalProperties": false}.

The description says what comes back, when to use the tool, and what it doesn’t do, so the model isn’t left to guess whether a call books a bicycle.

Exercise P8.2 (LO-8.2, ★★, about 15 minutes)

Propose a name and an input schema for a tool that reads whether a fictional service is offered. What does it return for an unknown service?

Hint 1

The tool reads, and changes nothing. Let its name say so.

Hint 2

The input is one required string. Ask yourself what a model should do differently when the service isn’t on the menu, and when it is offered but booked up.

Answer

A model answer:

  • Name: service_availability.

  • Description: “Report whether the shop offers a named service, such as flat or brakes, and whether a bench slot is open this week. Use it when a customer asks if the shop can do a repair. It doesn’t book the repair and gives no price; use find_service for prices. An unknown service returns an error result that lists the services the shop offers.”

  • Input schema: {"type": "object," "properties": {"service": {"type": "string"}}, "required": ["service"], "additionalProperties": false}.

For an unknown service, return a tool_result with is_error set to true and a message such as “Unknown service: chain. The shop offers: flat, brakes.” The message tells the model what to try next. A common mistake is answering “not available” for an unknown service. That reads the same as “offered, but booked up,” so the model can’t tell the customer which one is true.

Worked example: answering a failed call

A response’s only call has ID c1 and asks shop_hours for "Sunday". The tool’s enum allows only Monday and Friday, so the call fails. The next request adds the response’s content as an assistant message, then this user message:

{"role": "user", "content": [
    {"type": "tool_result", "tool_use_id": "c1",
     "content": "Unknown day: Sunday. Hours are known for Monday and Friday.", "is_error": True},
]}

The failure is reported as data. The model sees which call failed and why, and can answer the customer or try another day.

Exercise P8.3 (LO-8.3, ★★, about 15 minutes)

One assistant turn holds two calls, with IDs p1 and h1. p1 succeeds and h1 fails. Outline the next message.

Hint 1

Every call in the turn needs a result, including the one that failed.

Hint 2

Count the user messages you are about to send. The parallel tool use guide says how many there should be.

Answer

One user message whose content starts with two tool_result blocks: one with tool_use_id p1 and the tool’s ordinary output, and one with tool_use_id h1, is_error set to true, and a message that says what went wrong. It comes right after the assistant message that holds both calls.

Three mistakes are common. Leaving out h1’s result makes the API reject the request, because a tool_use block has no tool_result. Sending the two results in separate user messages makes the model less likely to call tools in parallel. Putting text before the results breaks the rule that results come first.

Worked example: counting attempts

A work order allows two retries. Request 1 is cut off at max_tokens, and request 2 passes every check. The program sent two requests, and returns the record from request 2. Had request 2 failed too, the program would send request 3, its last allowed attempt: one first attempt plus two retries.

Exercise P8.4 (LO-8.1, LO-8.3, ★★★, about 25 minutes)

A JSON response fails validation twice, with a retry limit of one. What happens?

Hint 1

The first call is followed by at most one retry.

Hint 2

Think about what the caller needs to choose a fallback.

Answer

The program sent two requests: the first and its one retry. Both failed, so it stops and returns an explicit failure, such as a ValueError that holds the last response and the reason it failed. The caller then decides the fallback, for example sending the ticket to a person’s queue.

A common mistake is to return the last record anyway, which hands invalid data to code that trusts it. Another is to keep retrying, which costs a request each time and may never succeed.