Skip to content

Fix: Omit tool_calls from assistant message if empty #115

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

Open
wants to merge 1 commit into
base: alpha
Choose a base branch
from

Conversation

trsdn
Copy link

@trsdn trsdn commented May 23, 2025

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.

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.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant