Skip to main content

MessageFormatter

Trait MessageFormatter 

Source
pub trait MessageFormatter:
    Send
    + Sync
    + 'static {
    // Required methods
    fn name(&self) -> &str;
    fn format_messages(&self, messages: &[Message]) -> Result<Value, PeError>;
    fn format_tools(&self, tools: &[ToolSchema]) -> Result<Value, PeError>;
    fn parse_response(&self, raw: &Value) -> Result<LlmResponse, PeError>;
}
Expand description

Trait for converting between library Message types and provider-specific wire formats.

Each LLM provider has its own message format. The formatter handles the translation so providers only deal with their native format.

Implementors produce serde_json::Value so the provider HTTP layer can embed the result directly into request bodies without further conversion.

Required Methods§

Source

fn name(&self) -> &str

Provider name for debugging and logging.

§Example
use pe_core::openai_formatter::OpenAiFormatter;
use pe_core::formatter::MessageFormatter;

assert_eq!(OpenAiFormatter.name(), "openai");
Source

fn format_messages(&self, messages: &[Message]) -> Result<Value, PeError>

Convert library messages to the provider’s wire format (as JSON Value).

Returns an array of message objects in the provider’s expected format. Unknown or unsupported message variants are silently skipped.

Source

fn format_tools(&self, tools: &[ToolSchema]) -> Result<Value, PeError>

Convert tool schemas to the provider’s tool format (as JSON Value).

Returns an array of tool definition objects. Returns an empty array for empty input.

Source

fn parse_response(&self, raw: &Value) -> Result<LlmResponse, PeError>

Parse a provider response back into library types.

Takes the raw JSON response body from the provider and extracts the AI message, usage metadata, and provider-specific metadata.

Dyn Compatibility§

This trait is dyn compatible.

In older versions of Rust, dyn compatibility was called "object safety".

Implementors§