fix an error where role 'tool' is not a response to a preceding messa… #1116
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.
Fix for OpenAI API Tool Message Error
Changes
Added modifications to handle tool messages in the
add_message
andadd_messages
methods of theMemory
class inschema.py
.Specifically, when the number of messages exceeds
max_messages
, we now remove any leading messages withrole="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 containingtool_calls
.Solution
When the message history exceeds
max_messages
, we now remove any leading messages withrole="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
max_messages