jina.types.message package

Module contents

class jina.types.message.Message(envelope, request, *args, **kwargs)[source]

Bases: object

Message is one of the primitive data type in Jina.

It offers a Pythonic interface to allow users access and manipulate jina.jina_pb2.MessageProto object without working with Protobuf itself.

A container class for jina_pb2.MessageProto. Note, the Protobuf version of jina_pb2.MessageProto contains a jina_pb2.EnvelopeProto and jina_pb2.RequestProto. Here, it contains:

  • a jina_pb2.EnvelopeProto object

  • and one of:
    • a Request object wrapping jina_pb2.RequestProto

    • a jina_pb2.RequestProto object

It provide a generic view of as jina_pb2.MessageProto, allowing one to access its member, request and envelope as if using jina_pb2.MessageProto object directly.

This class also collected all helper functions related to jina_pb2.MessageProto into one place.

Parameters
  • envelope (Union[bytes, EnvelopeProto, None]) – Represents a Envelope, a part of the Message.

  • request (Union[bytes, RequestProto]) – Represents a Request

  • args – Additional positional arguments.

  • kwargs – Additional keyword arguments.

classmethod from_proto(msg)[source]

Creates a new Message object from a given MessageProto object.

Parameters

msg (MessageProto) – the to-be-copied message

Returns

the new message object

property request

Get the request.

Return type

Request

Returns

request

property proto

Get the RequestProto.

Return type

MessageProto

Returns

protobuf object

property is_data_request

check if the request is not a control request

Warning

If request change the type, e.g. by leveraging the feature of oneof, this property wont be updated. This is not considered as a good practice.

Return type

bool

Returns

boolean which states if data is requested

dump()[source]

Get the message in a list of bytes.

Return type

List[bytes]

Returns

array, containing encoded receiver id, serialized envelope and the compressed serialized envelope

property colored_route

Get the string representation of the routes in a message.

Return type

str

Returns

colored route

add_route(name, identity)[source]

Add a route to the envelope.

Parameters
  • name (str) – the name of the pod service

  • identity (str) – the identity of the pod service

property size

Get the size in bytes.

To get the latest size, use it after dump() :return: size of the message

update_timestamp()[source]

Update the timestamp of the last route

property response

Get the response of the message in protobuf.

Note

This should be only called at Gateway

Return type

Request

Returns

request object which contains the response

merge_envelope_from(msgs)[source]

Extend the current envelope routes with :param: msgs.

Parameters

msgs (List[Message]) – List of msgs.

add_exception(ex=None, executor=None)[source]

Add exception to the last route in the envelope

Parameters
  • ex (Optional[ForwardRef]) – Exception to be added

  • executor (BaseExecutor) – Executor related to the exception

Return type

None

property is_error

Return if the envelope status is ERROR.

return

boolean stating if the status code of the envelope is error

Return type

bool

property is_ready

Return if the envelope status is READY.

Return type

bool

Returns

boolean stating if the status code of the envelope is ready