message module

class message.EnvStateMessage(*, role: str = 'user', content: str | None = None, content_is_json_str: bool = False, info: dict | None = None)[source]

Bases: Message

A message that contains the current state of the environment.

model_computed_fields: ClassVar[Dict[str, ComputedFieldInfo]] = {}

A dictionary of computed field names and their corresponding ComputedFieldInfo objects.

model_config: ClassVar[ConfigDict] = {}

Configuration for the model, should be a dictionary conforming to [ConfigDict][pydantic.config.ConfigDict].

model_fields: ClassVar[Dict[str, FieldInfo]] = {'content': FieldInfo(annotation=Union[str, NoneType], required=False, default=None, description='Optional message content. Can be a string or a dictionary or None. If a dictionary (for multimodal content), it will be JSON serialized. None is a sentinel value for the absence of content (different than empty string).'), 'content_is_json_str': FieldInfo(annotation=bool, required=False, default=False, description='Whether the content is JSON-serialized (e.g., for multiple modalities).', exclude=True, repr=False), 'info': FieldInfo(annotation=Union[dict, NoneType], required=False, default=None, description='Optional metadata about the message.', exclude=True, repr=False), 'role': FieldInfo(annotation=str, required=False, default='user', description="Message role matching OpenAI's role conventions.")}

Metadata about the fields defined on the model, mapping of field names to [FieldInfo][pydantic.fields.FieldInfo] objects.

This replaces Model.__fields__ from Pydantic V1.

exception message.MalformedMessageError[source]

Bases: ValueError

Error to throw if some aspect of a Message variant is malformed.

classmethod common_retryable_errors_log_filter(record: LogRecord) bool[source]

Filter out common parsing failures not worth looking into from logs.

Returns:

False if the LogRecord should be filtered out, otherwise True to keep it.

class message.Message(*, role: str = 'user', content: str | None = None, content_is_json_str: bool = False, info: dict | None = None)[source]

Bases: BaseModel

DEFAULT_ROLE: ClassVar[str] = 'user'
VALID_ROLES: ClassVar[set[str]] = {'assistant', 'function', 'system', 'tool', 'user'}
append_text(text: str, delim: str = '\n', inplace: bool = True) Message[source]

Append text to the content.

Parameters:
  • text – The text to append.

  • delim – The delimiter to use when concatenating strings.

  • inplace – Whether to modify the message in place.

Returns:

The modified message. Note that the original message is modified and returned if inplace=True and a new message is returned otherwise.

classmethod check_role(v: str) str[source]
content: str | None
content_is_json_str: bool
classmethod create_message(role: str = 'user', text: str | None = None, image: np.ndarray | None = None) Self[source]
info: dict | None
model_computed_fields: ClassVar[Dict[str, ComputedFieldInfo]] = {}

A dictionary of computed field names and their corresponding ComputedFieldInfo objects.

model_config: ClassVar[ConfigDict] = {}

Configuration for the model, should be a dictionary conforming to [ConfigDict][pydantic.config.ConfigDict].

model_dump(*args, **kwargs) dict[source]

Usage docs: https://docs.pydantic.dev/2.9/concepts/serialization/#modelmodel_dump

Generate a dictionary representation of the model, optionally specifying which fields to include or exclude.

Parameters:
  • mode – The mode in which to_python should run. If mode is ‘json’, the output will only contain JSON serializable types. If mode is ‘python’, the output may contain non-JSON-serializable Python objects.

  • include – A set of fields to include in the output.

  • exclude – A set of fields to exclude from the output.

  • context – Additional context to pass to the serializer.

  • by_alias – Whether to use the field’s alias in the dictionary key if defined.

  • exclude_unset – Whether to exclude fields that have not been explicitly set.

  • exclude_defaults – Whether to exclude fields that are set to their default value.

  • exclude_none – Whether to exclude fields that have a value of None.

  • round_trip – If True, dumped values should be valid as input for non-idempotent types such as Json[T].

  • warnings – How to handle serialization errors. False/”none” ignores them, True/”warn” logs errors, “error” raises a [PydanticSerializationError][pydantic_core.PydanticSerializationError].

  • serialize_as_any – Whether to serialize fields with duck-typing serialization behavior.

Returns:

A dictionary representation of the model.

model_fields: ClassVar[Dict[str, FieldInfo]] = {'content': FieldInfo(annotation=Union[str, NoneType], required=False, default=None, description='Optional message content. Can be a string or a dictionary or None. If a dictionary (for multimodal content), it will be JSON serialized. None is a sentinel value for the absence of content (different than empty string).'), 'content_is_json_str': FieldInfo(annotation=bool, required=False, default=False, description='Whether the content is JSON-serialized (e.g., for multiple modalities).', exclude=True, repr=False), 'info': FieldInfo(annotation=Union[dict, NoneType], required=False, default=None, description='Optional metadata about the message.', exclude=True, repr=False), 'role': FieldInfo(annotation=str, required=False, default='user', description="Message role matching OpenAI's role conventions.")}

Metadata about the fields defined on the model, mapping of field names to [FieldInfo][pydantic.fields.FieldInfo] objects.

This replaces Model.__fields__ from Pydantic V1.

role: str
classmethod serialize_content(data)[source]
message.join(msgs: Iterable[Message], delimiter: str = '\n', include_roles: bool = True) str[source]