Skip to content

fix: drop orphaned tool messages in message validation#267

Merged
veithly merged 3 commits intomainfrom
fix/tool-message-validation
Apr 14, 2026
Merged

fix: drop orphaned tool messages in message validation#267
veithly merged 3 commits intomainfrom
fix/tool-message-validation

Conversation

@veithly
Copy link
Copy Markdown
Collaborator

@veithly veithly commented Apr 14, 2026

Summary

  • Fix OpenAI API 400 error: messages with role 'tool' must be a response to a preceding message with 'tool_calls'
  • The _validate_and_fix_message_sequence was logging orphaned tool messages but keeping them, causing API rejection

Root Cause

In openai_compatible_provider.py, _validate_and_fix_message_sequence handled three cases of orphaned tool messages by only logging at debug level and keeping them in the message list:

  1. Tool messages with no tool_call_id at all
  2. Tool messages with no preceding assistant message containing tool_calls
  3. Tool messages whose tool_call_id doesn't match any preceding tool_calls

All three cases cause OpenAI to reject the request with HTTP 400.

Fix

Changed all three cases to drop the orphaned messages with warning level logs instead of silently keeping them.

Test plan

  • Verify conversations with clean tool call sequences work normally
  • Verify orphaned tool messages (from session history injection) are properly dropped
  • Test with OpenRouter/OpenAI provider to confirm no more 400 errors
  • Confirm Gemini provider (separate code path) is unaffected

Made with Cursor

veithly added 2 commits April 14, 2026 12:00
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
@XSpoonAi XSpoonAi deleted a comment from chatgpt-codex-connector Bot Apr 14, 2026
@veithly
Copy link
Copy Markdown
Collaborator Author

veithly commented Apr 14, 2026

@codex review it

@chatgpt-codex-connector
Copy link
Copy Markdown

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
@veithly veithly merged commit 0297ec7 into main Apr 14, 2026
1 check passed
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.

1 participant