fix: OpenAI API compliance for tool response format#2450
Open
drewd789 wants to merge 2 commits intoQwenLM:mainfrom
Open
fix: OpenAI API compliance for tool response format#2450drewd789 wants to merge 2 commits intoQwenLM:mainfrom
drewd789 wants to merge 2 commits intoQwenLM:mainfrom
Conversation
f6b1f8a to
ca6e72d
Compare
Collaborator
|
@drewd789 Thanks for your contribution, but can you help link any existing issue to this PR or file a new one if necessary? We would like to reproduce issue first before merging any PR targeting core module. |
Add tests that demonstrate the OpenAI API compliance issue with tool response storage format (https://go.openai.com/chat-api-ref). See ChatCompletionToolMessageParam. The current implementation stores tool results with a message field containing Gemini-format functionResponse parts. This causes 400 errors when sending conversation history to at least one OpenAI-compatible API (hunter-alpha via OpenRouter) because: - Tool responses are stored as role: "user" instead of role: "tool" - The parts array with functionResponse is Gemini format, not OpenAI - The converter duplicates the functionResponse in the messages array Tests added: - chatRecordingService.test.ts: Verify tool_result records do NOT have a message field (will fail until fix is applied) - sessionService.test.ts: Verify buildApiHistoryFromConversation reconstructs tool messages from toolCallResult (will fail until fix is applied) These tests will pass after the fix that: 1. Removes message field from tool_result records in storage 2. Reconstructs tool messages from toolCallResult at API time 3. Ensures proper OpenAI format: {role: "tool", tool_call_id, content}
Fix storage and conversion of tool results to comply with OpenAI API standard (https://go.openai.com/chat-api-ref), specifically the ChatCompletionToolMessageParam requirements. The previous implementation stored tool results with a message field containing Gemini-format functionResponse parts. This caused 400 errors when sending conversation history to OpenAI-compatible APIs because: - Tool results were stored with role: "user" instead of role: "tool" - The parts array used Gemini functionResponse format, not OpenAI - The converter duplicated functionResponse in the messages array Changes made: - chatRecordingService: stop storing tool results message field with Gemini-style functionResponse parts; tool_result stream now stores only tool output data - sessionService: buildApiHistoryFromConversation now reconstructs tool messages from toolCallResult records in proper OpenAI format: {role: tool, tool_call_id, content} This resolves 400 errors with OpenAI-compatible APIs (e.g., hunter-alpha via OpenRouter). Fixes the failing tests added in commit 68e30077c.
ca6e72d to
9d671c1
Compare
Contributor
Author
|
@tanzhenxin yes, will post for this and the other PR later today, swamped at the moment. |
Contributor
Author
|
@tanzhenxin https://youtu.be/xwd5oZxwyes Video of both bugs repro'd. This PR fixes the 400 error at the end. Repro is very easy, just use |
Collaborator
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.


Description
Fixes OpenAI API compliance bug in tool response storage and conversion format.
Problem
When using tools with OpenAI-compatible APIs (e.g., hunter-alpha via OpenRouter), conversation history containing tool calls would fail with 400 errors. The root cause was non-compliance with the OpenAI API standard for tool response messages (ChatCompletionToolMessageParam).
Issues:
Solution
Storage Layer (chatRecordingService.ts)
Conversion Layer (sessionService.ts)
Testing
Two-part commit strategy:
test commit (57787a2): Adds failing tests that verify the bug exists
fix commit (8226024): Implements the fix that makes tests pass
Related Issues
Fixes issues with OpenAI-compatible API providers when tools are used in conversation.
Checklist