Skip to main content

RawAdapter

Trait RawAdapter 

Source
pub trait RawAdapter: Send + Sync {
    // Required methods
    fn build_request(
        &self,
        request: &ChatRequest,
        mode: CallMode,
    ) -> Result<RawRequest, LlmError>;
    fn execute_stream<'life0, 'life1, 'async_trait>(
        &'life0 self,
        client: &'life1 dyn HttpClient,
        request: RawRequest,
    ) -> Pin<Box<dyn Future<Output = Result<ChatStream, LlmError>> + Send + 'async_trait>>
       where 'life0: 'async_trait,
             'life1: 'async_trait,
             Self: 'async_trait;
    fn capabilities(&self) -> Capabilities;
    fn info(&self) -> ProviderInfo;

    // Provided methods
    fn parse_sse_stream<'life0, 'life1, 'async_trait>(
        &'life0 self,
        _client: &'life1 dyn HttpClient,
        _request: RawRequest,
        _response: HttpResponse,
    ) -> Pin<Box<dyn Future<Output = Result<ChatStream, LlmError>> + Send + 'async_trait>>
       where 'life0: 'async_trait,
             'life1: 'async_trait,
             Self: 'async_trait { ... }
    fn parse_response(&self, _body: &[u8]) -> Result<ChatResponse, LlmError> { ... }
    fn supported_modes(&self) -> &[CallMode] { ... }
}
Expand description

Low-level adapter trait.

Implement this to get full LlmProvider functionality, wrapped automatically by GenericProvider.

Implementors must provide:

  • build_request - Build HTTP request
  • execute_stream - Required to implement, but GenericProvider does not call it; it is the hook for adapters that own the whole send-and-parse flow
  • parse_sse_stream - Parse SSE from a pre-fetched response. Override this for streaming to work: the default returns an error, and it is this method GenericProvider calls.
  • capabilities - Declare capabilities
  • info - Provide info

Optional:

  • parse_response - Parse non-streaming response (default returns an error). chat() only falls back to streaming when supported_modes() omits CallMode::Once — since that is not the default, an adapter that does not implement parse_response must also narrow supported_modes, or chat() will return the default error.

Note: the adapter is fully responsible for stream parsing, including SSE frame parsing and incremental tool call assembly. GenericProvider does not get involved in stream details.

Required Methods§

Source

fn build_request( &self, request: &ChatRequest, mode: CallMode, ) -> Result<RawRequest, LlmError>

Build HTTP request.

Source

fn execute_stream<'life0, 'life1, 'async_trait>( &'life0 self, client: &'life1 dyn HttpClient, request: RawRequest, ) -> Pin<Box<dyn Future<Output = Result<ChatStream, LlmError>> + Send + 'async_trait>>
where 'life0: 'async_trait, 'life1: 'async_trait, Self: 'async_trait,

Execute streaming request, fully parse SSE response.

The implementor receives &dyn HttpClient and parses the stream protocol, returning ChatStream. This gives the adapter full control, suitable for different providers’ SSE format differences.

Typical implementation:

  1. Send request via client.send(&request)
  2. Check HTTP status code
  3. Parse SSE stream (according to provider’s protocol format)
  4. Incremental tool call assembly (if needed)
  5. Return ChatStream
Source

fn capabilities(&self) -> Capabilities

Get capabilities.

Source

fn info(&self) -> ProviderInfo

Get info.

Provided Methods§

Source

fn parse_sse_stream<'life0, 'life1, 'async_trait>( &'life0 self, _client: &'life1 dyn HttpClient, _request: RawRequest, _response: HttpResponse, ) -> Pin<Box<dyn Future<Output = Result<ChatStream, LlmError>> + Send + 'async_trait>>
where 'life0: 'async_trait, 'life1: 'async_trait, Self: 'async_trait,

Parse SSE stream from an already-received HTTP response.

This method is called by GenericProvider after a successful HTTP response (with retry logic applied). The adapter only needs to parse the SSE stream, not send the HTTP request.

Default implementation: returns an error. Adapters must override this — GenericProvider calls it rather than execute_stream once a response is in hand, so an unimplemented override surfaces as a stream error rather than a silently re-sent request.

Source

fn parse_response(&self, _body: &[u8]) -> Result<ChatResponse, LlmError>

Parse non-streaming response.

Default: not supported. Override this if the adapter supports non-streaming mode.

Source

fn supported_modes(&self) -> &[CallMode]

Supported call modes.

Dyn Compatibility§

This trait is dyn compatible.

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

Implementors§