1212import json
1313import logging
1414import re
15- from typing import Any , Literal as TypingLiteral
15+ from typing import Annotated , Any , Literal as TypingLiteral , TypeAlias
1616
1717from pydantic import (
1818 BaseModel ,
4646 "FlowDefinition" ,
4747 "FlowDefinitionCondition" ,
4848 "FlowDefinitionDiagnostic" ,
49+ "FlowDictStateDefinition" ,
4950 "FlowEachActionDefinition" ,
5051 "FlowEachInnerActionDefinition" ,
5152 "FlowExpressionActionDefinition" ,
5253 "FlowHumanFeedbackDefinition" ,
54+ "FlowJsonSchemaStateDefinition" ,
5355 "FlowMethodDefinition" ,
5456 "FlowPersistenceDefinition" ,
57+ "FlowPydanticStateDefinition" ,
5558 "FlowStateDefinition" ,
5659 "FlowToolActionDefinition" ,
60+ "FlowUnknownStateDefinition" ,
5761]
5862
5963
@@ -74,13 +78,114 @@ class FlowDefinitionDiagnostic(BaseModel):
7478 path : str | None = None
7579
7680
77- class FlowStateDefinition (BaseModel ):
78- """Static description of a Flow state contract."""
81+ class FlowDictStateDefinition (BaseModel ):
82+ """Static description of a plain dictionary Flow state contract."""
7983
80- type : TypingLiteral ["dict" , "pydantic" , "json_schema" , "unknown" ] = "dict"
81- ref : str | None = None
82- json_schema : dict [str , Any ] | None = None
83- default : dict [str , Any ] | None = None
84+ model_config = ConfigDict (extra = "forbid" )
85+
86+ type : TypingLiteral ["dict" ] = Field (
87+ default = "dict" ,
88+ description = "Plain dictionary state with optional default values." ,
89+ examples = ["dict" ],
90+ )
91+ default : dict [str , Any ] | None = Field (
92+ default = None ,
93+ description = "Default state values applied before kickoff inputs." ,
94+ examples = [{"topic" : "AI agents" , "limit" : 3 }],
95+ )
96+
97+
98+ class FlowPydanticStateDefinition (BaseModel ):
99+ """Static description of an importable Pydantic Flow state contract."""
100+
101+ model_config = ConfigDict (extra = "forbid" )
102+
103+ type : TypingLiteral ["pydantic" ] = Field (
104+ default = "pydantic" ,
105+ description = "Importable Pydantic model used as the Flow state type." ,
106+ examples = ["pydantic" ],
107+ )
108+ ref : str | None = Field (
109+ default = None ,
110+ description = "Import reference for the state model, formatted as module:qualname." ,
111+ examples = ["my_project.flows:ResearchState" ],
112+ )
113+ json_schema : dict [str , Any ] | None = Field (
114+ default = None ,
115+ description = (
116+ "Fallback JSON Schema used when the Pydantic state ref is unavailable."
117+ ),
118+ examples = [
119+ {
120+ "type" : "object" ,
121+ "properties" : {"topic" : {"type" : "string" }},
122+ "required" : ["topic" ],
123+ }
124+ ],
125+ )
126+ default : dict [str , Any ] | None = Field (
127+ default = None ,
128+ description = "Default state values applied before kickoff inputs." ,
129+ examples = [{"topic" : "AI agents" , "limit" : 3 }],
130+ )
131+
132+
133+ class FlowJsonSchemaStateDefinition (BaseModel ):
134+ """Static description of an inline JSON Schema Flow state contract."""
135+
136+ model_config = ConfigDict (extra = "forbid" )
137+
138+ type : TypingLiteral ["json_schema" ] = Field (
139+ default = "json_schema" ,
140+ description = "Inline JSON Schema used as the Flow state contract." ,
141+ examples = ["json_schema" ],
142+ )
143+ json_schema : dict [str , Any ] = Field (
144+ description = "JSON Schema used to validate and document flow state." ,
145+ examples = [
146+ {
147+ "type" : "object" ,
148+ "properties" : {"topic" : {"type" : "string" }},
149+ "required" : ["topic" ],
150+ }
151+ ],
152+ )
153+ default : dict [str , Any ] | None = Field (
154+ default = None ,
155+ description = "Default state values applied before kickoff inputs." ,
156+ examples = [{"topic" : "AI agents" , "limit" : 3 }],
157+ )
158+
159+
160+ class FlowUnknownStateDefinition (BaseModel ):
161+ """Static description of a state contract that could not be serialized."""
162+
163+ model_config = ConfigDict (extra = "forbid" )
164+
165+ type : TypingLiteral ["unknown" ] = Field (
166+ default = "unknown" ,
167+ description = "Unknown state representation; runtime falls back to dictionary state." ,
168+ examples = ["unknown" ],
169+ )
170+ ref : str | None = Field (
171+ default = None ,
172+ description = "Best-effort import reference for the unknown state type." ,
173+ examples = ["my_project.flows:CustomState" ],
174+ )
175+ default : dict [str , Any ] | None = Field (
176+ default = None ,
177+ description = "Default state values applied before kickoff inputs." ,
178+ examples = [{"topic" : "AI agents" , "limit" : 3 }],
179+ )
180+
181+
182+ FlowStateDefinition : TypeAlias = Annotated [
183+ FlowDictStateDefinition
184+ | FlowPydanticStateDefinition
185+ | FlowJsonSchemaStateDefinition
186+ | FlowUnknownStateDefinition ,
187+ Field (discriminator = "type" ),
188+ ]
84189
85190
86191class FlowConfigDefinition (BaseModel ):
0 commit comments