OpenAI
Configuration
'openai' => [
'url' => env('OPENAI_URL', 'https://api.openai.com/v1'),
'api_key' => env('OPENAI_API_KEY', ''),
'organization' => env('OPENAI_ORGANIZATION', null),
]Provider-specific options
Strict Tool Schemas
Prism supports OpenAI's function calling with Structured Outputs via provider-specific meta.
Tool::as('search')
->for('Searching the web')
->withStringParameter('query', 'the detailed search query')
->using(fn (): string => '[Search results]')
->withProviderOptions([
'strict' => true,
]); Strict Structured Output Schemas
$response = Prism::structured()
->withProviderOptions([
'schema' => [
'strict' => true
]
]) Metadata
$response = Prism::structured()
->withProviderOptions([
'meta' => [
'project_id' => 23
]
]) Previous Responses
Prism supports OpenAI's conversation state with the previous_response_id parameter.
$response = Prism::structured()
->withProviderOptions([
'previous_response_id' => 'response_id'
]) Truncation
$response = Prism::structured()
->withProviderOptions([
'truncation' => 'auto'
]) Caching
Automatic caching does not currently work with JsonMode. Please ensure you use StructuredMode if you wish to utilise automatic caching.
Code interpreter
You can use the OpenAI code interpreter as follows:
use Prism\Prism\Prism;
use Prism\Prism\ValueObjects\ProviderTool;
Prism::text()
->using('openai', 'gpt-4.1')
->withPrompt('Solve the equation 3x + 10 = 14.')
->withProviderTools([new ProviderTool(type: 'code_interpreter', options: ['container' => ['type' => 'auto']])])
->asText();Additional Message Attributes
Adding optional parameters to a UserMessage like the name field can be done through the additionalAttributes parameter.
Prism::text()
->using('openai', 'gpt-4.1')
->withMessages([
new UserMessage('Who are you?', additionalAttributes: ['name' => 'TJ']),
])
->asText()Image Generation
OpenAI provides powerful image generation capabilities through multiple models. Prism supports all of OpenAI's image generation models with their full feature sets.
Supported Models
| Model | Description |
|---|---|
dall-e-3 | Latest DALL-E model |
dall-e-2 | Previous generation |
gpt-image-1 | GPT-based image model |
Basic Usage
$response = Prism::image()
->using('openai', 'dall-e-3')
->withPrompt('A serene mountain landscape at sunset')
->generate();
$image = $response->firstImage();
echo $image->url; // Generated image URLDALL-E 3 Options
DALL-E 3 is the most advanced model with the highest quality output:
$response = Prism::image()
->using('openai', 'dall-e-3')
->withPrompt('A futuristic cityscape with flying cars')
->withProviderOptions([
'size' => '1792x1024', // 1024x1024, 1024x1792, 1792x1024
'quality' => 'hd', // standard, hd
'style' => 'vivid', // vivid, natural
])
->generate();
// DALL-E 3 automatically revises prompts for better results
if ($response->firstImage()->hasRevisedPrompt()) {
echo "Revised prompt: " . $response->firstImage()->revisedPrompt;
}DALL-E 2 Options
DALL-E 2 supports generating multiple images and is more cost-effective:
$response = Prism::image()
->using('openai', 'dall-e-2')
->withPrompt('Abstract geometric patterns')
->withProviderOptions([
'n' => 4, // Number of images (1-10)
'size' => '1024x1024', // 256x256, 512x512, 1024x1024
'response_format' => 'url', // url only
'user' => 'user-123', // Optional user identifier
])
->generate();
// Process multiple images
foreach ($response->images as $image) {
echo "Image: {$image->url}\n";
}GPT-Image-1 Options
GPT-Image-1 offers advanced features including image editing and format control:
$response = Prism::image()
->using('openai', 'gpt-image-1')
->withPrompt('A detailed architectural rendering of a modern house')
->withProviderOptions([
'size' => '1536x1024', // Various sizes supported
'quality' => 'high', // standard, high
'output_format' => 'webp', // png, webp, jpeg
'output_compression' => 85, // Compression level (0-100)
'background' => 'transparent', // transparent, white, black
'moderation' => true, // Enable content moderation
])
->generate();Image Editing with GPT-Image-1
GPT-Image-1 supports sophisticated image editing operations:
// Load your source image and mask
$originalImage = base64_encode(file_get_contents('/path/to/photo.jpg'));
$maskImage = base64_encode(file_get_contents('/path/to/mask.png'));
$response = Prism::image()
->using('openai', 'gpt-image-1')
->withPrompt('Replace the sky with a dramatic sunset')
->withProviderOptions([
'image' => $originalImage, // Base64 encoded original image
'mask' => $maskImage, // Base64 encoded mask (optional)
'size' => '1024x1024',
'output_format' => 'png',
'quality' => 'high',
])
->generate();Response Format
Generated images are returned as URLs:
$response = Prism::image()
->using('openai', 'dall-e-3')
->withPrompt('Digital artwork')
->generate();
$image = $response->firstImage();
if ($image->hasUrl()) {
echo "<img src='{$image->url}' alt='Generated image'>";
}