-
-
Notifications
You must be signed in to change notification settings - Fork 245
Open
Description
Feature Request
Problem
Currently, the Laravel facade doesn't provide a way to set custom headers for individual requests. Headers can only be set globally when the client is instantiated in the ServiceProvider. This limitation makes it difficult to add request-specific headers for solutions like AI Gateway from Cloudflare: https://developers.cloudflare.com/ai-gateway/configuration/caching/#per-request-caching
Proposed Solution
Add a withHeaders()
method that can be chained before accessing resources like chat()
, completions()
, etc.
Ideal API:
use OpenAI\Laravel\Facades\OpenAI;
$result = OpenAI::withHeaders([
'x-custom-header' => 'custom-value',
'cf-aig-skip-cache' => true,
])
->chat()
->create([
'model' => 'o3',
'messages' => [
['role' => 'user', 'content' => 'Hello!'],
],
]);
Benefits
- Enables request-specific headers without creating multiple client instances
- Maintains the clean facade API while adding flexibility
- Useful for logging, tracing, authentication, and other middleware scenarios
Current Workaround
Users currently need to create separate client instances with custom HTTP clients, which is verbose and doesn't integrate well with the Laravel facade pattern.
Metadata
Metadata
Assignees
Labels
No labels