← Claude & OpenAI Made Easy
18 min

Structured Outputs and Tool Calls

Get answers your software can read, and let the model ask your code for help, safely.

Lesson 4 of 11
0 done
In plain English

Structured outputs make answers match a schema, so software can read them. In a tool call, the model asks and your code does the work. Write clear tool descriptions, keep the list short and check for refusals.

Schema
A plan of fields and types that the answer must follow.
Tool call
The model's request for your code to run something.
Refusal
A safety reply that may not match your schema.
Picture it
What a tool call looks like
1
Model asks
Name and inputs
2
Your code runs it
The model never runs your code
3
Result goes back
Tied to the call
4
Model answers
Or asks again

A chat answer is text for people. Software needs more. It needs a fixed shape it can read. It also needs a way to ask the world for things, such as weather or an order status. Both companies solve this with two features. Structured outputs give answers in a set shape. Tool calls let the model ask your code to do something.

Structured outputs

You give the model a schema. A schema is a plan of the fields you want and their types. The model then must follow it. OpenAI describes the difference clearly. JSON mode only promises valid JSON. Structured outputs promise that the JSON matches your schema.

  • Required fields are always there.
  • A field that must be one of three words will be one of those three.
  • You do not need to retry because the shape was wrong.
a-schema-in-words.txt
Return an object with:
  category   one of: billing, delivery, other
  urgent     true or false
  summary    text, one sentence

Tool calls in plain steps

Tool use is also called function calling. Both docs describe the same loop.

Write tool descriptions like a good label

  • Use a clear name and say what the tool does and when to use it.
  • Describe each input and its format. Add an example when mistakes repeat.
  • Use fixed choices where you can, so bad values are hard to send.
  • Do not ask the model for things your code already knows, such as an ID from a previous step.
  • If two tools are always used together, merge them.
  • Keep the list short. The OpenAI docs suggest staying below about twenty.

Check for the unexpected

  • The model may refuse a request for safety reasons. A refusal might not match your schema, so your code must check.
  • A reply can be cut off at the length limit. Check that it finished.
  • The model will try to fit any input into the schema. Tell it what to do when the input makes no sense.

A small example

Imagine a support tool. A customer writes in. You want the model to label the message and then look up the order. First, a structured output labels the message as billing, delivery or other. Your code reads that label with no guessing. Next, a tool call asks your system for the order status. The model then writes a friendly reply from the real data.

Notice the split. The model handles language. Your code handles facts and actions. This split makes the whole system easier to test and to trust.

When something fails, look at each step alone. Was the label right? Was the tool called with the right inputs? Was the reply true to the data? Small steps are easier to fix than one big blur.

→Test a tool by giving only its description to a colleague. If they would use it wrongly, the model will too.
⚠A tool that can change things, such as sending money, needs a human check. Do not let a model approve its own risky actions.
Key takeaways
  • ✓Structured outputs make answers match a schema you define. JSON mode only promises valid JSON.
  • ✓In a tool call, the model asks and your code acts. The model never runs your code.
  • ✓Good tool descriptions are clear, short and say when to use the tool.
  • ✓Always check for refusals and cut-off answers.
  • ✓Put a human check in front of risky actions.
Hands-on exercise

Design a tiny tool

  1. Pick a job, such as looking up an order status.
  2. Write the tool name, a one-line purpose and its inputs with formats.
  3. Give the description to a friend. Ask them how they would call it.
  4. Fix anything they got wrong, then write what the tool should return on an error.
Check yourself
Q1What does a schema add that JSON mode does not?show answer

It makes the JSON match your fields and types, not only be valid.

Q2Who runs the code in a tool call?show answer

Your application. The model only asks for the call.

Q3Why must code check for refusals?show answer

A refusal may not follow the schema. Unchecked, it can break the next step.

← Thinking Models: Effort, Cost and When They Pay OffPrompt Caching: Pay Less for Repeated Text →
0 / 11 finished0%