Skip to main content

Documentation Index

Fetch the complete documentation index at: https://mintlify.com/phpaisdk/openai/llms.txt

Use this file to discover all available pages before exploring further.

This guide walks you from a blank project to a working OpenAI integration in five minutes. You will install the package, configure authentication, generate your first text response, stream tokens as they arrive, and produce an image — all using the clean, fluent API provided by aisdk/core and aisdk/openai.
1

Install the package

Require aisdk/openai via Composer. It will pull in aisdk/core ^0.2 and any other dependencies automatically:
composer require aisdk/openai
2

Set your API key

Export your OpenAI API key as an environment variable before running any scripts:
export OPENAI_API_KEY=sk-...
The provider reads OPENAI_API_KEY automatically. You can also pass it programmatically via OpenAI::create(['apiKey' => 'sk-...']) if you prefer explicit configuration.
3

Generate text

Call Generate::text(), attach a model handle, add instructions and a prompt, then call run(). The response is synchronous and the generated content is available on $result->text:
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;
If you want to set a default model once and omit the ->model() call on every request, use the Generate::model() shorthand:
use AiSdk\Generate;
use AiSdk\OpenAI;

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

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

echo $result->text;
4

Stream tokens

Call ->stream() instead of ->run() to receive a stream object. Iterate over $stream->chunks() to print each token as it arrives, then call ->run() on the same stream to obtain the fully assembled result:
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();
5

Generate an image

Switch to the Generate::image() builder and pass an OpenAI::image() handle. Specify your prompt and desired dimensions, then call ->run(). Save the result directly to disk with $result->output->save():
use AiSdk\Generate;
use AiSdk\OpenAI;

$result = Generate::image()
    ->model(OpenAI::image('gpt-image-1'))
    ->prompt('A clean app icon for a PHP AI SDK')
    ->size('1024x1024')
    ->run();

$result->output->save(__DIR__.'/icon.png');

Next Steps

Tool Calling

Bind PHP callables to tools and let the model invoke them automatically.

Structured Output

Use json_schema mode to receive typed, validated objects from the model.

Configuration

Customise base URLs, organisation headers, and per-request provider options.

Streaming

Build real-time UIs by processing tokens as they stream from the API.

Build docs developers (and LLMs) love