Skip to main content

ResponseTransformFn

Type Alias ResponseTransformFn 

Source
pub type ResponseTransformFn = Arc<dyn Fn(&str, &HeaderMap, &[u8], Option<&str>) -> Result<Option<Bytes>, String> + Send + Sync>;
Expand description

Type alias for response transformation function

Takes (path, headers, body_bytes, original_model) and returns transformed body_bytes or None if no transformation. This allows you to sanitize and modify response bodies before they are returned to clients.

§Arguments

  • &str - The request path (e.g., “/v1/chat/completions”)
  • &HeaderMap - HTTP response headers from the upstream provider
  • &[u8] - The response body as raw bytes
  • Option<&str> - The original model requested by the client (for rewriting)

§Returns

  • Ok(Some(Bytes)) - Transformed response body to return
  • Ok(None) - Use original response body unchanged
  • Err(String) - Transformation failed with error message

§Examples

use onwards::ResponseTransformFn;
use axum::http::HeaderMap;
use std::sync::Arc;

// Transform function that sanitizes OpenAI responses
let transform: ResponseTransformFn = Arc::new(|path, _headers, body_bytes, _model| {
    if path.contains("/v1/chat/completions") {
        // Sanitization logic here
        Ok(Some(axum::body::Bytes::from(body_bytes.to_vec())))
    } else {
        Ok(None)
    }
});

Aliased Type§

pub struct ResponseTransformFn { /* private fields */ }