Skip to main content
Version: Next

mellea.formatters.chat_formatter

ChatFormatter for converting context histories to chat-message lists.

ChatFormatter is the standard formatter used by mellea's legacy backends. Its to_chat_messages method linearises a sequence of Component and CBlock objects into Message objects with user, assistant, or tool roles, handling ModelOutputThunk responses, image attachments, and parsed structured outputs. Concrete backends call this formatter when preparing input for a chat completion endpoint.

Classes

CLASS ChatFormatter

Formatter used by Legacy backends to format Contexts as Messages.

Methods:

FUNC to_chat_messages

to_chat_messages(self, cs: list[Span]) -> list[Message]

Convert a linearized chat history into a list of chat messages.

Iterates over each element in the context history and converts it to a Message with an appropriate role. ModelOutputThunk instances are treated as assistant responses, while all other Component and CBlock objects default to the user role. A Component may override this positional guess by setting role on the TemplateRepresentation returned from its format_for_llm, and a component with role="tool" may additionally declare tool_name/tool_args/tool_call_id to be rendered as a ToolMessage. Image attachments and parsed structured outputs are handled transparently.

Args:

  • cs: The linearized sequence of context components, content blocks, and model outputs to convert.

Returns:

  • list[Message]: A list of Message objects ready for submission to a chat completion endpoint.

Raises:

  • ValueError: If a component declares a role (via its TemplateRepresentation) outside Message.Role; role validation is deferred to Message/ToolMessage.