Module interceptor

Module interceptor 

Source
Expand description

Interceptor system for middleware and observability.

Interceptors provide hooks into the request/response lifecycle, enabling:

  • Telemetry and tracing
  • Logging and debugging
  • Metrics collection
  • Request/response transformation
  • Custom error handling
  • Authentication enhancement

§Architecture

The interceptor system follows a chain-of-responsibility pattern where multiple interceptors can be registered and executed in order. Each interceptor can:

  • Modify request context before sending
  • Observe and react to responses
  • Handle streaming chunks
  • Process errors

§Example

use openai_ergonomic::{Client, Interceptor, BeforeRequestContext};

struct LoggingInterceptor;

#[async_trait::async_trait]
impl Interceptor for LoggingInterceptor {
    async fn before_request(&self, ctx: &mut BeforeRequestContext<'_>) -> Result<()> {
        println!("Calling {} with model {}", ctx.operation, ctx.model);
        Ok(())
    }
}

let client = Client::from_env()?
    .with_interceptor(Box::new(LoggingInterceptor))
    .build();

Structs§

AfterResponseContext
Context provided after a successful non-streaming response.
BeforeRequestContext
Context provided before a request is sent.
ErrorContext
Context provided when an error occurs.
InterceptorChain
A chain of interceptors that are executed in order.
StreamChunkContext
Context provided for each chunk in a streaming response.
StreamEndContext
Context provided when a streaming response completes.

Traits§

Interceptor
Trait for implementing interceptors.