fix: drop orphaned tool messages in message validation#267
Merged
Conversation
The OpenAI API rejects tool-role messages that lack a preceding assistant message with tool_calls. Previously the validation kept these orphaned messages with only a debug log, causing 400 errors: "messages with role 'tool' must be a response to a preceding message with 'tool_calls'" Now properly drops: - Tool messages with missing tool_call_id - Tool messages with no preceding assistant tool_calls turn - Tool messages whose tool_call_id doesn't match any preceding tool_calls Made-with: Cursor
Tests cover: - Drop tool messages with missing tool_call_id - Drop tool messages without preceding assistant tool_calls - Drop tool messages with unmatched tool_call_id - Keep valid paired tool messages - Handle interleaved user messages with reorder + validate - Multiple orphaned messages all dropped Made-with: Cursor
Collaborator
Author
|
@codex review it |
|
To use Codex here, create a Codex account and connect to github. |
Extract shared `drop_orphaned_tool_messages()` utility that sanitises Message sequences before provider-specific conversion. Apply it in: - AnthropicProvider._convert_messages - GeminiProvider._convert_messages_for_tools - OllamaProvider._convert_messages - OpenAICompatibleProvider._convert_messages (defense-in-depth) The same three orphan cases (no tool_call_id, no preceding assistant with tool_calls, unmatched tool_call_id) that caused OpenAI 400 errors also trigger API rejections on Anthropic, Gemini, and Ollama. Tests cover the shared utility (9 cases) and per-provider integration (2 cases each for OpenAI, Anthropic, Gemini, Ollama). Made-with: Cursor
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.
Summary
messages with role 'tool' must be a response to a preceding message with 'tool_calls'_validate_and_fix_message_sequencewas logging orphaned tool messages but keeping them, causing API rejectionRoot Cause
In
openai_compatible_provider.py,_validate_and_fix_message_sequencehandled three cases of orphaned tool messages by only logging atdebuglevel and keeping them in the message list:tool_call_idat alltool_callstool_call_iddoesn't match any precedingtool_callsAll three cases cause OpenAI to reject the request with HTTP 400.
Fix
Changed all three cases to drop the orphaned messages with
warninglevel logs instead of silently keeping them.Test plan
Made with Cursor