Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat/Interceptor #2032

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open

feat/Interceptor #2032

wants to merge 3 commits into from

Conversation

Nisarg38
Copy link

Add Request/Response Interceptor Pattern

Overview

This PR introduces a flexible interceptor pattern to the OpenAI Python client, allowing users to intercept and modify requests before they are sent and responses after they are received. This enables powerful use cases like logging, retrying, metrics collection, and custom request/response manipulation.

Key Components

Core Interceptor Framework

  • InterceptorRequest: Data class containing request details (method, URL, headers, params, body)
  • InterceptorResponse: Generic data class for response data (status code, headers, body, original request)
  • Interceptor: Abstract base class defining the interceptor interface
  • InterceptorChain: Manages a sequence of interceptors, executing them in order

Example Implementations

Added example interceptors demonstrating practical use cases:

  1. LoggingInterceptor: Provides detailed request/response logging
  2. RetryInterceptor: Implements exponential backoff retry logic for 5xx errors

Usage Example

client = OpenAI( interceptors=[ LoggingInterceptor(), RetryInterceptor(max_retries=3, initial_delay=1.0) ])

Benefits

  • Extensibility: Easy to add new interceptors for custom behavior
  • Separation of Concerns: Clean separation between core client logic and auxiliary functionality
  • Error Resilience: Built-in error handling in interceptor chain execution
  • Type Safety: Full type annotations and generic support

Testing

  • Added example usage in examples/interceptor.py
  • Tested with logging and retry scenarios
  • Verified error handling and chain execution

Future Improvements

  • Add more built-in interceptors for common use cases
  • Consider adding async support
  • Add interceptor ordering/priority mechanism

Breaking Changes

None. This is a new feature that maintains backward compatibility.

@Nisarg38 Nisarg38 requested a review from a team as a code owner January 18, 2025 08:05
@Nisarg38 Nisarg38 changed the title Interceptor feat/Interceptor Jan 18, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant