Skip to main content
Here we build a small Pydantic AI agent whose model is Jev. Many of an agent’s steps are decisions: which tool to call, which queue a message goes to, whether a customer is reporting fraud. An LLM can make those decisions, but it answers in text and doesn’t say how sure it is. Pydantic AI has a model class for Jev, TypeSafeModel. You give the agent a Pydantic model as its output type, and each field becomes one question for Jev. Jev answers all the questions in one request. TypeSafeModel turns each answer into a Python value of the field’s type, and also returns Jev’s probabilities and confidence. The agent triages messages sent to a bank’s support team. It asks two questions about every message: what the message is about, and whether the customer reports fraud. You end up with a Triage output type and a triage() function, which returns the agent’s output for one message with Jev’s probabilities and confidence. You then decide in code whether the fraud probability is high enough to send the message to the fraud team.

Setup

Run in Colab

Runs as published without an API key.

Python file

This page as one .py file, to run locally.

Open in playground

Edit one request and run it live. Needs a TypeSafe account.
Requires Python 3.11 or later.
With uv, uv run pydantic_ai_quickstart.py installs the dependencies for you. The code replays saved answers, so it runs as published without an API key. To change a request, set TYPESAFE_API_KEY; in Colab, add it in the Secrets panel. The numbers below came from jev-1.14.0 on 2026-11-25.

Write the questions as a type

Pydantic AI builds the Jev request from your types and from the message you pass to the agent: Pydantic AI sends each question’s instructions as a JSON object (see structured instructions). The keys field, question, and goal are names Pydantic AI chose. They aren’t part of the TypeSafe API. The field type sets the question type: The state holds the text the questions are about, plus any facts that help answer them. Pydantic AI builds it from the message you pass to the agent. Put any facts the questions need, such as a refund policy, in the message. Keep them out of the class docstring and out of the instructions argument to Agent, because both are copied into every question. State has more on keeping content apart from questions.
Here topic is the Choice question and fraud is the Noul question, whose value is Jev’s probability of yes. A bool field would also be a Noul question, but Pydantic AI would turn the probability into True or False at a threshold of 0.5. You can change that threshold with the decision_boolean_threshold model setting. A Score question needs an IntEnum that mixes in Pydantic AI’s UseEnumMemberDocstrings, with a docstring under each level. Version 2.71.0 reads those docstrings from the source file. A notebook has no source file, so in a notebook the question goes out without the level descriptions. Put that enum in a .py file and import it.

Run the agent

The typesafe: prefix on the model name gives the agent a TypeSafeModel, which sends the request to Jev. TypeSafeModel reads your key from TYPESAFE_API_KEY. With no key set, it gets the placeholder from Setup, which is enough to replay the saved answers. Each message goes to the agent as the user prompt. To run the agent on one message, call triage(). It returns a dictionary with the agent’s output and the response’s provider_details, which hold Jev’s confidence and probabilities. jev_request() builds the request Pydantic AI would send Jev, without calling the API. It runs the agent with CaptureRequest, a model class that records the request instead of sending it.
triage() returns plain JSON, so we validate its output into a Triage object to read each field as a Python value. In provider_details:
  • The confidence key holds a confidence score from 0 to 1 for each Choice question. A Noul question has no confidence score. Its probability of yes is the fraud value.
  • The probabilities key holds Jev’s probability for every option of each Choice question.
  • The scores key holds the answer to each Score question, so it is empty here.

Act on the probability in code

Jev’s answer for fraud is a probability, so you decide in code how high it has to be before a message goes to the fraud team.
The fraud value is 0.22. That leans no without ruling fraud out. The customer doesn’t recognize the payment, which could be fraud or a subscription they forgot about. A bool field would give you only True or False, decided by the same threshold for every bool field. With the probability, you set a threshold for this one decision, based on what a wrong answer costs. Missing fraud costs the bank more than checking a payment that turns out to be fine, so we send any message with a fraud value of 0.3 or more to the fraud team:
At 0.22, this message goes to the payments queue. A bank more worried about missing fraud could set the threshold to 0.15, which would send this message to the fraud team. Raise the threshold when a wrong yes costs more than a wrong no. Lower it when a wrong no costs more. You can also have a person check messages whose probability is close to the threshold. To choose a threshold, try a few on a set of your own messages, each labeled with the answer you expect.

Open the request in the playground

To see the request Pydantic AI sends Jev for the last message, call jev_request(). Then pass that request to make_playground_link() from cooksafe to get a TypeSafe playground link. In the playground, you can run the request or change its questions.
Open in the TypeSafe playground →

Next steps

  • Banking assistant: build a larger agent in which Jev picks one of 30 routes, asks the customer a follow-up question when it’s unsure, and passes money transfers to an LLM.
  • The Pydantic AI TypeSafe docs cover the provider, the model settings, and FallbackModel, which switches to another model on API errors by default.
  • Primitives and confidence explain Noul, Choice, and Score, and how confidence is calculated.