Fix: Omit tool_calls from assistant message if empty #115
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_calls
array to have a minimum length of 1 if the property is present in an assistant message. Sending an emptytool_calls
array results in a 400 error.This commit fixes the issue by modifying the
messages_format
method 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_calls
property is deleted from the formatted assistant message object before it's sent to the API. This ensures that an emptytool_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 wherecontent_tool_calls
is undefined, null, or empty, verifying thattool_calls
is correctly omitted. A test case for validcontent_tool_calls
is also included.