academy.message¶
ActionRequest
pydantic-model
¶
Bases: BaseModel
Agent action request message.
Warning
The positional and keywords arguments for the invoked action are
serialized using serialization strategy when the message is
serialized to JSON. This can have non-trivial time and space
overheads for large arguments.
Config:
default:DEFAULT_MUTABLE_CONFIG
Fields:
-
action(str) -
serialization(SerializationStrategy) -
result_serialization(SerializationStrategy | None) -
exception_serialization(SerializationStrategy | None) -
pargs(SkipValidation[tuple[Any, ...]]) -
kargs(SkipValidation[dict[str, Any]]) -
kind(Literal['action-request'])
serialization
pydantic-field
¶
serialization: SerializationStrategy
Serialization strategy used to send args
result_serialization
pydantic-field
¶
result_serialization: SerializationStrategy | None = None
Requested serialization of results. If none or empty, use the same method the args were serialized with.
exception_serialization
pydantic-field
¶
exception_serialization: SerializationStrategy | None = None
Requested serialization of exceptions. If none or empty, use the same method for exceptions as for results.
pargs
pydantic-field
¶
pargs: SkipValidation[tuple[Any, ...]]
Positional arguments to the action method.
kargs
pydantic-field
¶
kargs: SkipValidation[dict[str, Any]]
Keyword arguments to the action method.
get_args
¶
Get the positional arguments.
Lazily deserializes and returns the positional arguments. Caches the result to avoid redundant decoding.
Returns:
Source code in academy/message.py
get_kwargs
¶
Get the keyword arguments.
Lazily deserializes and returns the keyword arguments. Caches the result to avoid redundant decoding.
Returns:
Source code in academy/message.py
PingRequest
pydantic-model
¶
CancelRequest
pydantic-model
¶
ShutdownRequest
pydantic-model
¶
ActionResponse
pydantic-model
¶
Bases: BaseModel
Agent action response message.
Warning
The result is serialized using serialization when the response is
serialized to JSON. This can have non-trivial time and space
overheads for large results.
Config:
default:DEFAULT_MUTABLE_CONFIG
Fields:
-
result(SkipValidation[Any]) -
serialization(SerializationStrategy) -
kind(Literal['action-response'])
serialization
pydantic-field
¶
serialization: SerializationStrategy
Serialization strategy used to send result.
get_result
¶
get_result() -> Any
Get the result.
Lazily deserializes and returns the result of the action. Caches the result to avoid redundant decoding.
Returns:
-
Any–The deserialized result of the action.
Source code in academy/message.py
ErrorCode
¶
Bases: IntEnum
Error codes returned by requests.
These error codes allow us to return errors without serialization.
AcademyErrorResponse
pydantic-model
¶
Bases: BaseModel
Error response created by Academy.
Fields:
-
error_code(ErrorCode) -
mailbox_id(EntityId | None) -
kind(Literal['academy-error-response'])
mailbox_id
pydantic-field
¶
mailbox_id: EntityId | None = None
Mailbox id if necessary for the error.
get_exception
¶
get_exception() -> Exception
Get the exception.
Returns:
-
Exception–The exception.
Source code in academy/message.py
UserErrorResponse
pydantic-model
¶
Bases: BaseModel
Error response message.
Contains the exception raised by a failed request.
Config:
default:DEFAULT_MUTABLE_CONFIG
Fields:
-
serialization(SerializationStrategy) -
exception(SkipValidation[Exception]) -
kind(Literal['user-error-response'])
serialization
pydantic-field
¶
serialization: SerializationStrategy
Serialization strategy used to send exception.
get_exception
¶
get_exception() -> Exception
Get the exception.
Lazily deserializes and returns the exception object. Caches the result to avoid redundant decoding.
Returns:
-
Exception–The deserialized exception.
Source code in academy/message.py
SuccessResponse
pydantic-model
¶
Header
pydantic-model
¶
Bases: BaseModel
Message metadata header.
Contains information about the sender, receiver, and message context.
Config:
default:DEFAULT_FROZEN_CONFIG
Fields:
-
src(EntityId) -
dest(EntityId) -
tag(UUID) -
label(UUID | None) -
kind(Literal['request', 'response']) -
protocol_version(str | None)
label
pydantic-field
¶
label: UUID | None = None
Optional label used to disambiguate response messages when multiple objects (i.e., handles) share the same mailbox. This is a different usage from the tag.
protocol_version
pydantic-field
¶
Version of the academy protocol used. Messages within a major version are intended to be mutually compatible, while minor version changes might introduce optional fields or remove existing fields without prohibiting their existence.
create_response_header
¶
Create a response header based on the current request header.
Swaps the source and destination, retains the tag and label, and sets the kind to 'response'.
Returns:
-
Self–A new header instance with reversed roles.
Raises:
-
ValueError–If the current header is already a response.
Source code in academy/message.py
Message
pydantic-model
¶
Bases: BaseModel, Generic[BodyT]
A complete message with header and body.
Wraps a header and a typed request/response body. Supports lazy deserialization of message bodies, metadata access, and convenient construction.
Note
The body value is ignored when testing equality or hashing an instance
because the body value could be in either a serialized or
deserialized state until
get_body() is called.
Config:
default:DEFAULT_MUTABLE_CONFIG
Fields:
-
header(Header) -
body(SkipValidation[BodyT])
create
classmethod
¶
create(
src: EntityId,
dest: EntityId,
body: BodyT,
*,
label: UUID | None = None,
tag: UUID | None = None
) -> Message[BodyT]
Create a new message with the specified header and body.
Parameters:
-
src(EntityId) –Source entity ID.
-
dest(EntityId) –Destination entity ID.
-
body(BodyT) –Message body.
-
label(UUID | None, default:None) –Optional label for disambiguation.
-
tag(UUID | None, default:None) –Optional tag for relating responses to requests.
Returns:
-
Message[BodyT]–A new message instance.
Source code in academy/message.py
create_response
¶
create_response(body: ResponseT) -> Message[ResponseT]
Create a response message from this request message.
Parameters:
-
body(ResponseT) –Response message body.
Returns:
-
Message[ResponseT]–A new response message instance.
Raises:
-
ValueError–If this message is already a response.
Source code in academy/message.py
get_body
¶
Return the message body, deserializing if needed.
Lazily deserializes and returns the body object. Caches the body to avoid redundant decoding.
Returns:
-
BodyT–The deserialized body.
Source code in academy/message.py
model_deserialize
classmethod
¶
model_serialize
¶
model_serialize() -> bytes
log_extra
¶
Returns extra info useful in logs about this Message.
Source code in academy/message.py
check_version
¶
Checks if a protocol_version is compatible.
Messages within a major version are intended to be mutually compatible, while minor version changes might introduce optional fields or remove existing fields without prohibiting their existence. Local tags may be added for branches/forks and must match exactly.
Parameters:
-
version(str | None) –The version string to check
Returns:
-
bool–True if the version is compatible.