schema
Class info¶
Classes¶
| Name | Children | Inherits |
|---|---|---|
| ConfigSchema schemez.schema Schema for configuration objects that may declare required packages. |
||
| Schema schemez.schema Base class for configuration models. |
🛈 DocStrings¶
Configuration models for Schemez.
ConfigSchema
¶
Inner
¶
Schema
¶
Bases: BaseModel
Base class for configuration models.
Provides:
- Common Pydantic settings (extra="forbid", attribute docstrings as descriptions)
- YAML loading and dumping (optionally with field descriptions as comments)
- Markdown documentation, Python code and test data generation
- Model creation from functions, class constructors and JSON Schema
- Merging and sub-model iteration
Dumps (model_dump(), model_dump_json(), YAML) put the fields of the most derived
class first, then those of its bases. Pydantic itself orders base fields first, so a
wrap serializer reorders the keys of every dump, nested Schema instances included.
That makes dumps about 3x slower than a plain BaseModel (around 1.8 µs instead of
0.6 µs for a three-field model). This is deliberate: the order is what users read in
dumped configs; the key order per class is cached, the remaining cost is the Python-level
serializer call.
Source code in src/schemez/schema.py
32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 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 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 | |
dump_markdown_schema
classmethod
¶
dump_markdown_schema(
*,
template: str | None = None,
header_level: int = 1,
include_defaults: bool = True,
include_examples: bool = True,
include_constraints: bool = True,
display_mode: Literal["table", "python_code", "yaml"] = "table",
mode: Literal["minimal", "maximal", "default"] = "default",
exclude_none: bool = True,
exclude_defaults: bool = False,
exclude_unset: bool = False,
indent: int = 2,
header_style: Literal["default", "pymdownx"] = "default",
serialization_mode: Literal["json", "python"] = "json"
) -> str
Dump model class schema to Markdown documentation.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
template
|
str | None
|
Custom Jinja2 template string (uses default if None) |
None
|
header_level
|
int
|
Starting header level (1 = h1, 2 = h2, etc.) |
1
|
include_defaults
|
bool
|
Include default values in the table |
True
|
include_examples
|
bool
|
Include examples section |
True
|
include_constraints
|
bool
|
Include constraints section |
True
|
display_mode
|
Literal['table', 'python_code', 'yaml']
|
Output format - "table", "python_code", or "yaml" |
'table'
|
mode
|
Literal['minimal', 'maximal', 'default']
|
Generation mode for YAML examples |
'default'
|
exclude_none
|
bool
|
Exclude None values from YAML |
True
|
exclude_defaults
|
bool
|
Exclude default values from YAML |
False
|
exclude_unset
|
bool
|
Exclude unset values from YAML |
False
|
indent
|
int
|
YAML indentation |
2
|
header_style
|
Literal['default', 'pymdownx']
|
Code block header style - "default" or "pymdownx" |
'default'
|
serialization_mode
|
Literal['json', 'python']
|
Pydantic serialization mode - "json" (default) or "python" |
'json'
|
Returns:
| Type | Description |
|---|---|
str
|
Markdown string documenting the model schema |
Source code in src/schemez/schema.py
for_class_ctor
classmethod
¶
for_class_ctor(target_cls: type) -> type[Schema]
Create a schema model from a class constructor.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
target_cls
|
type
|
The class whose constructor to convert |
required |
Returns:
| Type | Description |
|---|---|
type[Schema]
|
A new schema model class based on the constructor parameters |
Source code in src/schemez/schema.py
for_function
classmethod
¶
for_function(func: Callable[..., Any], *, name: str | None = None) -> type[Schema]
Create a schema model from a function's signature.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
func
|
Callable[..., Any]
|
The function to create a schema from |
required |
name
|
str | None
|
Optional name for the model |
None
|
Returns:
| Type | Description |
|---|---|
type[Schema]
|
A new schema model class based on the function parameters |
Source code in src/schemez/schema.py
from_json_schema
classmethod
¶
from_json_schema(json_schema: dict[str, Any]) -> type[Schema]
Create a schema model from a JSON schema.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
json_schema
|
dict[str, Any]
|
The JSON schema to create a schema from |
required |
Returns:
| Type | Description |
|---|---|
type[Schema]
|
A new schema model class based on the JSON schema |
Source code in src/schemez/schema.py
from_llm
async
classmethod
¶
from_llm(
*prompts: JoinablePathLike | str | UserContent,
model: str = "google-gla:gemini-2.0-flash",
system_prompt: str = DEFAULT_SYSTEM_PROMPT,
user_prompt: str = DEFAULT_USER_PROMPT
) -> Self
Create a schema model from a text snippet using AI.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
prompts
|
JoinablePathLike | str | UserContent
|
The text to create a schema from |
()
|
model
|
str
|
The AI model to use for schema extraction |
'google-gla:gemini-2.0-flash'
|
system_prompt
|
str
|
The system prompt to use for schema extraction |
DEFAULT_SYSTEM_PROMPT
|
user_prompt
|
str
|
The user prompt to use for schema extraction |
DEFAULT_USER_PROMPT
|
Returns:
| Type | Description |
|---|---|
Self
|
A new schema model class based on the document |
Source code in src/schemez/schema.py
from_yaml
classmethod
¶
Create from YAML string.
Source code in src/schemez/schema.py
generate_test_data
classmethod
¶
generate_test_data(
*,
seed: int = 0,
mode: Literal["minimal", "maximal", "default"] = "default",
validate: bool = True
) -> Self
Generate test data that conforms to this schema.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
seed
|
int
|
Seed for deterministic generation (default: 0) |
0
|
mode
|
Literal['minimal', 'maximal', 'default']
|
Generation mode: - "minimal": Only required fields, minimum values - "maximal": All fields, maximum reasonable values - "default": Balanced generation (default) |
'default'
|
validate
|
bool
|
Whether to validate the generated data (default: True) |
True
|
Returns:
| Type | Description |
|---|---|
Self
|
An instance of this schema populated with generated test data |
Example
class PersonSchema(Schema):
name: str
age: int = 25
email: str | None = None
# Generate test data
person = PersonSchema.generate_test_data(seed=42)
# Result: PersonSchema(name="abc", age=42, email=None)
# Generate minimal data (required fields only)
minimal = PersonSchema.generate_test_data(mode="minimal")
# Result: PersonSchema(name="a", age=0, email=None)
# Generate maximal data (all fields populated)
maximal = PersonSchema.generate_test_data(mode="maximal")
# Result: PersonSchema(name="abcdefghij", age=1000, email="user0@example.com")
Source code in src/schemez/schema.py
iter_submodel_types
classmethod
¶
iter_submodel_types(
*, recursive: bool = True, include_union_members: bool = True
) -> Iterator[tuple[str, type[BaseModel]]]
Iterate through all nested BaseModel types in field annotations.
Supports field types: - BaseModel (direct type) - list[BaseModel] - dict[str, BaseModel] - BaseModel | OtherModel (unions, when include_union_members=True)
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
recursive
|
bool
|
If True, also iterate through submodel types of submodels |
True
|
include_union_members
|
bool
|
If True, yield each union member separately |
True
|
Yields:
| Type | Description |
|---|---|
tuple[str, type[BaseModel]]
|
Tuples of (path, model_type) where path is like "field", "field[]", "field{}" |
Source code in src/schemez/schema.py
iter_submodels
¶
Iterate through all nested BaseModel instances in fields.
Supports fields of type: - BaseModel (direct instance) - list[BaseModel] - dict[str, BaseModel]
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
recursive
|
bool
|
If True, also iterate through submodels of submodels |
True
|
Yields:
| Type | Description |
|---|---|
tuple[str, BaseModel]
|
Tuples of (path, submodel) where path is like "field", "field[0]", "field['key']" |
Source code in src/schemez/schema.py
merge
¶
model_dump_markdown
¶
model_dump_markdown(
*,
template: str | None = None,
header_level: int = 1,
include_defaults: bool = True,
include_examples: bool = True,
include_constraints: bool = True,
include_values: bool = True,
display_mode: Literal["table", "python_code", "yaml"] = "table",
exclude_none: bool = True,
exclude_defaults_yaml: bool = False,
exclude_unset: bool = False,
indent: int = 2,
header_style: Literal["default", "pymdownx"] = "default",
serialization_mode: Literal["json", "python"] = "json"
) -> str
Dump model instance to Markdown documentation.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
template
|
str | None
|
Custom Jinja2 template string (uses default if None) |
None
|
header_level
|
int
|
Starting header level (1 = h1, 2 = h2, etc.) |
1
|
include_defaults
|
bool
|
Include default values in the table |
True
|
include_examples
|
bool
|
Include examples section |
True
|
include_constraints
|
bool
|
Include constraints section |
True
|
include_values
|
bool
|
Include current instance values |
True
|
display_mode
|
Literal['table', 'python_code', 'yaml']
|
Output format - "table", "python_code", or "yaml" |
'table'
|
exclude_none
|
bool
|
Exclude None values from YAML |
True
|
exclude_defaults_yaml
|
bool
|
Exclude default values from YAML |
False
|
exclude_unset
|
bool
|
Exclude unset values from YAML |
False
|
indent
|
int
|
YAML indentation |
2
|
header_style
|
Literal['default', 'pymdownx']
|
Code block header style - "default" or "pymdownx" |
'default'
|
serialization_mode
|
Literal['json', 'python']
|
Pydantic serialization mode - "json" (default) or "python" |
'json'
|
Returns:
| Type | Description |
|---|---|
str
|
Markdown string documenting the model |
Source code in src/schemez/schema.py
model_dump_yaml
¶
model_dump_yaml(
exclude_none: bool = True,
exclude_defaults: bool = False,
exclude_unset: bool = False,
indent: int = 2,
default_flow_style: bool | None = None,
allow_unicode: bool = True,
comments: bool = False,
sort_keys: bool = True,
mode: Literal["json", "python"] = "python",
) -> str
Dump configuration to YAML string.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
exclude_none
|
bool
|
Exclude fields with None values |
True
|
exclude_defaults
|
bool
|
Exclude fields with default values |
False
|
exclude_unset
|
bool
|
Exclude fields that are not set |
False
|
indent
|
int
|
Indentation level for YAML output |
2
|
default_flow_style
|
bool | None
|
Default flow style for YAML output |
None
|
allow_unicode
|
bool
|
Allow unicode characters in YAML output |
True
|
comments
|
bool
|
Include descriptions as comments in the YAML output |
False
|
sort_keys
|
bool
|
Sort keys in the YAML output |
True
|
mode
|
Literal['json', 'python']
|
Output mode, either "json" or "python" |
'python'
|
Returns:
| Type | Description |
|---|---|
str
|
YAML string representation of the model |
Source code in src/schemez/schema.py
save
¶
Save configuration to a YAML file.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
path
|
JoinablePathLike
|
Path to save the configuration to |
required |
overwrite
|
bool
|
Whether to overwrite an existing file |
False
|
Raises:
| Type | Description |
|---|---|
OSError
|
If file cannot be written |
ValueError
|
If path is invalid |
Source code in src/schemez/schema.py
to_python_code
classmethod
¶
to_python_code(
*,
class_name: str | None = None,
target_python_version: PythonVersionStr | None = None,
model_type: str = "pydantic.BaseModel"
) -> str
Convert this model to Python code asynchronously.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
class_name
|
str | None
|
Optional custom class name for the generated code |
None
|
target_python_version
|
PythonVersionStr | None
|
Target Python version for code generation |
None
|
model_type
|
str
|
Type of model to generate |
'pydantic.BaseModel'
|
Returns:
| Type | Description |
|---|---|
str
|
Generated Python code as string |