Skip to content

OpenAI

Configuration

php
'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.

php
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

php
$response = Prism::structured()
    ->withProviderOptions([ 
        'schema' => [ 
            'strict' => true
        ] 
    ]) 

Metadata

php
$response = Prism::structured()
    ->withProviderOptions([ 
        'meta' => [ 
            'project_id' => 23
        ] 
    ]) 

Previous Responses

Prism supports OpenAI's conversation state with the previous_response_id parameter.

php
$response = Prism::structured()
    ->withProviderOptions([ 
        'previous_response_id' => 'response_id'
    ]) 

Truncation

php
$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:

php
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();

Released under the MIT License.