rigging.message
This module covers core message objects and handling.
Role = t.Literal['system', 'user', 'assistant']
module-attribute
#
The role of a message. Can be 'system', 'user', or 'assistant'.
Message(role: Role, content: str | list[str | Content], parts: t.Sequence[ParsedMessagePart] | None = None, **kwargs: t.Any)
#
Bases: BaseModel
Represents a message with role, content, and parsed message parts.
Note
Historically, content
was a string, but multi-modal LLMs
require us to have a more structured content representation.
For interface stability, content
will remain a property
accessor for the text of a message, but the "real" content
is available in all_content
. During serialization, we rename
all_content
to content
for compatibility.
Source code in rigging/message.py
all_content: str | list[Content] = Field('', repr=False)
class-attribute
instance-attribute
#
Interior str content or structured content parts.
content: str
property
writable
#
The content of the message.
If the interior of the message content is stored as a list of Content objects, this property will return the concatenated text of any ContentText parts.
models: list[Model]
property
#
Returns a list of models parsed from the message.
parts: list[ParsedMessagePart] = Field(default_factory=list)
class-attribute
instance-attribute
#
The parsed message parts.
role: Role
instance-attribute
#
The role of the message.
uuid: UUID = Field(default_factory=uuid4, repr=False)
class-attribute
instance-attribute
#
The unique identifier for the message.
apply(**kwargs: str) -> Message
#
Applies the given keyword arguments with string templating to the content of the message.
Uses string.Template.safe_substitute underneath.
Note
This call produces a clone of the message, leaving the original message unchanged.
Parameters:
-
**kwargs
(str
, default:{}
) –Keyword arguments to substitute in the message content.
Source code in rigging/message.py
apply_to_list(messages: t.Sequence[Message], **kwargs: str) -> list[Message]
classmethod
#
Helper function to apply keyword arguments to a list of Message objects.
clone() -> Message
#
fit(message: t.Union[Message, MessageDict, str]) -> Message
classmethod
#
Helper function to convert various common types to a Message object.
Source code in rigging/message.py
fit_as_list(messages: t.Sequence[MessageDict] | t.Sequence[Message] | MessageDict | Message | str) -> list[Message]
classmethod
#
Helper function to convert various common types to a strict list of Message objects.
Source code in rigging/message.py
force_str_content() -> Message
#
Forces the content of the message to be a string by stripping any structured content parts like images.
Returns:
-
Message
–The modified message.
from_model(models: Model | t.Sequence[Model], role: Role = 'user', suffix: str | None = None) -> Message
classmethod
#
Create a Message object from one or more Model objects.
Parameters:
-
models
(Model | Sequence[Model]
) –The Model object(s) to convert to a Message.
-
role
(Role
, default:'user'
) –The role of the Message.
-
suffix
(str | None
, default:None
) –A suffix to append to the content.
Returns:
-
Message
–The created Message object.
Source code in rigging/message.py
parse(model_type: type[ModelT]) -> ModelT
#
Parses a model from the message content.
Parameters:
-
model_type
(type[ModelT]
) –The type of model to parse.
Returns:
-
ModelT
–The parsed model.
Raises:
-
ValueError
–If no models of the given type are found and
fail_on_missing
is set toTrue
.
Source code in rigging/message.py
parse_many(*types: type[ModelT]) -> list[ModelT]
#
Parses multiple models of the specified non-identical types from the message content.
Parameters:
-
*types
(type[ModelT]
, default:()
) –The types of models to parse.
Returns:
-
list[ModelT]
–A list of parsed models.
Raises:
-
MissingModelError
–If any of the models are missing.
Source code in rigging/message.py
parse_set(model_type: type[ModelT], minimum: int | None = None) -> list[ModelT]
#
Parses a set of models of the specified identical type from the message content.
Parameters:
-
model_type
(type[ModelT]
) –The type of models to parse.
-
minimum
(int | None
, default:None
) –The minimum number of models required.
Returns:
-
list[ModelT]
–A list of parsed models.
Raises:
-
MissingModelError
–If the minimum number of models is not met.
Source code in rigging/message.py
strip(model_type: type[Model], *, fail_on_missing: bool = False) -> list[ParsedMessagePart]
#
Removes and returns a list of ParsedMessagePart objects from the message that match the specified model type.
Parameters:
-
model_type
(type[Model]
) –The type of model to match.
-
fail_on_missing
(bool
, default:False
) –If True, raises a TypeError if no matching model is found.
Returns:
-
list[ParsedMessagePart]
–A list of removed ParsedMessagePart objects.
Raises:
-
TypeError
–If no matching model is found and fail_on_missing is True.
Source code in rigging/message.py
to_openai_spec() -> dict[str, t.Any]
#
Converts the message to the OpenAI-compatible JSON format. This should be the primary way to serialize a message for use with APIs.
Returns:
-
dict[str, Any]
–The serialized message.
Source code in rigging/message.py
try_parse(model_type: type[ModelT]) -> ModelT | None
#
Tries to parse a model from the message content.
Parameters:
-
model_type
(type[ModelT]
) –The type of model to search for.
Returns:
-
ModelT | None
–The first model that matches the given model type, or None if no match is found.
Source code in rigging/message.py
try_parse_many(*types: type[ModelT], fail_on_missing: bool = False) -> list[ModelT]
#
Tries to parse multiple models from the content of the message.
Parameters:
-
*types
(type[ModelT]
, default:()
) –The types of models to parse.
-
fail_on_missing
(bool
, default:False
) –Whether to raise an exception if a model type is missing.
Returns:
-
list[ModelT]
–A list of parsed models.
Raises:
-
MissingModelError
–If a model type is missing and
fail_on_missing
is True.
Source code in rigging/message.py
try_parse_set(model_type: type[ModelT], minimum: int | None = None, fail_on_missing: bool = False) -> list[ModelT]
#
Tries to parse a set of models from the message content.
Parameters:
-
model_type
(type[ModelT]
) –The type of model to parse.
-
minimum
(int | None
, default:None
) –The minimum number of models expected.
-
fail_on_missing
(bool
, default:False
) –Whether to raise an exception if models are missing.
Returns:
-
list[ModelT]
–The parsed models.
Raises:
-
MissingModelError
–If the number of parsed models is less than the minimum required.
Source code in rigging/message.py
MessageDict
#
Bases: TypedDict
Helper to represent a rigging.message.Message as a dictionary.