Skip to content

Commit 723f805

Browse files
committed
fix(openai): support gpt-5-nano parameters
1 parent c22aa7a commit 723f805

File tree

2 files changed

+59
-9
lines changed

2 files changed

+59
-9
lines changed

README.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,7 @@ $openai = new OpenAI(
7070
```
7171

7272
Available OpenAI Models:
73+
- `MODEL_GPT_5_NANO`: GPT-5 Nano - Small GPT-5 variant optimized for low latency and cost-sensitive workloads
7374
- `MODEL_GPT_4_5_PREVIEW`: GPT-4.5 Preview - OpenAI's most advanced model with enhanced reasoning, broader knowledge, and improved instruction following
7475
- `MODEL_GPT_4_1`: GPT-4.1 - Advanced large language model with strong reasoning capabilities and improved context handling
7576
- `MODEL_GPT_4O`: GPT-4o - Multimodal model optimized for both text and image processing with faster response times
@@ -245,4 +246,4 @@ We truly ❤️ pull requests! If you wish to help, you can learn more about how
245246

246247
## Copyright and license
247248

248-
The MIT License (MIT) [http://www.opensource.org/licenses/mit-license.php](http://www.opensource.org/licenses/mit-license.php)
249+
The MIT License (MIT) [http://www.opensource.org/licenses/mit-license.php](http://www.opensource.org/licenses/mit-license.php)

src/Agents/Adapters/OpenAI.php

Lines changed: 57 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,11 @@
1111

1212
class OpenAI extends Adapter
1313
{
14+
/**
15+
* GPT-5 Nano - Small GPT-5 variant optimized for low latency and cost-sensitive workloads
16+
*/
17+
public const MODEL_GPT_5_NANO = 'gpt-5-nano';
18+
1419
/**
1520
* GPT-4.5 Preview - OpenAI's most advanced model with enhanced reasoning, broader knowledge, and improved instruction following
1621
*/
@@ -76,6 +81,11 @@ class OpenAI extends Adapter
7681
*/
7782
protected int $timeout;
7883

84+
/**
85+
* @var bool
86+
*/
87+
protected bool $hasWarnedTemperatureOverride = false;
88+
7989
/**
8090
* Create a new OpenAI adapter
8191
*
@@ -164,8 +174,15 @@ public function send(array $messages, ?callable $listener = null): Message
164174
$payload = [
165175
'model' => $this->model,
166176
'messages' => $formattedMessages,
167-
'temperature' => $this->temperature,
168177
];
178+
$temperature = $this->temperature;
179+
if ($this->usesDefaultTemperatureOnly()) {
180+
if ($this->temperature !== 1.0) {
181+
$this->warnTemperatureOverride();
182+
}
183+
$temperature = 1.0;
184+
}
185+
$payload['temperature'] = $temperature;
169186

170187
$schema = $this->getAgent()->getSchema();
171188
if ($schema !== null) {
@@ -187,13 +204,7 @@ public function send(array $messages, ?callable $listener = null): Message
187204
$payload['stream'] = true;
188205
}
189206

190-
// Use 'max_completion_tokens' for o-series models, else 'max_tokens'
191-
$oSeriesModels = [
192-
self::MODEL_O3,
193-
self::MODEL_O3_MINI,
194-
self::MODEL_O4_MINI,
195-
];
196-
if (in_array($this->model, $oSeriesModels)) {
207+
if ($this->usesMaxCompletionTokens()) {
197208
$payload['max_completion_tokens'] = $this->maxTokens;
198209
} else {
199210
$payload['max_tokens'] = $this->maxTokens;
@@ -306,6 +317,7 @@ protected function process(Chunk $chunk, ?callable $listener): string
306317
public function getModels(): array
307318
{
308319
return [
320+
self::MODEL_GPT_5_NANO,
309321
self::MODEL_GPT_4_5_PREVIEW,
310322
self::MODEL_GPT_4_1,
311323
self::MODEL_GPT_4O,
@@ -315,6 +327,43 @@ public function getModels(): array
315327
];
316328
}
317329

330+
/**
331+
* OpenAI expects max_completion_tokens for these models.
332+
*/
333+
protected function usesMaxCompletionTokens(): bool
334+
{
335+
return in_array($this->model, [
336+
self::MODEL_GPT_5_NANO,
337+
self::MODEL_O4_MINI,
338+
self::MODEL_O3,
339+
self::MODEL_O3_MINI,
340+
], true);
341+
}
342+
343+
/**
344+
* Some models only accept the default temperature (1).
345+
*/
346+
protected function usesDefaultTemperatureOnly(): bool
347+
{
348+
return in_array($this->model, [
349+
self::MODEL_GPT_5_NANO,
350+
], true);
351+
}
352+
353+
protected function warnTemperatureOverride(): void
354+
{
355+
if ($this->hasWarnedTemperatureOverride) {
356+
return;
357+
}
358+
359+
$this->hasWarnedTemperatureOverride = true;
360+
error_log(
361+
"OpenAI adapter warning: model '{$this->model}' only supports temperature=1.0. "
362+
."Overriding provided value {$this->temperature}. "
363+
.'Set temperature to 1.0 to remove this warning.'
364+
);
365+
}
366+
318367
/**
319368
* Get current model
320369
*

0 commit comments

Comments
 (0)