fix(agent): filter malformed tool calls before persistence and replay#5071
Open
giwaov wants to merge 1 commit into
Open
fix(agent): filter malformed tool calls before persistence and replay#5071giwaov wants to merge 1 commit into
giwaov wants to merge 1 commit into
Conversation
Drop tool calls with empty function names or empty arguments before persisting assistant messages to the session database and before replaying stored history to the LLM provider. Previously, malformed tool calls (e.g. empty name/arguments from edge-case provider responses) could be written to SQLite and then replayed on every subsequent request in that session, causing repeated HTTP 400 errors from strict OpenAI-compatible providers. The fix adds validation in two places: - _flush_messages_to_session_db: skip tool calls with falsy name or arguments before writing to the database. - _sanitize_api_messages: strip malformed tool calls from assistant messages before every API call, protecting sessions that were already poisoned. Closes NousResearch#4662
6 tasks
Collaborator
1 similar comment
Collaborator
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.
What changed and why
Hermes could persist malformed assistant tool calls (empty \unction.name, empty \unction.arguments) into the SQLite session database. Once persisted, these invalid entries were replayed on every subsequent request in that session, causing repeated HTTP 400 errors from strict OpenAI-compatible providers:
\
HTTP 400: InternalError.Algo.InvalidParameter: The function.arguments parameter of the code model must be in JSON format.
\\
Fix
Added validation in two places:
**_flush_messages_to_session_db**: Skip tool calls with falsy
ame\ or \�rguments\ before writing to the database. Handles both the \ ool_calls\ attribute path (raw API response objects) and the dict path (pre-normalized messages). If all tool calls in a message are invalid, \ ool_calls_data\ is set to \None\ so the row is written cleanly.
**_sanitize_api_messages**: Strip malformed tool calls from assistant messages before every API call. This protects sessions that were already poisoned by older versions. Handles both flat dict format (
ame/\�rguments\ at top level) and nested format (\unction.name/\unction.arguments).
How to test
Platform tested
Windows 11
Related issue
Closes #4662