-
-
Notifications
You must be signed in to change notification settings - Fork 199
fix(ai): return Chat Completions refusals and retry reasoning tool turns on gateways #1819
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
chhoumann
merged 2 commits into
master
from
fix/chat-completions-refusal-and-proxy-tool-retry
Sep 26, 2026
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,181 @@ | ||
| import { beforeEach, describe, expect, it } from "vitest"; | ||
| import type { AIProvider, Model } from "./Provider"; | ||
| import type { NormalizedChatRequest } from "./tools/NormalizedTools"; | ||
|
|
||
| import { storeState, mocks, makeApp } from "../../tests/helpers/ai/requestHarness"; | ||
|
|
||
| const { requestUrlMock, noticeMock } = mocks; | ||
|
|
||
| const { chatRequest } = await import("./OpenAIRequest"); | ||
|
|
||
| // A gateway serving OpenAI models over Chat Completions (OpenAI's own endpoint | ||
| // uses the Responses API, where this rejection doesn't happen). | ||
| const gatewayProvider: AIProvider = { | ||
| name: "Gateway", | ||
| endpoint: "https://openrouter.ai/api/v1", | ||
| kind: "openai", | ||
| apiKey: "sk", | ||
| models: [], | ||
| modelSource: "modelsDev", | ||
| }; | ||
|
|
||
| const gpt6: Model = { | ||
| name: "gpt-6-sol", | ||
| maxTokens: 1_050_000, | ||
| maxOutputTokens: 128_000, | ||
| supportsTemperature: false, | ||
| }; | ||
|
|
||
| // Exact live error for gpt-6-sol with function tools on /v1/chat/completions | ||
| // (2026-09-26); gpt-5.6-* and the other gpt-6-* models return the same text. | ||
| function toolsWhileReasoningFailure(model = "gpt-6-sol") { | ||
| return { | ||
| status: 400, | ||
| json: { | ||
| error: { | ||
| message: `Function tools with reasoning_effort are not supported for ${model} in /v1/chat/completions. To use function tools, use /v1/responses or set reasoning_effort to 'none'.`, | ||
| type: "invalid_request_error", | ||
| param: null, | ||
| code: null, | ||
| }, | ||
| }, | ||
| }; | ||
| } | ||
|
|
||
| function toolCallSuccess() { | ||
| return { | ||
| status: 200, | ||
| json: Promise.resolve({ | ||
| id: "1", | ||
| model: "gpt-6-sol", | ||
| choices: [ | ||
| { | ||
| finish_reason: "tool_calls", | ||
| index: 0, | ||
| message: { | ||
| role: "assistant", | ||
| content: null, | ||
| tool_calls: [ | ||
| { | ||
| id: "call_1", | ||
| type: "function", | ||
| function: { name: "get_weather", arguments: '{"city":"Paris"}' }, | ||
| }, | ||
| ], | ||
| }, | ||
| }, | ||
| ], | ||
| usage: { prompt_tokens: 1, completion_tokens: 1, total_tokens: 2 }, | ||
| created: 0, | ||
| }), | ||
| }; | ||
| } | ||
|
|
||
| function toolRequest( | ||
| modelParams: Record<string, unknown> = {}, | ||
| ): NormalizedChatRequest { | ||
| return { | ||
| messages: [{ role: "user", content: "Weather in Paris?" }], | ||
| modelParams, | ||
| tools: [ | ||
| { | ||
| name: "get_weather", | ||
| description: "Get weather", | ||
| parameters: { | ||
| type: "object", | ||
| properties: { city: { type: "string" } }, | ||
| required: ["city"], | ||
| }, | ||
| }, | ||
| ], | ||
| toolChoice: "auto", | ||
| }; | ||
| } | ||
|
|
||
| function sentBody(callIndex: number): Record<string, unknown> { | ||
| return JSON.parse(requestUrlMock.mock.calls[callIndex][0].body as string); | ||
| } | ||
|
|
||
| beforeEach(() => { | ||
| requestUrlMock.mockReset(); | ||
| noticeMock.mockReset(); | ||
| storeState.disableOnlineFeatures = false; | ||
| }); | ||
|
|
||
| describe("function tools on reasoning models behind a Chat Completions gateway", () => { | ||
| it("retries once with reasoning_effort 'none' when the model rejects tools while reasoning", async () => { | ||
| requestUrlMock | ||
| .mockReturnValueOnce(Promise.resolve(toolsWhileReasoningFailure())) | ||
| .mockReturnValueOnce(Promise.resolve(toolCallSuccess())); | ||
|
|
||
| const res = await chatRequest(makeApp(), "sk", gpt6, gatewayProvider, toolRequest()); | ||
|
|
||
| expect(res.toolCalls?.map((call) => call.name)).toEqual(["get_weather"]); | ||
| expect(requestUrlMock).toHaveBeenCalledTimes(2); | ||
| expect(sentBody(0).reasoning_effort).toBeUndefined(); | ||
| expect(sentBody(1).reasoning_effort).toBe("none"); | ||
| expect(sentBody(1).tools).toEqual(sentBody(0).tools); | ||
| }); | ||
|
|
||
| it("keeps a reasoning effort the caller chose and surfaces the error", async () => { | ||
| requestUrlMock.mockReturnValueOnce( | ||
| Promise.resolve(toolsWhileReasoningFailure()), | ||
| ); | ||
|
|
||
| await expect( | ||
| chatRequest( | ||
| makeApp(), | ||
| "sk", | ||
| gpt6, | ||
| gatewayProvider, | ||
| toolRequest({ reasoning_effort: "high" }), | ||
| ), | ||
| ).rejects.toThrow(/reasoning_effort/); | ||
| expect(requestUrlMock).toHaveBeenCalledTimes(1); | ||
| }); | ||
|
|
||
| it("does not retry other 400s", async () => { | ||
| requestUrlMock.mockReturnValueOnce( | ||
| Promise.resolve({ | ||
| status: 400, | ||
| json: { | ||
| error: { | ||
| message: "Invalid schema for function 'get_weather'.", | ||
| type: "invalid_request_error", | ||
| }, | ||
| }, | ||
| }), | ||
| ); | ||
|
|
||
| await expect( | ||
| chatRequest(makeApp(), "sk", gpt6, gatewayProvider, toolRequest()), | ||
| ).rejects.toThrow(/Invalid schema/); | ||
| expect(requestUrlMock).toHaveBeenCalledTimes(1); | ||
| }); | ||
|
|
||
| it("retries at the gateway's Chat Completions URL", async () => { | ||
| requestUrlMock | ||
| .mockReturnValueOnce(Promise.resolve(toolsWhileReasoningFailure())) | ||
| .mockReturnValueOnce(Promise.resolve(toolCallSuccess())); | ||
|
|
||
| await chatRequest(makeApp(), "sk", gpt6, gatewayProvider, toolRequest()); | ||
|
|
||
| expect(requestUrlMock.mock.calls.map((call) => call[0].url)).toEqual([ | ||
| "https://openrouter.ai/api/v1/chat/completions", | ||
| "https://openrouter.ai/api/v1/chat/completions", | ||
| ]); | ||
| }); | ||
|
|
||
| it("does not add reasoning_effort to a request without tools", async () => { | ||
| requestUrlMock.mockReturnValueOnce( | ||
| Promise.resolve(toolsWhileReasoningFailure()), | ||
| ); | ||
|
|
||
| await expect( | ||
| chatRequest(makeApp(), "sk", gpt6, gatewayProvider, { | ||
| messages: [{ role: "user", content: "hi" }], | ||
| }), | ||
| ).rejects.toThrow(); | ||
| expect(requestUrlMock).toHaveBeenCalledTimes(1); | ||
| }); | ||
| }); | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.