Skip to content

Commit f1fe17a

Browse files
igorcostaAutohand Evolve
andcommitted
Recover unsupported OpenAI token parameters with one bounded resend
Co-authored-by: Autohand Evolve <code-noreply@autohand.ai>
2 parents e59017b + 1e2d99d commit f1fe17a

2 files changed

Lines changed: 88 additions & 28 deletions

File tree

‎src/providers/OpenAIProvider.ts‎

Lines changed: 41 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -289,41 +289,54 @@ export class OpenAIProvider implements LLMProvider {
289289
let response: Response;
290290
const headers = await this.buildAuthHeaders();
291291

292-
try {
293-
response = await fetch(`${this.baseUrl}/chat/completions`, {
294-
method: 'POST',
295-
headers: {
296-
'Content-Type': 'application/json',
297-
...headers
298-
},
299-
body: JSON.stringify(body),
300-
signal: request.signal
301-
});
302-
} catch (error) {
303-
const err = error as Error;
292+
for (let swappedTokenParameter = false; ; swappedTokenParameter = true) {
293+
try {
294+
response = await fetch(`${this.baseUrl}/chat/completions`, {
295+
method: 'POST',
296+
headers: {
297+
'Content-Type': 'application/json',
298+
...headers
299+
},
300+
body: JSON.stringify(body),
301+
signal: request.signal
302+
});
303+
} catch (error) {
304+
const err = error as Error;
304305

305-
// User cancelled
306-
if (err.name === 'AbortError' && request.signal?.aborted) {
307-
throw new ApiError('Request cancelled.', 'cancelled', 0, false);
308-
}
306+
// User cancelled
307+
if (err.name === 'AbortError' && request.signal?.aborted) {
308+
throw new ApiError('Request cancelled.', 'cancelled', 0, false);
309+
}
310+
311+
// Timeout
312+
if (err.name === 'AbortError') {
313+
throw new ApiError(
314+
'The request timed out. The OpenAI service may be experiencing high load.',
315+
'timeout', 0, true,
316+
);
317+
}
309318

310-
// Timeout
311-
if (err.name === 'AbortError') {
319+
// Network error
312320
throw new ApiError(
313-
'The request timed out. The OpenAI service may be experiencing high load.',
314-
'timeout', 0, true,
321+
`Unable to connect to ${this.baseUrl}. Please check the URL and your internet connection.`,
322+
'network_error', 0, true,
315323
);
316324
}
317325

318-
// Network error
319-
throw new ApiError(
320-
`Unable to connect to ${this.baseUrl}. Please check the URL and your internet connection.`,
321-
'network_error', 0, true,
322-
);
323-
}
326+
if (response.ok) {
327+
break;
328+
}
324329

325-
if (!response.ok) {
326-
throw await this.buildApiError(response);
330+
const apiError = await this.buildApiError(response);
331+
// Models we don't recognize (fine-tunes, new families) may still reject
332+
// max_tokens. Resend once with max_completion_tokens instead of failing.
333+
if (!swappedTokenParameter && 'max_tokens' in body
334+
&& /unsupported parameter:\s*'max_tokens'/i.test(apiError.rawDetail ?? '')) {
335+
body.max_completion_tokens = body.max_tokens;
336+
delete body.max_tokens;
337+
continue;
338+
}
339+
throw apiError;
327340
}
328341

329342
const data = await response.json() as OpenAIChatResponse;

‎tests/providers/OpenAIProvider.test.ts‎

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1557,6 +1557,53 @@ describe('OpenAIProvider', () => {
15571557
expect(sentBody.max_tokens).toBeUndefined();
15581558
});
15591559

1560+
describe('unsupported max_tokens fallback', () => {
1561+
const unsupportedMaxTokens = () => new Response(JSON.stringify({
1562+
error: {
1563+
message: "Unsupported parameter: 'max_tokens' is not supported with this model. Use 'max_completion_tokens' instead.",
1564+
type: 'invalid_request_error',
1565+
param: 'max_tokens',
1566+
code: 'unsupported_parameter',
1567+
},
1568+
}), { status: 400, headers: { 'Content-Type': 'application/json' } });
1569+
1570+
it('resends once with max_completion_tokens when an unrecognized model rejects max_tokens', async () => {
1571+
const fetchSpy = vi.spyOn(globalThis, 'fetch')
1572+
.mockResolvedValueOnce(unsupportedMaxTokens())
1573+
.mockResolvedValueOnce(new Response(JSON.stringify({
1574+
id: 'resp-ft', created: 1,
1575+
choices: [{ message: { role: 'assistant', content: 'OK' }, finish_reason: 'stop' }],
1576+
}), { status: 200, headers: { 'Content-Type': 'application/json' } }));
1577+
1578+
const result = await provider.complete({
1579+
model: 'ft:gpt-6.1-sol:acme::abc123',
1580+
maxTokens: 4096,
1581+
messages: [{ role: 'user', content: 'hi' }],
1582+
});
1583+
1584+
expect(result.content).toBe('OK');
1585+
expect(fetchSpy).toHaveBeenCalledTimes(2);
1586+
const firstBody = JSON.parse(fetchSpy.mock.calls[0][1]?.body as string);
1587+
const retryBody = JSON.parse(fetchSpy.mock.calls[1][1]?.body as string);
1588+
expect(firstBody.max_tokens).toBe(4096);
1589+
expect(retryBody.max_completion_tokens).toBe(4096);
1590+
expect(retryBody).not.toHaveProperty('max_tokens');
1591+
});
1592+
1593+
it('does not retry other 400 errors', async () => {
1594+
const fetchSpy = vi.spyOn(globalThis, 'fetch').mockResolvedValueOnce(new Response(JSON.stringify({
1595+
error: { message: "Unsupported parameter: 'temperature' is not supported with this model." },
1596+
}), { status: 400, headers: { 'Content-Type': 'application/json' } }));
1597+
1598+
await expect(provider.complete({
1599+
model: 'ft:gpt-6.1-sol:acme::abc123',
1600+
maxTokens: 4096,
1601+
messages: [{ role: 'user', content: 'hi' }],
1602+
})).rejects.toBeInstanceOf(ApiError);
1603+
expect(fetchSpy).toHaveBeenCalledTimes(1);
1604+
});
1605+
});
1606+
15601607
describe('Chat Completions finish reason normalization', () => {
15611608
const mockJson = (body: unknown) => vi.spyOn(globalThis, 'fetch').mockResolvedValueOnce(
15621609
new Response(JSON.stringify(body), { status: 200, headers: { 'Content-Type': 'application/json' } }),

0 commit comments

Comments
 (0)