Registry & Models
The registry that maps block types to block classes, and the block data models. See Blocks & Registry for the conceptual guide.
hother.streamblocks.core.registry
Type-specific registry for StreamBlocks.
ContentValidatorFunc
MetadataValidatorFunc
MetadataValidationFailureMode
Bases: StrEnum
Behavior when metadata validation fails.
Source code in src/hother/streamblocks/core/registry.py
Registry
Type-specific registry for a single syntax type.
This registry holds exactly one syntax instance and maps block types to block classes.
Example
syntax = DelimiterPreambleSyntax(name="my_syntax") registry = Registry(syntax) registry.register("files_operations", FileOperations, validators=[my_validator]) registry.register("patch", Patch)
Or with bulk registration:
registry = Registry( ... syntax=syntax, ... blocks={ ... "files_operations": FileOperations, ... "patch": Patch, ... } ... ) registry.add_validator("files_operations", my_validator)
Source code in src/hother/streamblocks/core/registry.py
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 | |
metadata_failure_mode
property
metadata_failure_mode: MetadataValidationFailureMode
Get the metadata validation failure mode.
registered_blocks
property
Read-only view of registered block types mapped to block classes.
add_content_validator
add_content_validator(
block_type: BlockType, validator: ContentValidatorFunc
) -> None
Add an early content validator for a block type.
Content validators are called when the content section completes, before the final BlockEndEvent. They receive the raw content string and the parsed content dict.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
block_type
|
BlockType
|
Type of block to validate |
required |
validator
|
ContentValidatorFunc
|
Function that validates content and returns ValidationResult |
required |
Source code in src/hother/streamblocks/core/registry.py
add_metadata_validator
add_metadata_validator(
block_type: BlockType, validator: MetadataValidatorFunc
) -> None
Add an early metadata validator for a block type.
Metadata validators are called when the metadata section completes, before content accumulation begins. They receive the raw metadata string and the parsed metadata dict.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
block_type
|
BlockType
|
Type of block to validate |
required |
validator
|
MetadataValidatorFunc
|
Function that validates metadata and returns ValidationResult |
required |
Source code in src/hother/streamblocks/core/registry.py
add_validator
add_validator(
block_type: BlockType, validator: ValidatorFunc
) -> None
Add a validator for a block type.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
block_type
|
BlockType
|
Type of block to validate |
required |
validator
|
ValidatorFunc
|
Function that validates a block |
required |
Source code in src/hother/streamblocks/core/registry.py
get_block_class
register
register(
name: str,
block_class: type[Block[Any, Any]],
validators: list[ValidatorFunc] | None = None,
) -> None
Register a block class for a block type.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
name
|
str
|
Block type name (e.g., "files_operations", "patch") |
required |
block_class
|
type[Block[Any, Any]]
|
Block class inheriting from Block[M, C] |
required |
validators
|
list[ValidatorFunc] | None
|
Optional list of validator functions for this block type |
None
|
Source code in src/hother/streamblocks/core/registry.py
register_template
Register a custom prompt template for this registry.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
version
|
str
|
Version identifier used by |
required |
template
|
str | Path
|
Template string, or a Path to a template file. |
required |
mode
|
str
|
"registry", "single", or "both". |
'both'
|
Source code in src/hother/streamblocks/core/registry.py
serialize_block
to_prompt
Generate an LLM system prompt documenting all registered blocks.
The prompt describes this registry's syntax format and, for each registered block type, its description, metadata fields, content format, and (optionally) serialized examples.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
include_examples
|
bool
|
Whether to render each block's examples. |
True
|
template_version
|
str
|
Template version for A/B testing. |
'default'
|
Returns:
| Type | Description |
|---|---|
str
|
The rendered system prompt. |
Source code in src/hother/streamblocks/core/registry.py
validate_block
validate_block(block: ExtractedBlock[Any, Any]) -> bool
Run all validators for a block.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
block
|
ExtractedBlock[Any, Any]
|
Extracted block to validate |
required |
Returns:
| Type | Description |
|---|---|
bool
|
True if all validators pass |
Source code in src/hother/streamblocks/core/registry.py
validate_content
validate_content(
block_type: str,
raw_content: str,
parsed_content: dict[str, Any] | None,
) -> ValidationResult
Run all content validators for a block type.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
block_type
|
str
|
Type of block being validated |
required |
raw_content
|
str
|
Raw content string |
required |
parsed_content
|
dict[str, Any] | None
|
Parsed content dict (if available) |
required |
Returns:
| Type | Description |
|---|---|
ValidationResult
|
ValidationResult with combined results from all validators |
Source code in src/hother/streamblocks/core/registry.py
validate_metadata
validate_metadata(
block_type: str,
raw_metadata: str,
parsed_metadata: dict[str, Any] | None,
) -> ValidationResult
Run all metadata validators for a block type.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
block_type
|
str
|
Type of block being validated |
required |
raw_metadata
|
str
|
Raw metadata string |
required |
parsed_metadata
|
dict[str, Any] | None
|
Parsed metadata dict (if available) |
required |
Returns:
| Type | Description |
|---|---|
ValidationResult
|
ValidationResult with combined results from all validators |
Source code in src/hother/streamblocks/core/registry.py
ValidationResult
dataclass
Result from section validation.
Attributes:
| Name | Type | Description |
|---|---|---|
passed |
bool
|
Whether validation succeeded |
error |
str | None
|
Error message if validation failed |
Source code in src/hother/streamblocks/core/registry.py
failure
classmethod
failure(error: str) -> ValidationResult
success
classmethod
success() -> ValidationResult
hother.streamblocks.core.models
Core models for StreamBlocks.
Block
Bases: BaseModel
User-facing base class for defining block types.
This minimal class contains only the essential fields (metadata and content). Users inherit from this to define their block types.
Usage
class YesNo(Block[YesNoMetadata, YesNoContent]): pass
Access fields
block: Block[YesNoMetadata, YesNoContent] block.metadata.prompt # Type-safe access to metadata fields block.content.response # Type-safe access to content fields
Source code in src/hother/streamblocks/core/models.py
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 | |
content
class-attribute
instance-attribute
content: TContent = Field(
..., description="Parsed block content"
)
metadata
class-attribute
instance-attribute
metadata: TMetadata = Field(
..., description="Parsed block metadata"
)
add_example
classmethod
Add a single example for this block type.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
example
|
Self | dict[str, Any]
|
A block instance, or a dict with |
required |
Source code in src/hother/streamblocks/core/models.py
add_examples
classmethod
clear_examples
classmethod
Clear examples added via add_example().
Does not affect examples declared in the __examples__ attribute.
get_examples
classmethod
Return all examples (declared + dynamically added) as instances.
Declared examples come from __examples__ (inline dicts or a markdown
file). Dynamically added examples come from :meth:add_example.
Source code in src/hother/streamblocks/core/models.py
BlockCandidate
Tracks a potential block being accumulated.
Source code in src/hother/streamblocks/core/models.py
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 | |
add_line
add_line(line: str) -> None
cache_content_validation
Cache content validation result.
This method encapsulates validation result storage, providing a clear interface for the state machine to cache validation state.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
passed
|
bool
|
Whether content validation passed |
required |
error
|
str | None
|
Error message if validation failed, None otherwise |
required |
Source code in src/hother/streamblocks/core/models.py
cache_metadata_validation
Cache metadata validation result.
This method encapsulates validation result storage, providing a clear interface for the state machine to cache validation state.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
passed
|
bool
|
Whether metadata validation passed |
required |
error
|
str | None
|
Error message if validation failed, None otherwise |
required |
Source code in src/hother/streamblocks/core/models.py
transition_to_content
Transition from metadata/header to content section.
This method encapsulates the section state transition logic, making the state change explicit and centralized.
Source code in src/hother/streamblocks/core/models.py
transition_to_metadata
Transition from header to metadata section.
This method encapsulates the section state transition logic, making the state change explicit and centralized.
Source code in src/hother/streamblocks/core/models.py
ExtractedBlock
Bases: Block[TMetadata, TContent]
Full runtime representation of an extracted block.
This class extends the minimal Block with extraction metadata like line numbers, syntax name, and hash ID. The processor creates instances of this class when blocks are successfully extracted.
The metadata and content fields are typed generics, allowing type-safe access to block-specific fields.
Source code in src/hother/streamblocks/core/models.py
content
class-attribute
instance-attribute
content: TContent = Field(
..., description="Parsed block content"
)
hash_id
class-attribute
instance-attribute
line_end
class-attribute
instance-attribute
line_start
class-attribute
instance-attribute
metadata
class-attribute
instance-attribute
metadata: TMetadata = Field(
..., description="Parsed block metadata"
)
raw_text
class-attribute
instance-attribute
syntax_name
class-attribute
instance-attribute
add_example
classmethod
Add a single example for this block type.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
example
|
Self | dict[str, Any]
|
A block instance, or a dict with |
required |
Source code in src/hother/streamblocks/core/models.py
add_examples
classmethod
clear_examples
classmethod
Clear examples added via add_example().
Does not affect examples declared in the __examples__ attribute.
get_examples
classmethod
Return all examples (declared + dynamically added) as instances.
Declared examples come from __examples__ (inline dicts or a markdown
file). Dynamically added examples come from :meth:add_example.
Source code in src/hother/streamblocks/core/models.py
extract_block_types
extract_block_types(
block_class: type[Any],
) -> tuple[type[BaseMetadata], type[BaseContent]]
Extract metadata and content type parameters from a Block class.
For classes inheriting from Block[M, C], Pydantic resolves the concrete types and stores them in the field annotations during class definition. This function extracts those resolved types from model_fields.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
block_class
|
type[Any]
|
The block class to extract types from |
required |
Returns:
| Type | Description |
|---|---|
tuple[type[BaseMetadata], type[BaseContent]]
|
Tuple of (metadata_class, content_class) |