Fix: Omit tool_calls from assistant message if empty#115
Open
trsdn wants to merge 1 commit intoBigSweetPotatoStudio:alphafrom
Open
Fix: Omit tool_calls from assistant message if empty#115trsdn wants to merge 1 commit intoBigSweetPotatoStudio:alphafrom
trsdn wants to merge 1 commit intoBigSweetPotatoStudio:alphafrom
Conversation
The OpenAI API expects the `tool_calls` array to have a minimum length of 1 if the property is present in an assistant message. Sending an empty `tool_calls` array results in a 400 error. This commit fixes the issue by modifying the `messages_format` method in `web/src/common/openai.ts`. The method now checks if `content_tool_calls` (the source array from `MyMessage`) is null, undefined, or empty. If it is, the `tool_calls` property is deleted from the formatted assistant message object before it's sent to the API. This ensures that an empty `tool_calls` array is not transmitted. A potential variable shadowing issue within the map function was also addressed by renaming an inner variable. Unit tests have been added in `web/src/common/openai.test.ts` to cover scenarios where `content_tool_calls` is undefined, null, or empty, verifying that `tool_calls` is correctly omitted. A test case for valid `content_tool_calls` is also included.
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
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
The OpenAI API expects the
tool_callsarray to have a minimum length of 1 if the property is present in an assistant message. Sending an emptytool_callsarray results in a 400 error.This commit fixes the issue by modifying the
messages_formatmethod inweb/src/common/openai.ts. The method now checks ifcontent_tool_calls(the source array fromMyMessage) is null, undefined, or empty. If it is, thetool_callsproperty is deleted from the formatted assistant message object before it's sent to the API. This ensures that an emptytool_callsarray is not transmitted.A potential variable shadowing issue within the map function was also addressed by renaming an inner variable.
Unit tests have been added in
web/src/common/openai.test.tsto cover scenarios wherecontent_tool_callsis undefined, null, or empty, verifying thattool_callsis correctly omitted. A test case for validcontent_tool_callsis also included.