fix: validate tool calls before persisting to prevent session poisoning#4724
Closed
Bartok9 wants to merge 1 commit into
Closed
fix: validate tool calls before persisting to prevent session poisoning#4724Bartok9 wants to merge 1 commit into
Bartok9 wants to merge 1 commit into
Conversation
Malformed tool calls (empty function name, invalid JSON arguments) can
poison a session and cause repeated 400 errors when replayed to strict
providers. This fix adds two layers of defense:
1. _is_valid_tool_call(): Validates tool calls before persisting to
session history in _build_assistant_message(). Invalid tool calls are
logged and skipped rather than persisted.
2. _sanitize_tool_calls_for_strict_api(): Enhanced to also filter
malformed tool calls when preparing messages for API calls. This
provides a second layer of defense for existing poisoned sessions.
Validation rules:
- Function name must be a non-empty string
- Arguments must be valid JSON object (dict), not array or primitive
- Empty arguments string is invalid (should be "{}" at minimum)
- None arguments are allowed (some providers omit for empty args)
Includes comprehensive test coverage for both validation functions.
Fixes NousResearch#4662
14cfb9c to
b99a3ed
Compare
Contributor
Author
|
Superseded by #14407 — rebased onto current main with full coverage. |
Collaborator
|
Superseded by #14407 (rebased version). |
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.
Problem
Malformed tool calls can poison a session and cause repeated 400 errors when replayed to strict providers. Once invalid tool calls are persisted in session history, every subsequent request fails because the same malformed history is replayed.
Observed failure mode:
This happens when persisted tool calls contain:
function.namefunction.argumentsstringSolution
Adds two layers of defense:
1.
_is_valid_tool_call()- Validation at persist timeValidates tool calls before persisting to session history in
_build_assistant_message(). Invalid tool calls are logged and skipped rather than persisted:2.
_sanitize_tool_calls_for_strict_api()- Enhanced API-level filteringProvides a second layer of defense by filtering malformed tool calls when preparing messages for API calls. This helps with existing poisoned sessions.
Validation rules
"{}"at minimum)Testing
Added comprehensive test coverage in
tests/test_tool_call_validation.py:Fixes
Fixes #4662