PHPackages                             aisdk/openai - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. [Utility &amp; Helpers](/categories/utility)
4. /
5. aisdk/openai

ActiveLibrary[Utility &amp; Helpers](/categories/utility)

aisdk/openai
============

Official OpenAI provider for the PHP AI SDK.

v0.1.0-alpha1(yesterday)02↑2900%MITPHPPHP ^8.3

Since Jun 30Pushed yesterdayCompare

[ Source](https://github.com/phpaisdk/openai)[ Packagist](https://packagist.org/packages/aisdk/openai)[ Docs](https://github.com/phpaisdk/openai)[ RSS](/packages/aisdk-openai/feed)WikiDiscussions main Synced today

READMEChangelog (1)Dependencies (6)Versions (2)Used By (0)

aisdk/openai
============

[](#aisdkopenai)

Official OpenAI provider for the PHP AI SDK.

Installation
------------

[](#installation)

```
composer require aisdk/openai
```

Basic Usage
-----------

[](#basic-usage)

```
use AiSdk\Generate;
use AiSdk\OpenAI;

$result = Generate::text()
    ->model(OpenAI::model('gpt-4o'))
    ->instructions('Write short, clear answers.')
    ->prompt('Explain closures in PHP.')
    ->run();

echo $result->text;
```

Default model shorthand:

```
Generate::model(OpenAI::model('gpt-4o'));

$result = Generate::text('Explain closures in PHP.')->run();
```

Configuration
-------------

[](#configuration)

### Environment Variables

[](#environment-variables)

VariableDescriptionDefault`OPENAI_API_KEY`API key for authenticationRequired`OPENAI_BASE_URL`Base URL for API requests`https://api.openai.com/v1``OPENAI_ORGANIZATION`Organization ID headerNone### Programmatic Configuration

[](#programmatic-configuration)

```
$provider = OpenAI::create([
    'apiKey' => 'sk-...',
    'baseUrl' => 'https://api.openai.com/v1',
    'organization' => 'org-...',
    'headers' => ['X-Custom-Header' => 'value'],
]);
```

Supported Capabilities
----------------------

[](#supported-capabilities)

CapabilitySupportText generationNativeStreamingNativeTool callingNativeStructured outputNative (`json_schema`)ReasoningNative (`reasoning_effort`)Text inputNativeImage inputNativeAudio inputNativeFile inputNativeStreaming
---------

[](#streaming)

```
use AiSdk\Generate;
use AiSdk\OpenAI;

$stream = Generate::text('Tell me a story.')
    ->model(OpenAI::model('gpt-4o'))
    ->stream();

foreach ($stream->chunks() as $chunk) {
    echo $chunk;
}

$result = $stream->run();
```

Structured Output
-----------------

[](#structured-output)

```
use AiSdk\Generate;
use AiSdk\OpenAI;
use AiSdk\Schema;

$result = Generate::text()
    ->model(OpenAI::model('gpt-4o'))
    ->prompt('Extract the city and country from: Lahore, Pakistan.')
    ->output(Schema::object(
        name: 'address',
        properties: [
            Schema::string(name: 'city')->required(),
            Schema::string(name: 'country')->required(),
        ],
    ))
    ->run();
```

Tools
-----

[](#tools)

```
use AiSdk\Generate;
use AiSdk\OpenAI;
use AiSdk\Schema;
use AiSdk\Tool;

$weather = Tool::make('weather', 'Get current weather')
    ->input(Schema::string(name: 'city')->required())
    ->run(fn (string $city): string => "Sunny in {$city}");

$result = Generate::text()
    ->model(OpenAI::model('gpt-4o'))
    ->prompt('What is the weather in Lahore?')
    ->tool($weather)
    ->run();
```

Custom Model Registration
-------------------------

[](#custom-model-registration)

Register new OpenAI models without waiting for a package release:

```
use AiSdk\Capability;
use AiSdk\OpenAI;

OpenAI::registerModel('gpt-4.2', capabilities: [
    Capability::TextGeneration,
    Capability::Streaming,
    Capability::ToolCalling,
    Capability::StructuredOutput,
    Capability::TextInput,
    Capability::ImageInput,
]);

$result = Generate::text('Hello')
    ->model(OpenAI::model('gpt-4.2'))
    ->run();
```

Unknown unregistered models are allowed for text generation. For unlisted advanced capabilities, opt in on the model handle:

```
$model = OpenAI::model('gpt-4.2-preview')->assume([
    Capability::ToolCalling,
    Capability::StructuredOutput,
]);
```

The OpenAI API will return a normalized SDK exception if the model or requested feature is rejected by the provider.

Provider-Specific Options
-------------------------

[](#provider-specific-options)

Raw provider options can be passed as an escape hatch:

```
$result = Generate::text('Hello')
    ->model(OpenAI::model('gpt-4o'))
    ->providerOptions('openai', [
        'raw' => ['seed' => 42, 'service_tier' => 'default'],
    ])
    ->run();
```

Testing
-------

[](#testing)

```
composer test
```

Links
-----

[](#links)

- [Core Package](https://github.com/phpaisdk/core)
- [Project Documentation](https://github.com/phpaisdk)

###  Health Score

37

—

LowBetter than 81% of packages

Maintenance100

Actively maintained with recent releases

Popularity3

Limited adoption so far

Community6

Small or concentrated contributor base

Maturity34

Early-stage or recently created project

 Bus Factor1

Top contributor holds 100% of commits — single point of failure

How is this calculated?**Maintenance (25%)** — Last commit recency, latest release date, and issue-to-star ratio. Uses a 2-year decay window.

**Popularity (30%)** — Total and monthly downloads, GitHub stars, and forks. Logarithmic scaling prevents top-heavy scores.

**Community (15%)** — Contributors, dependents, forks, watchers, and maintainers. Measures real ecosystem engagement.

**Maturity (30%)** — Project age, version count, PHP version support, and release stability.

###  Release Activity

Cadence

Unknown

Total

1

Last Release

1d ago

### Community

Maintainers

![](https://www.gravatar.com/avatar/ef29d1962da41e7526023f215cf844494e209b2fd163742084cb919a3b508021?d=identicon)[aisdk](/maintainers/aisdk)

---

Top Contributors

[![RanaMoizHaider](https://avatars.githubusercontent.com/u/58527494?v=4)](https://github.com/RanaMoizHaider "RanaMoizHaider (2 commits)")

---

Tags

aiopenaillmaisdk

###  Code Quality

TestsPest

Static AnalysisPHPStan, Rector

Code StyleLaravel Pint

Type Coverage Yes

### Embed Badge

![Health badge](/badges/aisdk-openai/health.svg)

```
[![Health](https://phpackages.com/badges/aisdk-openai/health.svg)](https://phpackages.com/packages/aisdk-openai)
```

###  Alternatives

[cognesy/instructor-php

The complete AI toolkit for PHP: unified LLM API, structured outputs, agents, and coding agent control

318117.1k1](/packages/cognesy-instructor-php)[soukicz/llm

LLM client with support for cache, tools and async requests

4411.4k](/packages/soukicz-llm)

PHPackages © 2026

[Directory](/)[Categories](/categories)[Trending](/trending)[Changelog](/changelog)[Analyze](/analyze)
