guidellm.utils.encoding
Message encoding utilities for multiprocess communication with Pydantic model support.
Provides binary serialization and deserialization of Python objects using various serialization formats and encoding packages to enable performance configurations for distributed scheduler operations. Supports configurable two-stage processing pipeline: object serialization (to dict/sequence) followed by binary encoding (msgpack/msgspec) with specialized Pydantic model handling for type preservation.
Encoder
Binary encoding and decoding using MessagePack or msgspec formats.
Handles binary serialization of Python objects using configurable encoding strategies with automatic fallback when dependencies are unavailable. Supports both standalone instances and pooled encoder/decoder pairs for performance optimization in high-throughput scenarios.
Source code in src/guidellm/utils/encoding.py
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 | |
__init__(encoding=None)
Initialize encoder with specified encoding strategy.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
encoding | EncodingTypesAlias | list[EncodingTypesAlias] | Encoding format preference (None, "msgpack", "msgspec", or preference list) | None |
Source code in src/guidellm/utils/encoding.py
decode(data)
Decode binary data using configured encoding strategy.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
data | bytes | Any | Binary data to decode or object if no encoding configured | required |
Returns:
| Type | Description |
|---|---|
Any | Decoded Python object |
Raises:
| Type | Description |
|---|---|
ImportError | If required encoding library is not available |
Source code in src/guidellm/utils/encoding.py
encode(obj)
Encode object to binary format using configured encoding strategy.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
obj | Any | Object to encode (must be serializable by chosen format) | required |
Returns:
| Type | Description |
|---|---|
bytes | Any | Encoded bytes or original object if no encoding configured |
Raises:
| Type | Description |
|---|---|
ImportError | If required encoding library is not available |
Source code in src/guidellm/utils/encoding.py
MessageEncoding
Bases: Generic[ObjT, MsgT]
High-performance message encoding and decoding for multiprocessing communication.
Supports configurable object serialization and binary encoding with specialized handling for Pydantic models. Provides a two-stage pipeline of serialization (object to dict/str) followed by encoding (dict/str to binary) for optimal performance and compatibility across different transport mechanisms used in distributed scheduler operations.
Example: :: from guidellm.utils.encoding import MessageEncoding from pydantic import BaseModel
class DataModel(BaseModel):
name: str
value: int
# Configure with dict serialization and msgpack encoding
encoding = MessageEncoding(serialization="dict", encoding="msgpack")
encoding.register_pydantic(DataModel)
# Encode and decode objects
data = DataModel(name="test", value=42)
encoded_msg = encoding.encode(data)
decoded_data = encoding.decode(encoded_msg)
Attributes:
| Name | Type | Description |
|---|---|---|
DEFAULT_ENCODING_PREFERENCE | list[str] | Preferred encoding formats in priority order |
Source code in src/guidellm/utils/encoding.py
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 | |
__init__(serialization=None, encoding=None, pydantic_models=None)
Initialize MessageEncoding with serialization and encoding strategies.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
serialization | SerializationTypesAlias | Serialization strategy (None, "dict", or "sequence") | None |
encoding | EncodingTypesAlias | list[EncodingTypesAlias] | Encoding strategy (None, "msgpack", "msgspec", or preference list) | None |
Source code in src/guidellm/utils/encoding.py
decode(message)
Decode message using instance configuration.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
message | MsgT | Encoded message to decode using configured strategies | required |
Returns:
| Type | Description |
|---|---|
ObjT | Reconstructed object |
Source code in src/guidellm/utils/encoding.py
decode_message(message, serializer, encoder) classmethod
Decode message using specified serializer and encoder. Must match the encoding configuration originally used.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
message | MsgT | Encoded message to decode | required |
serializer | Serializer | None | Serializer for object reconstruction, None for no serialization | required |
encoder | Encoder | None | Encoder for binary decoding, None for no encoding | required |
Returns:
| Type | Description |
|---|---|
ObjT | Reconstructed object |
Source code in src/guidellm/utils/encoding.py
encode(obj)
Encode object using instance configuration.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
obj | ObjT | Object to encode using configured serialization and encoding | required |
Returns:
| Type | Description |
|---|---|
MsgT | Encoded message ready for transport |
Source code in src/guidellm/utils/encoding.py
encode_message(obj, serializer, encoder) classmethod
Encode object using specified serializer and encoder.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
obj | ObjT | Object to encode | required |
serializer | Serializer | None | Serializer for object conversion, None for no serialization | required |
encoder | Encoder | None | Encoder for binary conversion, None for no encoding | required |
Returns:
| Type | Description |
|---|---|
MsgT | Encoded message ready for transport |
Source code in src/guidellm/utils/encoding.py
register_pydantic(model)
Register Pydantic model for specialized serialization handling.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
model | type[BaseModel] | Pydantic model class to register for type preservation | required |
Source code in src/guidellm/utils/encoding.py
Serializer
Object serialization with specialized Pydantic model support.
Converts Python objects to serializable formats (dict/sequence) with type preservation for Pydantic models. Maintains object integrity through encoding/decoding cycles by storing class metadata and enabling proper reconstruction of complex objects. Supports both dictionary-based and sequence-based serialization strategies for different use cases.
Source code in src/guidellm/utils/encoding.py
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 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 | |
__init__(serialization=None, pydantic_models=None)
Initialize serializer with strategy and Pydantic registry.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
serialization | SerializationTypesAlias | Default serialization strategy for this instance | None |
Source code in src/guidellm/utils/encoding.py
deserialize(msg)
Deserialize object using specified or configured strategy.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
msg | Any | Serialized message to deserialize | required |
Returns:
| Type | Description |
|---|---|
Any | Reconstructed object |
Source code in src/guidellm/utils/encoding.py
from_dict(data)
Reconstruct object from dictionary with Pydantic model type restoration.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
data | Any | Dictionary representation possibly containing type metadata | required |
Returns:
| Type | Description |
|---|---|
Any | Reconstructed object with proper types restored |
Source code in src/guidellm/utils/encoding.py
from_dict_pydantic(item)
Reconstruct object from dictionary with Pydantic type metadata.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
item | dict[str, Any] | Dictionary containing type metadata and data | required |
Returns:
| Type | Description |
|---|---|
Any | Reconstructed Pydantic model or original data |
Source code in src/guidellm/utils/encoding.py
from_sequence(data)
Reconstruct object from sequence format with type restoration.
Handles deserialization of objects encoded with to_sequence, properly restoring Pydantic models and collection structures.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
data | str | Any | Serialized sequence data to reconstruct | required |
Returns:
| Type | Description |
|---|---|
Any | Reconstructed object with proper types |
Raises:
| Type | Description |
|---|---|
ValueError | If sequence format is invalid or contains multiple packed sequences |
Source code in src/guidellm/utils/encoding.py
from_sequence_pydantic(data)
Reconstruct Pydantic model from sequence format.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
data | str | bytes | Sequence data containing class metadata and JSON | required |
Returns:
| Type | Description |
|---|---|
BaseModel | Reconstructed Pydantic model instance |
Source code in src/guidellm/utils/encoding.py
from_sequence_python(data)
Deserialize Python object from JSON format.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
data | str | bytes | JSON string or bytes to deserialize | required |
Returns:
| Type | Description |
|---|---|
Any | Reconstructed Python object |
load_pydantic(type_name, module_name)
Load Pydantic class by name with registry fallback to dynamic import.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
type_name | str | Class name to load | required |
module_name | str | Module containing the class | required |
Returns:
| Type | Description |
|---|---|
type[BaseModel] | Loaded Pydantic model class |
Source code in src/guidellm/utils/encoding.py
pack_next_sequence(type_, payload, current)
Pack payload into sequence format with type and length metadata.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
type_ | PayloadType | Type identifier for the payload | required |
payload | str | bytes | Data to pack into sequence | required |
current | str | bytes | None | Current sequence data to append to (unused but maintained for signature compatibility) | required |
Returns:
| Type | Description |
|---|---|
str | bytes | Packed sequence with type, length, and payload |
Raises:
| Type | Description |
|---|---|
ValueError | If payload type doesn't match current type or unknown type specified |
Source code in src/guidellm/utils/encoding.py
register_pydantic(model)
Register Pydantic model for specialized serialization handling.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
model | type[BaseModel] | Pydantic model class to register for type preservation | required |
Source code in src/guidellm/utils/encoding.py
serialize(obj)
Serialize object using specified or configured strategy.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
obj | Any | Object to serialize | required |
Returns:
| Type | Description |
|---|---|
Any | Serialized representation (dict, str, or original object) |
Source code in src/guidellm/utils/encoding.py
to_dict(obj)
Convert object to dictionary with Pydantic model type preservation.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
obj | Any | Object to convert (BaseModel, collections, or primitive) | required |
Returns:
| Type | Description |
|---|---|
Any | Dictionary representation with type metadata for Pydantic models |
Source code in src/guidellm/utils/encoding.py
to_dict_pydantic(item)
Convert item to dictionary with Pydantic type metadata.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
item | Any | Item to convert (may or may not be a Pydantic model) | required |
Returns:
| Type | Description |
|---|---|
Any | Dictionary with type preservation metadata |
Source code in src/guidellm/utils/encoding.py
to_sequence(obj)
Convert object to sequence format with type-aware serialization.
Handles Pydantic models, collections, and mappings with proper type preservation through structured sequence encoding.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
obj | Any | Object to serialize to sequence format | required |
Returns:
| Type | Description |
|---|---|
str | Any | Serialized sequence string or bytes |
Source code in src/guidellm/utils/encoding.py
to_sequence_pydantic(obj)
Serialize Pydantic model to sequence format with class metadata.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
obj | BaseModel | Pydantic model instance to serialize | required |
Returns:
| Type | Description |
|---|---|
str | bytes | Sequence string or bytes containing class info and JSON data |
Source code in src/guidellm/utils/encoding.py
to_sequence_python(obj)
Serialize Python object to JSON format.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
obj | Any | Python object to serialize | required |
Returns:
| Type | Description |
|---|---|
str | bytes | JSON string or bytes representation |
unpack_next_sequence(data)
Unpack sequence format to extract type, payload, and remaining data.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
data | str | bytes | Packed sequence data to unpack | required |
Returns:
| Type | Description |
|---|---|
tuple[PayloadType, str | bytes, str | bytes | None] | Tuple of (type, payload, remaining_data) |
Raises:
| Type | Description |
|---|---|
ValueError | If sequence format is invalid or unknown type character |