guidellm.schemas.base
Pydantic utilities for polymorphic model serialization and registry integration.
Provides integration between Pydantic and the registry system, enabling polymorphic serialization and deserialization of Pydantic models using a discriminator field and dynamic class registry. Includes base model classes with standardized configurations and generic status breakdown models for structured result organization.
PydanticClassRegistryMixin
Bases: ReloadableBaseModel, RegistryMixin[type[BaseModelT]], ABC, Generic[BaseModelT]
Polymorphic Pydantic model mixin enabling registry-based dynamic instantiation.
Integrates Pydantic validation with the registry system to enable polymorphic serialization and deserialization based on a discriminator field. Automatically instantiates the correct subclass during validation based on registry mappings, providing a foundation for extensible plugin-style architectures.
Example: :: from speculators.utils import PydanticClassRegistryMixin
class BaseConfig(PydanticClassRegistryMixin["BaseConfig"]):
schema_discriminator: ClassVar[str] = "config_type"
config_type: str = Field(description="Configuration type identifier")
@classmethod
def __pydantic_schema_base_type__(cls) -> type["BaseConfig"]:
return BaseConfig
@BaseConfig.register("database")
class DatabaseConfig(BaseConfig):
config_type: str = "database"
connection_string: str = Field(description="Database connection string")
# Dynamic instantiation based on discriminator
config = BaseConfig.model_validate({
"config_type": "database",
"connection_string": "postgresql://localhost:5432/db"
})
Attributes:
| Name | Type | Description |
|---|---|---|
schema_discriminator | str | Field name used for polymorphic type discrimination |
Source code in src/guidellm/schemas/base.py
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 | |
__get_pydantic_core_schema__(source_type, handler) classmethod
Generate polymorphic validation schema for dynamic type instantiation.
Creates a tagged union schema that enables Pydantic to automatically instantiate the correct subclass based on the discriminator field value. Falls back to base schema generation when no registry is available.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
source_type | Any | Type being processed for schema generation | required |
handler | GetCoreSchemaHandler | Pydantic core schema generation handler | required |
Returns:
| Type | Description |
|---|---|
CoreSchema | Tagged union schema for polymorphic validation or base schema |
Source code in src/guidellm/schemas/base.py
__new__(*args, **kwargs)
Prevent direct instantiation of base classes that use this mixin.
Only allows instantiation of concrete subclasses, not the base class.
Source code in src/guidellm/schemas/base.py
__pydantic_generate_base_schema__(handler) classmethod
Generate fallback schema for polymorphic models without registry.
Provides a base schema that accepts any valid input when no registry is available for polymorphic validation. Used as fallback during schema generation when the registry has not been populated.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
handler | GetCoreSchemaHandler | Pydantic core schema generation handler | required |
Returns:
| Type | Description |
|---|---|
CoreSchema | Base CoreSchema that accepts any valid input |
Source code in src/guidellm/schemas/base.py
__pydantic_schema_base_type__() abstractmethod classmethod
Define the base type for polymorphic validation hierarchy.
Must be implemented by subclasses to specify which type serves as the root of the polymorphic hierarchy for schema generation and validation.
Returns:
| Type | Description |
|---|---|
type[BaseModelT] | Base class type for the polymorphic model hierarchy |
Source code in src/guidellm/schemas/base.py
auto_populate_registry() classmethod
Initialize registry with auto-discovery and reload validation schema.
Triggers automatic population of the class registry through the parent RegistryMixin functionality and ensures the Pydantic validation schema is updated to include all discovered types for polymorphic validation.
Returns:
| Type | Description |
|---|---|
bool | True if registry was populated, False if already populated |
Raises:
| Type | Description |
|---|---|
ValueError | If called when registry_auto_discovery is disabled |
Source code in src/guidellm/schemas/base.py
register_decorator(clazz, name=None) classmethod
Register a Pydantic model class with type validation and schema reload.
Validates that the class is a proper Pydantic BaseModel subclass before registering it in the class registry. Automatically triggers schema reload to incorporate the new type into polymorphic validation.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
clazz | RegisterClassT | Pydantic model class to register in the polymorphic hierarchy | required |
name | str | list[str] | None | Registry identifier for the class. Uses class name if None | None |
Returns:
| Type | Description |
|---|---|
RegisterClassT | The registered class unchanged for decorator chaining |
Raises:
| Type | Description |
|---|---|
TypeError | If clazz is not a Pydantic BaseModel subclass |
Source code in src/guidellm/schemas/base.py
registered_classes() classmethod
Get all registered pydantic classes from the registry.
Automatically triggers auto-discovery if registry_auto_discovery is enabled to ensure all available implementations are included.
Returns:
| Type | Description |
|---|---|
tuple[type[BaseModelT], ...] | Tuple of all registered classes including auto-discovered ones |
Raises:
| Type | Description |
|---|---|
ValueError | If called before any objects have been registered |
Source code in src/guidellm/schemas/base.py
ReloadableBaseModel
Bases: BaseModel
Base Pydantic model with schema reloading capabilities.
Provides dynamic schema rebuilding functionality for models that need to update their validation schemas at runtime, particularly useful when working with registry-based polymorphic models where new types are registered after initial class definition.
Source code in src/guidellm/schemas/base.py
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 | |
reload_parent_schemas() classmethod
Recursively reload schemas for all Pydantic models that reference this class in their field annotations.
Walks the subclass trees of guidellm's own model roots (ReloadableBaseModel and StandardBaseModel) rather than all BaseModel subclasses, so third-party Pydantic models loaded in the same process are not visited.
Source code in src/guidellm/schemas/base.py
reload_schema(parents=False) classmethod
Reload the class schema with updated registry information.
Forces a complete rebuild of the Pydantic model schema to incorporate any changes made to associated registries or validation rules.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
parents | bool | Whether to also rebuild schemas for any pydantic parent types that reference this model. | False |
Source code in src/guidellm/schemas/base.py
StandardBaseDict
Bases: StandardBaseModel
Base Pydantic model allowing arbitrary additional fields.
Extends StandardBaseModel to accept extra fields beyond those explicitly defined in the model schema. Useful for flexible data structures that need to accommodate varying or unknown field sets while maintaining type safety for known fields.
Source code in src/guidellm/schemas/base.py
StandardBaseModel
Bases: BaseModel
Base Pydantic model with standardized configuration for GuideLLM.
Provides consistent validation behavior and configuration settings across all Pydantic models in the application, including field validation, attribute conversion, and default value handling.
Example: :: class MyModel(StandardBaseModel): name: str value: int = 42
# Access default values
default_value = MyModel.get_default("value") # Returns 42
Source code in src/guidellm/schemas/base.py
get_default(field) classmethod
Get default value for a model field.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
field | str | Name of the field to get the default value for | required |
Returns:
| Type | Description |
|---|---|
Any | Default value of the specified field |
Raises:
| Type | Description |
|---|---|
KeyError | If the field does not exist in the model |
Source code in src/guidellm/schemas/base.py
StatusBreakdown
Bases: BaseModel, Generic[SuccessfulT, ErroredT, IncompleteT, TotalT]
Generic model for organizing results by processing status.
Provides structured categorization of results into successful, errored, incomplete, and total status groups. Supports flexible typing for each status category to accommodate different result types while maintaining consistent organization patterns across the application.
Example: :: from guidellm.utils import StatusBreakdown
# Define a breakdown for request counts
breakdown = StatusBreakdown[int, int, int, int](
successful=150,
errored=5,
incomplete=10,
total=165
)
Source code in src/guidellm/schemas/base.py
standard_model_config(**updates)
Generate a standard Pydantic model configuration with optional updates.
Provides a consistent base configuration for Pydantic models in the application, allowing for easy customization through additional keyword arguments.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
updates | Any | Additional configuration settings to override defaults | {} |
Returns:
| Type | Description |
|---|---|
ConfigDict | ConfigDict with standard settings merged with any updates |