Skip to content

fix an error where role 'tool' is not a response to a preceding messa… #1116

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: main
Choose a base branch
from

Conversation

NetchiHub
Copy link

@NetchiHub NetchiHub commented May 4, 2025

Fix for OpenAI API Tool Message Error

Changes

Added modifications to handle tool messages in the add_message and add_messages methods of the Memory class in schema.py.

Specifically, when the number of messages exceeds max_messages, we now remove any leading messages with role="tool".

Background

When using the OpenAI API, the following error was occurring:
ERROR | app.llm:ask_tool:768 - API error: Error code: 400 - {'error': {'message': "Messages with role 'tool' must be a response to a preceding message with 'tool_calls'", 'type': 'invalid_request_error', 'param': None, 'code': 'invalid_request_error'}}

This error occurs when a message with role="tool" is not preceded by a message containing tool_calls.

Solution

When the message history exceeds max_messages, we now remove any leading messages with role="tool". This prevents tool messages from being sent without the proper context.

Impact

This fix only affects message history management when using the OpenAI API. It has no impact on other functionality.

Testing

  • Verified behavior when message count exceeds max_messages
  • Confirmed proper handling of tool messages
  • Verified successful processing of OpenAI API requests

…ge with 'tool_calls' when using the OpenAI API
This was referenced May 4, 2025
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.

2 participants