Processors
The two stream processors and their configuration. See the architecture overview for how they fit in the pipeline.
hother.streamblocks.core.processor
Stream processing engine for StreamBlocks.
ProcessorConfig
dataclass
Configuration for StreamBlockProcessor.
Attributes:
| Name | Type | Description |
|---|---|---|
lines_buffer |
int
|
Number of recent lines to keep in buffer for context (default: 5). Used for debugging and error messages. |
max_line_length |
int
|
Maximum line length in bytes before truncation (default: 16,384). Lines exceeding this limit are truncated to prevent memory issues. |
max_block_size |
int
|
Maximum block size in bytes before rejection (default: 1,048,576 = 1MB). Blocks exceeding this limit are rejected with SIZE_EXCEEDED error. |
emit_original_events |
bool
|
Whether to pass through original provider events (default: True). When False, only StreamBlocks events are emitted. Set to False when using IdentityInputAdapter to avoid duplicate events. |
emit_text_deltas |
bool
|
Whether to emit TextDeltaEvent for real-time streaming (default: True). Enables character-level streaming for live UIs. Disable to reduce event volume. |
emit_section_end_events |
bool
|
Whether to emit section end events (default: True). Controls BlockMetadataEndEvent and BlockContentEndEvent emission for early validation. |
auto_detect_adapter |
bool
|
Whether to auto-detect input adapter from first chunk (default: True). When False, uses IdentityInputAdapter. Disable for performance with known adapter. |
Example
Custom configuration for large blocks
config = ProcessorConfig( ... max_block_size=2_097_152, # 2MB ... emit_original_events=False, ... emit_text_deltas=False, ... ) processor = StreamBlockProcessor(registry, config=config)
Minimal configuration for performance
config = ProcessorConfig( ... emit_section_end_events=False, ... auto_detect_adapter=False, ... )
Source code in src/hother/streamblocks/core/processor.py
StreamBlockProcessor
Main stream processing engine for a single syntax type.
This processor works with exactly one syntax and coordinates: - Adapter detection and text extraction - Line accumulation via LineAccumulator - Block detection and extraction via BlockStateMachine - Event emission (TextDeltaEvent, block events, etc.)
Source code in src/hother/streamblocks/core/processor.py
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 | |
finalize
Finalize processing and flush any incomplete blocks.
Call this method after processing all chunks to get rejection events for any blocks that were opened but never closed.
This method processes any accumulated text as a final line before flushing candidates, ensuring the last line is processed even if it doesn't end with a newline.
Returns:
| Type | Description |
|---|---|
list[Event]
|
List of events including processed final line and rejection events |
list[Event]
|
for incomplete blocks |
Example
processor = StreamBlockProcessor(registry) async for chunk in stream: ... events = processor.process_chunk(chunk) ... # ... handle events ...
Stream ended, process remaining text and flush incomplete blocks
final_events = processor.finalize() for event in final_events: ... if isinstance(event, BlockErrorEvent): ... print(f"Incomplete block: {event.reason}")
Source code in src/hother/streamblocks/core/processor.py
is_native_event
Check if event is a native provider event (not a StreamBlocks event).
This method provides provider-agnostic detection of native events. It checks if the event originates from the AI provider (Gemini, OpenAI, Anthropic, etc.) versus being a StreamBlocks event.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
event
|
Any
|
Event to check |
required |
Returns:
| Type | Description |
|---|---|
bool
|
True if event is from the native provider, False if it's a StreamBlocks |
bool
|
event or if detection is not possible |
Example
processor = StreamBlockProcessor(registry) async for event in processor.process_stream(gemini_stream): ... if processor.is_native_event(event): ... # Handle Gemini event (provider-agnostic!) ... usage = getattr(event, 'usage_metadata', None) ... elif isinstance(event, BlockEndEvent): ... # Handle StreamBlocks event ... print(f"Block: {event.block_id}")
Source code in src/hother/streamblocks/core/processor.py
process_chunk
process_chunk(
chunk: TChunk,
adapter: InputProtocolAdapter[TChunk] | None = None,
) -> list[TChunk | Event]
Process a single chunk and return resulting events.
This method is stateful - it maintains internal state between calls. Call finalize() after processing all chunks to flush incomplete blocks.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
chunk
|
TChunk
|
Single chunk to process |
required |
adapter
|
InputProtocolAdapter[TChunk] | None
|
Optional adapter for extracting text. If not provided and auto_detect_adapter=True, will auto-detect on first chunk. |
None
|
Returns:
| Type | Description |
|---|---|
list[TChunk | Event]
|
List of events generated from this chunk. May be empty if chunk only |
list[TChunk | Event]
|
accumulates text without completing any lines. |
Raises:
| Type | Description |
|---|---|
RuntimeError
|
If adapter is not set after first chunk processing (internal state error, should not occur in normal usage). |
Example
processor = StreamBlockProcessor(registry) response = await client.generate_content_stream(...) async for chunk in response: ... events = processor.process_chunk(chunk) ... for event in events: ... if isinstance(event, BlockEndEvent): ... print(f"Block: {event.block_id}") ...
Finalize at stream end
final_events = processor.finalize() for event in final_events: ... if isinstance(event, BlockErrorEvent): ... print(f"Incomplete block: {event.reason}")
Source code in src/hother/streamblocks/core/processor.py
process_stream
async
process_stream(
stream: AsyncIterator[TChunk],
adapter: InputProtocolAdapter[TChunk] | None = None,
) -> AsyncGenerator[TChunk | Event]
Process stream and yield mixed events.
This method processes chunks from any stream format, extracting text via an adapter and emitting both original chunks (if enabled) and StreamBlocks events.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
stream
|
AsyncIterator[TChunk]
|
Async iterator yielding chunks (text or objects) |
required |
adapter
|
InputProtocolAdapter[TChunk] | None
|
Optional adapter for extracting text from chunks. If None and auto_detect_adapter=True, will auto-detect from first chunk. |
None
|
Yields:
| Type | Description |
|---|---|
AsyncGenerator[TChunk | Event]
|
Mixed stream of: |
AsyncGenerator[TChunk | Event]
|
|
AsyncGenerator[TChunk | Event]
|
|
AsyncGenerator[TChunk | Event]
|
|
Raises:
| Type | Description |
|---|---|
RuntimeError
|
If adapter is not set after first chunk processing (internal state error, should not occur in normal usage). |
Example
Plain text
async for event in processor.process_stream(text_stream): ... if isinstance(event, BlockEndEvent): ... print(f"Extracted: {event.block_id}")
With Gemini adapter (auto-detected)
async for event in processor.process_stream(gemini_stream): ... if hasattr(event, 'usage_metadata'): ... print(f"Tokens: {event.usage_metadata}") ... elif isinstance(event, BlockEndEvent): ... print(f"Extracted: {event.block_id}")
Source code in src/hother/streamblocks/core/processor.py
StreamState
dataclass
Tracks state during stream processing.
Source code in src/hother/streamblocks/core/processor.py
start_time
class-attribute
instance-attribute
stream_id
class-attribute
instance-attribute
hother.streamblocks.core.protocol_processor
Protocol stream processor for bidirectional adapter support.
ProtocolStreamProcessor
StreamBlocks processor with bidirectional protocol support.
This processor enables processing of any input stream format and transformation to any output format using the adapter pattern.
The processor supports three usage modes:
- Auto-detect input: Pass
input_adapter=Noneto auto-detect from first event - Explicit adapters: Specify both input and output adapters
- Default output: Pass
output_adapter=Noneto emit native StreamBlocks events
Example
from hother.streamblocks import ProtocolStreamProcessor, Registry from hother.streamblocks.adapters.input import IdentityInputAdapter from hother.streamblocks.adapters.output import StreamBlocksOutputAdapter
Auto-detect input, native output
processor = ProtocolStreamProcessor(registry)
Explicit adapters
processor = ProtocolStreamProcessor( ... registry, ... input_adapter=IdentityInputAdapter(), ... output_adapter=StreamBlocksOutputAdapter(), ... )
Process stream
async for event in processor.process_stream(input_stream): ... if isinstance(event, BlockExtractedEvent): ... print(f"Block: {event.block.metadata.id}")
Source code in src/hother/streamblocks/core/protocol_processor.py
28 29 30 31 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 | |
input_adapter
property
input_adapter: InputProtocolAdapter[TInput] | None
The active input adapter (may be auto-detected).
Returns:
| Type | Description |
|---|---|
InputProtocolAdapter[TInput] | None
|
The input adapter, or None if not yet detected |
output_adapter
property
output_adapter: OutputProtocolAdapter[TOutput]
The active output adapter.
Returns:
| Type | Description |
|---|---|
OutputProtocolAdapter[TOutput]
|
The output adapter |
was_auto_detected
property
was_auto_detected: bool
Whether the input adapter was auto-detected.
Returns:
| Type | Description |
|---|---|
bool
|
True if adapter was detected from first event, False if explicitly provided |
finalize
finalize() -> list[TOutput]
Finalize processing and flush any incomplete blocks.
Call this method after processing all events to get rejection events for any blocks that were opened but never closed.
Returns:
| Type | Description |
|---|---|
list[TOutput]
|
List of output events including rejection events for incomplete blocks |
Source code in src/hother/streamblocks/core/protocol_processor.py
process_chunk
process_chunk(input_event: TInput) -> list[TOutput]
Process a single input event synchronously.
This method is stateful - it maintains internal state between calls. Call finalize() after processing all events to flush incomplete blocks.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
input_event
|
TInput
|
Single input event to process |
required |
Returns:
| Type | Description |
|---|---|
list[TOutput]
|
List of output events generated from this input event |
Source code in src/hother/streamblocks/core/protocol_processor.py
process_stream
async
process_stream(
input_stream: AsyncIterator[TInput],
) -> AsyncIterator[TOutput]
Process input stream through StreamBlocks, emit output protocol events.
This method: 1. Auto-detects input adapter on first event if not specified 2. Categorizes each event (TEXT_CONTENT, PASSTHROUGH, SKIP) 3. For TEXT_CONTENT: extracts text, processes through StreamBlocks, transforms output 4. For PASSTHROUGH: passes event through output adapter's passthrough method 5. For SKIP: doesn't emit anything
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
input_stream
|
AsyncIterator[TInput]
|
Async iterator yielding input events |
required |
Yields:
| Type | Description |
|---|---|
AsyncIterator[TOutput]
|
Output protocol events |
Example
async for event in processor.process_stream(openai_stream): ... if isinstance(event, BlockExtractedEvent): ... print(f"Block: {event.block.metadata.id}")
Source code in src/hother/streamblocks/core/protocol_processor.py
reset
Reset the processor state for reuse.
This clears all internal state including: - Auto-detected adapter (will re-detect on next stream) - Core processor buffers and candidates