- How do I install and set up openai-php/laravel in a Laravel project?
- Run `composer require openai-php/laravel` to install the package, then execute `php artisan openai:install` to generate the `config/openai.php` file. Add your OpenAI API key and organization ID to your `.env` file using `OPENAI_API_KEY` and `OPENAI_ORGANIZATION`. The package automatically appends these variables to your `.env` during installation.
- Which Laravel versions and PHP versions does this package support?
- The package requires **PHP 8.2+** and is officially compatible with **Laravel 10 and 11**. The latest release (2026-03-17) also supports Laravel 13. Check the [GitHub releases](https://github.com/openai-php/laravel/releases) for version-specific details.
- Can I use this package for real-time chat applications with streaming responses?
- Yes, the package includes a `realtime` facade for streaming responses, such as chat completions. Use `OpenAI::realtime()->chat()->create()` with the `stream` parameter set to `true` to handle real-time data. This is ideal for chatbots or interactive AI features.
- How do I mock OpenAI API calls in Laravel tests (Pest/PHPUnit)?
- Use the built-in `fake()` method to simulate API responses. Call `OpenAI::fake()` before your test and define fake responses with `OpenAI::shouldReceive('chat')->andReturn($fakeResponse)`. The package integrates with Laravel’s testing tools, including assertion helpers like `assertSent()` to verify API calls.
- Does this package support caching API responses to reduce costs?
- While the package doesn’t enforce caching, you can manually cache responses using Laravel’s `Cache` facade. For example, cache a chat completion response with `Cache::remember('chat_response_key', now()->addHours(1), fn() => OpenAI::chat()->create(...))`. This reduces API calls and costs for repeated requests.
- What happens if OpenAI’s API endpoints change or deprecate?
- The package is actively maintained and tied to the underlying `openai-php/client` library, which adapts to OpenAI’s API changes. Monitor the [changelog](https://github.com/openai-php/client/blob/main/CHANGELOG.md) for updates. If OpenAI deprecates an endpoint, you may need to update the package or extend its functionality manually.
- Is there a way to handle rate limits or retries for high-volume API calls?
- The package uses Guzzle under the hood, which supports retries and middleware for rate limits. Configure retries in `config/openai.php` under the `http_client` section. For high-volume apps, consider queueing requests with Laravel Queues or implementing custom middleware to handle 429 errors.
- Can I use this package with other AI providers (e.g., Mistral, Anthropic) in the future?
- Currently, the package is tightly coupled to OpenAI’s API. If you need multi-provider support, you’d need to extend the `openai-php/client` library or refactor to use a generic HTTP client (e.g., Symfony HTTP Client). The facade layer abstracts OpenAI-specific logic, but switching providers would require significant customization.
- How do I manage API keys securely across dev/staging/prod environments?
- Use Laravel’s `.env` files to store environment-specific API keys (e.g., `OPENAI_API_KEY`). For production, consider using a secrets manager like AWS Secrets Manager or Laravel Forge. The package respects `.env` variables, so keys are automatically loaded from the environment.
- Are there alternatives to this package for Laravel OpenAI integration?
- Yes, alternatives include the official `openai-php/client` (framework-agnostic) or Laravel-specific packages like `spatie/laravel-openai`. However, `openai-php/laravel` stands out for its deep Laravel integration (facades, config, testing tools) and active maintenance. Compare features based on your needs, such as real-time support or caching.