snoozeml.streams — Event Streaming Envelopes & Clients#
Event streaming message envelopes, wire serializers, and broker clients for SnoozeML.
Maintains strict binary and JSON wire parity with SnoozeML’s Rust streaming engine
(src/stream/message.rs, src/stream/redis_stream.rs, src/stream/kafka.rs,
and src/stream/rabbitmq.rs).
- class snoozeml.streams.StreamWireFormat(*values)[source]#
-
Supported payload serialization formats across SnoozeML streaming transports.
- JSON = 'json'#
Standard JSON serialization matching
StreamInferRequestandStreamInferResponse.
- PROTOBUF = 'protobuf'#
Binary Protobuf serialization matching
snoozeml.v1.ModelInferRequestandModelInferResponse.
- classmethod from_str(value)[source]#
Parses a case-insensitive wire format string into a
StreamWireFormat.- Parameters:
value (StreamWireFormat | str)
- Return type:
- class snoozeml.streams.StreamInferRequest(inputs, model_id=None, id=<factory>, reply_to=None)[source]#
Bases:
objectOutgoing streaming inference request envelope matching
src/stream/message.rs.- Parameters:
inputs (List[TensorInput])
model_id (str | None)
id (str)
reply_to (str | None)
- inputs#
Validated list of
TensorInputtensors.- Type:
- model_id#
Optional target model identifier (overrides the stream binding’s default model).
- Type:
str | None
- id#
Correlation identifier used to match responses to requests (auto-generated UUID if omitted).
- Type:
- reply_to#
Optional dynamic destination topic, queue, or stream key for routing the response.
- Type:
str | None
- inputs: List[TensorInput]#
- classmethod from_inputs(inputs, *, model_id=None, id=None, reply_to=None, default_input_name='input')[source]#
Constructs a
StreamInferRequestfrom NumPy arrays, PyTorch tensors, or dicts.- Parameters:
inputs (Mapping[str, Any | Sequence[Any] | TensorInput] | Sequence[TensorInput | Mapping[str, Any]] | TensorInput | Any) – Input tensors in any format accepted by
normalize_inputs().model_id (str | None) – Optional target model identifier.
id (str | None) – Optional explicit correlation ID (auto-generated if
None).reply_to (str | None) – Optional dynamic egress topic/queue override.
default_input_name (str) – Binding name when a bare array is provided.
- Returns:
Validated
StreamInferRequest.- Return type:
- to_bytes(format=StreamWireFormat.JSON)[source]#
Encodes the request envelope into raw wire bytes for publishing to a broker.
- Parameters:
format (StreamWireFormat | str) – Serialization format (
StreamWireFormat.JSONorStreamWireFormat.PROTOBUF).- Returns:
Encoded byte payload ready for Kafka, RabbitMQ, or Redis Streams.
- Return type:
- classmethod from_bytes(payload, format=StreamWireFormat.JSON)[source]#
Deserializes a
StreamInferRequestfrom raw broker bytes.- Parameters:
payload (bytes | bytearray | memoryview | str) – Raw bytes or UTF-8 string read from a stream broker.
format (StreamWireFormat | str) – Wire format (
StreamWireFormat.JSONorStreamWireFormat.PROTOBUF).
- Returns:
Decoded
StreamInferRequest.- Return type:
- class snoozeml.streams.StreamInferResponse(id, model_id, status='success', outputs=<factory>, error=None, duration_ms=0.0)[source]#
Bases:
objectIncoming streaming inference response envelope published by SnoozeML to output/DLQ topics.
- Parameters:
- id#
Correlation ID matching the original
StreamInferRequest.- Type:
- outputs#
Output tensors wrapped in an
OutputList.- Type:
- outputs: List[TensorOutput]#
- raise_for_status()[source]#
Raises a
RuntimeInferenceErrorif the response represents an error.- Return type:
None
- to_inference_response()[source]#
Converts a successful stream response into a standard
InferenceResponse.- Raises:
RuntimeInferenceError – If
is_successisFalse.- Return type:
- get_tensor(name=None)[source]#
Retrieves an output
TensorOutputby name (or the first output).- Parameters:
name (str | None)
- Return type:
- get_numpy(name=None)[source]#
Returns the output tensor as a reshaped
np.ndarray(dtype=np.float32).
- to_bytes(format=StreamWireFormat.JSON)[source]#
Encodes this response envelope into raw bytes matching
src/stream/message.rs.- Parameters:
format (StreamWireFormat | str)
- Return type:
- classmethod from_dict(data)[source]#
Constructs a
StreamInferResponsefrom a decoded JSON dictionary.- Parameters:
- Return type:
- classmethod from_bytes(payload, format=StreamWireFormat.JSON, default_id='')[source]#
Deserializes a
StreamInferResponsefrom raw broker bytes.- Parameters:
payload (bytes | bytearray | memoryview | str) – Raw bytes or UTF-8 string read from an output or DLQ stream.
format (StreamWireFormat | str) – Wire format (
StreamWireFormat.JSONorStreamWireFormat.PROTOBUF).default_id (str) – Fallback correlation ID when decoding Protobuf responses.
- Returns:
Decoded
StreamInferResponse.- Return type:
- class snoozeml.streams.MockStreamClient(*, client=None, handler=None, default_model_id=None, input_topic='snooze.requests', output_topic='snooze.responses', error_topic='snooze.dlq', format=StreamWireFormat.JSON)[source]#
Bases:
objectDeterministic in-memory streaming client for testing stream pipelines in Python.
Can be wired either to a live
SnoozeClientor to a custom inference handler callback, simulating SnoozeML’ssrc/stream/mock_stream.rsbroker with full JSON and Protobuf wire serialization round-trips.- Parameters:
client (Optional[Any])
handler (Optional[Callable[[str, List[TensorInput]], List[TensorOutput]]])
default_model_id (Optional[str])
input_topic (str)
output_topic (str)
error_topic (str)
format (Union[StreamWireFormat, str])
- publish_request(inputs, *, model_id=None, request_id=None, reply_to=None, topic=None)[source]#
Serializes and publishes a
StreamInferRequestonto the input topic.If
clientorhandleris attached, immediately processes the message and publishes the serializedStreamInferResponseontoreply_tooroutput_topic.
- process_next(*, topic=None)[source]#
Pops one raw request frame from
topic, executes inference, and writes the response frame.- Parameters:
topic (str | None)
- Return type:
StreamInferResponse | None
- read_response(*, correlation_id=None, topic=None)[source]#
Reads and deserializes the next matching
StreamInferResponsefromtopic.- Parameters:
- Return type:
StreamInferResponse | None
- infer(inputs, *, model_id=None, reply_to=None)[source]#
Publishes a request and returns the resulting
InferenceResponse.- Parameters:
inputs (Mapping[str, Any | Sequence[Any] | TensorInput] | Sequence[TensorInput | Mapping[str, Any]] | TensorInput | Any)
model_id (str | None)
reply_to (str | None)
- Return type:
- class snoozeml.streams.RedisStreamClient(endpoint='redis://127.0.0.1:6379', *, input_topic='snooze.requests', output_topic='snooze.responses', default_model_id=None, format=StreamWireFormat.JSON, redis_client=None)[source]#
Bases:
objectRedis Streams producer/consumer client compatible with SnoozeML’s
RedisStreamDriver.Publishes requests via
XADD <input_topic> * payload <bytes>and reads responses from<output_topic>(or per-requestreply_to) viaXREAD.- Parameters:
endpoint (str) – Redis connection URL (e.g.
"redis://127.0.0.1:6379"). Ignored ifredis_clientis passed.input_topic (str) – Redis stream key where SnoozeML consumes requests (default
"snooze.requests").output_topic (str) – Redis stream key where SnoozeML publishes responses (default
"snooze.responses").default_model_id (Optional[str]) – Optional default
model_idattached to outgoing requests.format (Union[StreamWireFormat, str]) – Wire serialization format (
"json"or"protobuf").redis_client (Optional[Any]) – Optional pre-configured
redis.Redisclient instance.
- publish_request(inputs, *, model_id=None, request_id=None, reply_to=None, topic=None)[source]#
Publishes a
StreamInferRequestto Redis Streams viaXADD.- Returns:
Tuple of
(correlation_id, redis_stream_entry_id).- Parameters:
- Return type:
- wait_for_response(correlation_id, *, topic=None, last_id='0-0', timeout=10.0)[source]#
Polls the Redis output stream via
XREADuntil the matchingcorrelation_idarrives.- Parameters:
- Returns:
Matching
StreamInferResponse.- Return type: