-
Notifications
You must be signed in to change notification settings - Fork 2.7k
fix(openai): send native tool definitions by default #10314
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
+85
−1
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,77 @@ | ||
| import OpenAI from "openai" | ||
|
|
||
| import { OpenAiHandler } from "../openai" | ||
|
|
||
| describe("OpenAiHandler native tools", () => { | ||
| it("includes tools in request when custom model info lacks supportsNativeTools (regression test)", async () => { | ||
| const mockCreate = vi.fn().mockImplementationOnce(() => ({ | ||
| [Symbol.asyncIterator]: async function* () { | ||
| yield { | ||
| choices: [{ delta: { content: "Test response" } }], | ||
| } | ||
| }, | ||
| })) | ||
|
|
||
| // Set openAiCustomModelInfo WITHOUT supportsNativeTools to simulate | ||
| // a user-provided custom model info that doesn't specify native tool support. | ||
| // The getModel() fix should merge NATIVE_TOOL_DEFAULTS to ensure | ||
| // supportsNativeTools defaults to true. | ||
| const handler = new OpenAiHandler({ | ||
| openAiApiKey: "test-key", | ||
| openAiBaseUrl: "https://example.com/v1", | ||
| openAiModelId: "test-model", | ||
| openAiCustomModelInfo: { | ||
| maxTokens: 4096, | ||
| contextWindow: 128000, | ||
| }, | ||
| } as unknown as import("../../../shared/api").ApiHandlerOptions) | ||
|
|
||
| // Patch the OpenAI client call | ||
| const mockClient = { | ||
| chat: { | ||
| completions: { | ||
| create: mockCreate, | ||
| }, | ||
| }, | ||
| } as unknown as OpenAI | ||
| ;(handler as unknown as { client: OpenAI }).client = mockClient | ||
|
|
||
| const tools: OpenAI.Chat.ChatCompletionTool[] = [ | ||
| { | ||
| type: "function", | ||
| function: { | ||
| name: "test_tool", | ||
| description: "test", | ||
| parameters: { type: "object", properties: {} }, | ||
| }, | ||
| }, | ||
| ] | ||
|
|
||
| // Mimic the behavior in Task.attemptApiRequest() where tools are only | ||
| // included when modelInfo.supportsNativeTools is true. This is the | ||
| // actual regression path being tested - without the getModel() fix, | ||
| // supportsNativeTools would be undefined and tools wouldn't be passed. | ||
| const modelInfo = handler.getModel().info | ||
| const supportsNativeTools = modelInfo.supportsNativeTools ?? false | ||
|
|
||
| const stream = handler.createMessage("system", [], { | ||
| taskId: "test-task-id", | ||
| ...(supportsNativeTools && { tools }), | ||
| ...(supportsNativeTools && { toolProtocol: "native" as const }), | ||
| }) | ||
| await stream.next() | ||
|
|
||
| expect(mockCreate).toHaveBeenCalledWith( | ||
| expect.objectContaining({ | ||
| tools: expect.arrayContaining([ | ||
| expect.objectContaining({ | ||
| type: "function", | ||
| function: expect.objectContaining({ name: "test_tool" }), | ||
| }), | ||
| ]), | ||
| parallel_tool_calls: false, | ||
| }), | ||
| expect.anything(), | ||
| ) | ||
| }) | ||
| }) | ||
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This test will pass even without the
getModel()change becauseOpenAiHandler.createMessage()always forwardstoolswhenmetadata.toolsis present. The real regression fixed in this PR is earlier inTask.attemptApiRequest(), wheremetadata.toolsis omitted whenmodelInfo.supportsNativeToolsis missing fromopenAiCustomModelInfo. Consider settingopenAiCustomModelInfoin this test withoutsupportsNativeToolsand only providingmetadata.toolswhenhandler.getModel().info.supportsNativeToolsis true, so the test fails without this PR.Fix it with Roo Code or mention @roomote and request a fix.