pub trait ProviderAdapter:
Send
+ Sync
+ Debug {
// Required methods
fn provider_name(&self) -> &str;
fn convert_request(&self, payload: Value) -> Value;
fn convert_response(&self, response: Value) -> Value;
fn api_url(&self) -> &str;
// Provided methods
fn extra_headers(&self) -> Vec<(String, String)> { ... }
fn supports_streaming(&self) -> bool { ... }
fn enable_streaming(&self, _payload: &mut Value) { ... }
fn streaming_url(&self, base_url: &str) -> Option<String> { ... }
fn parse_stream_event(
&self,
_event_type: &str,
_data: &Value,
) -> Option<StreamEvent> { ... }
}Expand description
Trait for converting between the internal Chat Completions format and provider-specific API formats.
Implementations handle provider quirks like:
- Different message formats (Anthropic vs OpenAI)
- Prompt caching headers/fields
- Reasoning model parameters (o1/o3)
- Image block normalization
Required Methods§
Sourcefn provider_name(&self) -> &str
fn provider_name(&self) -> &str
Provider identifier (e.g., “openai”, “anthropic”).
Sourcefn convert_request(&self, payload: Value) -> Value
fn convert_request(&self, payload: Value) -> Value
Convert an internal Chat Completions payload to provider-specific format.
The input is always in OpenAI Chat Completions format. The adapter transforms it as needed for its provider’s API.
Sourcefn convert_response(&self, response: Value) -> Value
fn convert_response(&self, response: Value) -> Value
Convert a provider-specific response back to Chat Completions format.
The output should be in standard OpenAI Chat Completions response format so downstream code can handle all providers uniformly.
Provided Methods§
Sourcefn extra_headers(&self) -> Vec<(String, String)>
fn extra_headers(&self) -> Vec<(String, String)>
Get required headers for this provider (e.g., api-version, anthropic-version).
Sourcefn supports_streaming(&self) -> bool
fn supports_streaming(&self) -> bool
Whether this adapter supports streaming responses.
Sourcefn enable_streaming(&self, _payload: &mut Value)
fn enable_streaming(&self, _payload: &mut Value)
Add streaming parameters to the request payload.
Called before sending when streaming is requested. The adapter should
add provider-specific streaming flags (e.g., stream: true).
Sourcefn streaming_url(&self, base_url: &str) -> Option<String>
fn streaming_url(&self, base_url: &str) -> Option<String>
Get the streaming API URL, if different from the regular API URL.
Some providers use a different endpoint for streaming (e.g., Gemini
uses streamGenerateContent instead of generateContent). Returns
None to use the client’s default URL.
Sourcefn parse_stream_event(
&self,
_event_type: &str,
_data: &Value,
) -> Option<StreamEvent>
fn parse_stream_event( &self, _event_type: &str, _data: &Value, ) -> Option<StreamEvent>
Parse a single SSE event into a stream event.
event_type is the SSE event name (from event: line).
data is the parsed JSON from the data: line.