PipelineSpec¶
Build middleware pipelines in Python with export to JSON/YAML.
PipelineSpec provides a fluent API for constructing middleware pipelines that
can be compiled into middleware instances or exported as configuration files.
This is useful for configuration-driven deployments where pipeline definitions
need to be portable.
Quick Example¶
from pydantic_ai_middleware import PipelineSpec
from pydantic_ai_middleware.builder import MiddlewarePipelineCompiler
# Build a pipeline spec with fluent API
spec = (
PipelineSpec()
.add_type("logging", {"level": "DEBUG"})
.add_type("rate_limit", {"max_requests": 100})
.add_when(
predicate="is_admin",
then=[{"type": "admin_audit"}],
else_=[{"type": "user_audit"}],
)
)
# Export to YAML file
spec.save("middleware-pipeline.yaml")
# Or compile directly to middleware instances
compiler = MiddlewarePipelineCompiler(registry)
middleware_list = spec.compile(compiler)
Supported Node Types¶
- type: A single middleware by registered name
- chain: Sequential execution of multiple nodes
- parallel: Concurrent execution with result aggregation
- when: Conditional branching based on predicates
API Reference¶
pydantic_ai_middleware.pipeline_spec.PipelineSpec
dataclass
¶
A mutable pipeline spec builder for defining middleware pipelines in Python.
PipelineSpec provides a fluent API for building middleware pipelines that can be:
- Compiled into middleware instances using a :class:MiddlewarePipelineCompiler
- Exported as portable JSON/YAML config files for configuration-driven pipelines
The builder uses the same skeleton nodes as config loading: type, chain,
parallel, and when.
Example
from pydantic_ai_middleware import PipelineSpec
from pydantic_ai_middleware.builder import MiddlewarePipelineCompiler
# Build a pipeline spec
spec = (
PipelineSpec()
.add_type("logging", {"level": "DEBUG"})
.add_type("rate_limit", {"max_requests": 100})
.add_when(
predicate="is_admin",
then=[{"type": "admin_audit"}],
else_=[{"type": "user_audit"}],
)
)
# Export to YAML
spec.save("pipeline.yaml")
# Or compile to middleware
compiler = MiddlewarePipelineCompiler(registry)
middleware = spec.compile(compiler)
Source code in src/pydantic_ai_middleware/pipeline_spec.py
| Python | |
|---|---|
69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 | |
nodes = field(default_factory=list)
class-attribute
instance-attribute
¶
The list of pipeline nodes.
add(node)
¶
Add a raw node to the pipeline.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
node
|
Mapping[str, Any]
|
A node dictionary (e.g., {"type": "logging"}). |
required |
Returns:
| Type | Description |
|---|---|
Self
|
Self for method chaining. |
Source code in src/pydantic_ai_middleware/pipeline_spec.py
add_type(type_name, config=None)
¶
Add a middleware type node to the pipeline.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
type_name
|
str
|
The registered middleware type name. |
required |
config
|
Mapping[str, Any] | None
|
Optional configuration to pass to the middleware factory. |
None
|
Returns:
| Type | Description |
|---|---|
Self
|
Self for method chaining. |
Source code in src/pydantic_ai_middleware/pipeline_spec.py
add_chain(nodes)
¶
Add a chain node (sequential middleware execution).
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
nodes
|
Sequence[Mapping[str, Any]]
|
Sequence of nodes to execute in order. |
required |
Returns:
| Type | Description |
|---|---|
Self
|
Self for method chaining. |
Source code in src/pydantic_ai_middleware/pipeline_spec.py
add_parallel(nodes, *, strategy=None, timeout=None, name=None)
¶
Add a parallel node (concurrent middleware execution).
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
nodes
|
Sequence[Mapping[str, Any]]
|
Sequence of nodes to execute in parallel. |
required |
strategy
|
str | AggregationStrategy | None
|
Aggregation strategy for combining results. Options: "first", "last", "merge", "all". |
None
|
timeout
|
float | None
|
Optional timeout in seconds for parallel execution. |
None
|
name
|
str | None
|
Optional name for the parallel group. |
None
|
Returns:
| Type | Description |
|---|---|
Self
|
Self for method chaining. |
Example
Source code in src/pydantic_ai_middleware/pipeline_spec.py
add_when(*, predicate, then, else_=None)
¶
Add a conditional node (branching based on predicate).
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
predicate
|
str | Mapping[str, Any] | bool
|
Condition for branching. Can be: - A string (registered predicate name) - A dict (predicate with config) - A boolean (static condition) |
required |
then
|
Sequence[Mapping[str, Any]]
|
Nodes to execute when predicate is True. |
required |
else_
|
Sequence[Mapping[str, Any]] | None
|
Optional nodes to execute when predicate is False. |
None
|
Returns:
| Type | Description |
|---|---|
Self
|
Self for method chaining. |
Example
Source code in src/pydantic_ai_middleware/pipeline_spec.py
to_config()
¶
Convert the spec to a config-compatible list of nodes.
Returns:
| Type | Description |
|---|---|
list[dict[str, Any]]
|
A list of node dictionaries suitable for JSON/YAML serialization. |
Source code in src/pydantic_ai_middleware/pipeline_spec.py
dump(*, format='json')
¶
Serialize the pipeline to a string.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
format
|
str
|
Output format, either "json" or "yaml". |
'json'
|
Returns:
| Type | Description |
|---|---|
str
|
The serialized pipeline as a string. |
Raises:
| Type | Description |
|---|---|
MiddlewareConfigError
|
If format is unknown or YAML requested but PyYAML is not installed. |
Source code in src/pydantic_ai_middleware/pipeline_spec.py
save(path, *, format=None)
¶
Save the pipeline to a file.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
path
|
str | Path
|
File path to save to. Format is auto-detected from extension (.json, .yaml, .yml) if not specified. |
required |
format
|
str | None
|
Optional explicit format ("json" or "yaml"). |
None
|
Raises:
| Type | Description |
|---|---|
MiddlewareConfigError
|
If format cannot be determined or is unknown. |
Source code in src/pydantic_ai_middleware/pipeline_spec.py
compile(compiler)
¶
Compile the spec into middleware instances.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
compiler
|
MiddlewarePipelineCompiler[Any]
|
A MiddlewarePipelineCompiler with registered factories. |
required |
Returns:
| Type | Description |
|---|---|
list[AgentMiddleware[Any]]
|
A list of middleware instances ready to use with an agent. |
Raises:
| Type | Description |
|---|---|
MiddlewareConfigError
|
If compilation fails (e.g., unknown types). |