-
Notifications
You must be signed in to change notification settings - Fork 6.5k
Fix tool call protocol validation errors with strict third-party providers #7276
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Fix tool call protocol validation errors with strict third-party providers #7276
Conversation
…oviders OpenAI Chat Completions protocol requires: 1. All tool_calls must have matching tool responses 2. Assistant messages with tool_calls must be immediately followed by tool responses before any other messages Some API providers (e.g., third-party OpenAI-compatible APIs) strictly validate these requirements and reject requests that violate the protocol with 400 Bad Request errors. This commit adds validation logic that: - Removes assistant messages with incomplete tool calls (no responses) - Enforces proper message sequencing by removing messages that interrupt the tool_call → tool_response sequence Fixes issues where streaming interruptions or retry logic could leave conversation history in an invalid state that violates the OpenAI protocol.
|
I have read the CLA Document and I hereby sign the CLA You can retrigger this bot by commenting recheck in this Pull Request. Posted by the CLA Assistant Lite bot. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
💡 Codex Review
Here are some automated review suggestions for this pull request.
ℹ️ About Codex in GitHub
Codex has been enabled to automatically review pull requests in this repo. Reviews are triggered when you
- Open a pull request for review
- Mark a draft as ready
- Comment "@codex review".
If Codex has suggestions, it will comment; otherwise it will react with 👍.
When you sign up for Codex through ChatGPT, Codex can also answer questions or update the PR, like "@codex address that feedback".
When an assistant message with multiple tool_calls is removed due to incomplete responses, we must also remove any tool responses that were already received for that assistant message. Otherwise, those tool responses become orphaned (no preceding tool_calls), which still violates the OpenAI protocol. Example scenario that this fixes: - Assistant makes 2 tool calls: [call_A, call_B] - Only call_A gets a response (call_B fails/times out) - Previous logic: Removed assistant message, left tool response for call_A - Result: Orphaned tool response → still violates protocol - New logic: Remove both assistant message AND tool response for call_A
|
Thanks for the contribution. We've updated our contribution guidelines to clarify that we're currently accepting contributions for bugs and security fixes, but we're not generally accepting new features at this time. We need to make sure that all new features compose well with both existing and upcoming features and fit into our roadmap. If you would like to propose a new feature, please file or upvote an enhancement request in the issue tracker. We will generally prioritize new features based on community feedback. |
This is a bug fix. Don't you think this issue is a bug? |
|
This is similar to #7038 |
Description
Adds validation logic to ensure conversation history complies with OpenAI Chat Completions protocol requirements for tool calls. This fixes 400 Bad Request errors with third-party providers that strictly validate the protocol.
Fixes #7275
Problem
The OpenAI Chat Completions protocol requires:
tool_callsmust have matching tool response messagesSome third-party OpenAI-compatible providers strictly enforce these rules and reject violations with 400 errors. Codex's conversation history can violate these requirements when:
Solution
This PR adds validation that runs before sending requests to the Chat Completions API:
Phase 1: Remove incomplete tool calls
tool_call_idvalues from assistant messages withtool_callstool_call_idvalues from tool response messagesPhase 2: Enforce sequence ordering
tool_callsExample transformation:
Before validation:
After validation:
Changes
codex-rs/core/src/chat_completions.rs(lines 332-439):Testing
Tested with third-party providers that strictly validate protocol:
Before:
After:
Benefits
Implementation Notes
The validation is conservative and only removes messages that clearly violate the protocol. It preserves valid conversation history and only filters out:
This approach prioritizes protocol compliance while minimizing information loss from the conversation context.
Related
pr/tool-call-sequence-validationmain