Learning resourcesReviewed · Pydantic

Pydantic Graph Decision Nodes

Decision Nodes is a guide for Python developers adding conditional paths to Pydantic graphs, but branch order determines which matching path runs. It explains GraphBuilder decisions using types, literal values and custom predicates, with examples of catch-all routes, nested decisions and branch labels.

Researched 15 Nov 2026

In short.

Decision Nodes is a guide for Python developers building branching workflows, but only the first matching branch runs. Its eight examples explain how GraphBuilder connects decisions to steps using type checks, literal values and custom predicates. The page is useful for understanding routing mechanics; installation, compatible versions, unmatched-input behavior, costs and licensing are not established. No examples were executed.

Researched and written by an AI model and checked by a second AI model. How it works

Covers the complete selected page at [https://pydantic.dev/docs/ai/graph/builder/decisions/index.md], including its documentation-index and query-parameter notices, explanations, all eight examples, execution notes and next-step references. Linked documentation, API references, the documentation index and PEP 747 were not included. Installation, compatibility, runtime behavior, performance and failure handling were not independently assessed; nothing was executed.

The overview

Identity

Decision Nodes is a documentation page about conditional branching in pydantic_graph. It teaches a feature through explanations and Python examples using GraphBuilder, StepContext and TypeExpression.

Original source: [https://pydantic.dev/docs/ai/graph/builder/decisions/index.md].

Assessment

The guide is useful when a graph step produces data that must determine the next step. Its strongest practical lesson is that overlapping conditions are resolved by branch order, making routing priorities part of the workflow's behavior.

The examples explain the API's intended use, but they do not establish runtime reliability, performance or compatibility with a particular installed release. None were executed.

Users

The guide suits Python developers already building graphs who need different handlers for different values or types. Beginners will benefit from familiarity with async functions, decorators, dataclasses and type annotations before adapting the examples.

Experienced developers can use it to understand TypeExpression, overlapping predicates, nested routing and labels for documentation or diagram generation.

Capabilities

The page documents matching by ordinary Python types, literal values and custom predicates supplied through matches. It also shows union-type matching, ordered branch priority, a catch-all branch, nested decisions and branch labels.

Examples pass incoming values through ctx.inputs and demonstrate shared state through ctx.state. The page describes object or Any as catch-all choices and illustrates object.

Using it

Requirements

The examples require a Python environment that provides the imported pydantic_graph APIs. They use asynchronous execution and standard-library dataclasses; literal-matching examples also import Literal from typing.

The page does not specify a package release, minimum Python version, dependency constraints or hardware requirements. Its examples contain no model or external API calls.

Mechanism

A step returns a value that flows into a decision created with g.decision(). Branches added with g.match(...).to(...) select the destination. The decision checks branches in their added order and takes the first match.

Simple types such as int and str can be passed directly to g.match(). The guide uses TypeExpression for complex annotations such as unions and Literal. A matches predicate adds value-based conditions, such as checking whether an integer is even.

Nested routing is illustrated by sending a matching value through an intermediate step and then into another decision.

Setup

The examples create a state dataclass, instantiate GraphBuilder with state_type and output_type, define async steps with @g.step, and add edges connecting the start, decisions, handlers and end. They then call g.build() and await graph.run(state=...).

Each example includes a note to import asyncio and add asyncio.run(main()) when running it as a script. Package installation and environment preparation are not covered.

Example

The branch-priority example returns 10 and defines two integer branches: Branch A accepts values greater than or equal to 5, while Branch B accepts values greater than or equal to 0. Both conditions accept 10, but the documentation shows Branch A because it appears first.

This illustrates how a broad condition placed earlier can prevent a later matching branch from being reached. The displayed output is supplied by the documentation and was not independently observed.

Assessment & evidence

Limitations

The page does not explain what happens when no branch matches, when a predicate raises an exception, or when a graph contains invalid routing. Its catch-all example demonstrates the syntax but does not document those failure cases.

The nested example sends values less than or equal to zero to a handler returning Negative. Adapting it unchanged would therefore label zero as negative; applications needing that distinction should handle zero separately or change the label.

The guide provides no benchmarks, production results or executable verification evidence.

Reasoning

The examples support using decisions when data needs to select among separate graph steps. Type matches distinguish input kinds, literal matches distinguish named outcomes, and predicates express conditions on values.

A practical inference from first-match behavior is to place specific conditions before broader ones and a catch-all last. This ordering follows the documented mechanism; it is not a measured reliability claim.

Alternatives

The page compares decision routing with pattern matching and if-elif-else chains. For a condition contained within one function, ordinary conditional code may be sufficient; that is a design inference rather than a demonstrated comparison.

No competing graph libraries are evaluated. The linked parallel-execution and join-node pages concern adjacent graph features, but their contents are outside this page.

Evidence

The evidence is the complete captured Decision Nodes page, identified as selected-page.txt, from [https://pydantic.dev/docs/ai/graph/builder/decisions/index.md]. It contains explanations and eight Python examples covering the documented routing patterns.

Its source revision is sha256:c26f7d5b263cdb4d85608303accf94e2712bff738022671dd8bf2f161c920af5. Example outputs are documentation assertions; no code was executed and no runtime results were independently established.

Access & freshness

Costs

The page provides no pricing or usage-fee information. Its examples route fixed Python values without showing model calls or paid services, but that does not establish the cost of a wider application. Package-related, infrastructure and operational costs are unknown.

Rights

The supplied page contains no licence notice or reuse terms for its prose and code examples. Permission to copy, modify or redistribute them is unknown from this evidence.

Freshness

The supplied snapshot was captured on 15 November 2026 at 12:10:22.012 UTC. That timestamp identifies the capture, not the page's publication or last-update date.

The page does not identify its applicable library release. It describes PEP 747's TypeForm as a possible future replacement for the TypeExpression workaround, but does not establish when that change might occur.

Actions

Before adapting the examples, establish the compatible pydantic_graph and Python versions and the installation steps for your environment. Choose branch order deliberately and determine the intended handling of unmatched inputs.

When validating your implementation, include overlapping predicates, unmatched values and boundary values such as zero. Check the applicable licence terms before reusing or redistributing the examples.

Evidence for this report

selected-page.txtselected · checked 2026-11-15