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.

Lab 8: Structured output and tools

Objectives: LO-8.1, LO-8.2, LO-8.3
Prerequisites: the Module 8 reading
Time: about 2.5 hours

In this lab you validate dispatch records against a JSON Schema, write three tool definitions whose descriptions tell a model when to use each tool, and build the tool-use loop: run each call, return its result or its error, and stop on the right stop reason.

No cell sends anything over the network. Where a cell needs a model, a ScriptedModel from promptlab answers instead, from a script written in the cell. Its responses have the shape of real ones, but a person wrote them, so they are illustrative, not model output. The tools read a small fictional menu, MENU, and change nothing.

Each exercise ends with a check cell that prints passed or fails with a message. Run the cells in order, from the top. Some cells fail on purpose until you finish the exercise above them.

Setup

Predict validity

Both objects below are valid JSON. Predict which one satisfies SCHEMA, then run the cell.

Exercise L8.1 (LO-8.1, ★★, about 25 minutes)

Check the response contract. Write parse_record(text). Parse text as JSON, validate the value against SCHEMA, and return the record. A summary that is blank or holds only spaces is invalid too, even though it passes minLength. Turn the errors you expect, a parse error and a schema error, into ValueError, so the caller has one kind of error to handle.

Exercise L8.2 (LO-8.2, ★★, about 25 minutes)

Write tool specifications. Write specs(), which returns a list of three tool definitions, each with a name, a description, and an input_schema. Use the names and input schemas in the setup’s TOOLS, in the same order, but write your own descriptions. Those in TOOLS are one sentence long, and Anthropic’s guide to defining tools recommends at least three to four sentences for each. Say what each tool returns, when to use it, and what it doesn’t cover, so the descriptions tell a price from a bench time. The lab’s run_tool already maps these names to the fictional menu.

Exercise L8.3 (LO-8.3, ★★, about 25 minutes)

Handle completed and incomplete turns. Write terminal(response), which returns a pair with the stop reason first. For "end_turn", the second item is the response’s text, joined from every text block as Module 2 did. For every other stop reason, it is None.

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

Complete the loop. Write tool_loop(model, ticket, max_turns=4), which implements the reading’s tool-use loop with the setup’s TOOLS and run_tool:

  • Start the history with the ticket as a user message, and send TOOLS with every request.

  • On a "tool_use" stop, forward the response’s whole content as an assistant message, text blocks included, then one user message with a result for every tool_use block. A "tool_use" stop with no tool_use blocks raises ValueError.

  • On any other stop reason, return the same pair as terminal.

  • After max_turns requests without a final answer, return ("turn_limit", None).

Summary

  • json.loads checks syntax, jsonschema checks a record against its schema, and your own code checks what a schema can’t, such as a summary of spaces only. One ValueError reports every failure to the caller.

  • A tool’s description tells a model what the tool returns and when to use it. The host still validates every input, and answers a bad one with an error result.

  • The tool-use loop forwards the model’s whole turn, returns one result per call in a single user message, matched by tool_use_id, and stops on any other stop reason or at its turn limit.

  • The stand-in tested the loop’s plumbing. Whether a real model picks the right tool from your descriptions needs an evaluation against that model.