Standardize withProviderOptions across the provider option API

Merged
PU
5mo ago
+105-131

Right now the provider-option API is inconsistent. Embeddings and transcription use providerOptions(...), tools use a provider-keyed withProviderOptions($provider, $options), and #717 just landed withProviderOptions(array|Closure) on files. Three shapes, two names, for the same concept.

This unifies the setter on withProviderOptions(array|Closure $options) everywhere, matching the file API from #717. The providerOptions(Lab|string $provider) getter (the HasProviderOptions contract that agents implement) is unchanged.

// before
Embeddings::for(['Hi'])->providerOptions(['input_type' => 'search_query']);
(new WebSearch)->withProviderOptions('openai', ['search_context_size' => 'high']);

// after
Embeddings::for(['Hi'])->withProviderOptions(['input_type' => 'search_query']);
(new WebSearch)->withProviderOptions(['search_context_size' => 'high']);

Image, audio, and reranking also get withProviderOptions(), but only on the builder for now. The option is captured but not yet forwarded to the gateways (that plumbing is a follow-up), so on those three it's currently a no-op.

[!WARNING]
Breaking changes. If you use the old API, you need to update calls.

1. Embeddings and transcription

The setter was renamed. Arguments are identical, so it is a find-and-replace:

- Embeddings::for($inputs)->providerOptions(['input_type' => 'search_query']);
+ Embeddings::for($inputs)->withProviderOptions(['input_type' => 'search_query']);

- Transcription::of($audio)->providerOptions(['prompt' => 'Laravel']);
+ Transcription::of($audio)->withProviderOptions(['prompt' => 'Laravel']);

The closure form still works the same way (it receives the resolved provider, handy when a request can fail over across providers):

Embeddings::for($inputs)->withProviderOptions(fn (Provider $provider) => match ($provider->driver()) {
    'cohere' => ['input_type' => 'search_query'],
    'openai' => ['encoding_format' => 'base64'],
    default  => [],
});

The Str macro keeps the same parameter, just feeds the new method:

str('Hello')->toEmbeddings(providerOptions: ['input_type' => 'search_query']);

2. Tools

The provider argument is gone. A flat array now applies to every provider:

- (new WebSearch)->withProviderOptions('openai', ['search_context_size' => 'high']);
+ (new WebSearch)->withProviderOptions(['search_context_size' => 'high']);

If you relied on options being scoped to a single provider (for example an agent that fails over across providers, or a tool reused on more than one provider), pass a closure instead. It receives the resolved provider, so the options only apply where you want them:

(new WebSearch)->withProviderOptions(fn (Lab|string $provider) => $provider === Lab::OpenAI
    ? ['search_context_size' => 'high']
    : []);

Stacking multiple providers, which used to be repeated keyed calls, becomes one closure:

// before
(new WebSearch)
    ->withProviderOptions('openai', ['search_context_size' => 'high'])
    ->withProviderOptions('anthropic', ['blocked_domains' => ['spam.com']]);

// after
(new WebSearch)->withProviderOptions(fn (Lab|string $provider) => match ($provider) {
    Lab::OpenAI    => ['search_context_size' => 'high'],
    Lab::Anthropic => ['blocked_domains' => ['spam.com']],
    default        => [],
});

Not affected

Agents that declare options through the HasProviderOptions contract (the providerOptions(Lab|string $provider): array method) need no change:

class SalesCoach implements Agent, HasProviderOptions
{
    use Promptable;

    public function providerOptions(Lab|string $provider): array
    {
        return match ($provider) {
            Lab::OpenAI    => ['reasoning' => ['effort' => 'low']],
            Lab::Anthropic => ['thinking' => ['budget_tokens' => 1024]],
            default        => [],
        };
    }
}

Tests updated accordingly.