-
Notifications
You must be signed in to change notification settings - Fork 2.8k
fix(vscode-lm): add tool call/result sequence validation #10861
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
base: main
Are you sure you want to change the base?
Conversation
Fixes #10843 The VSCode Language Model API requires that when an Assistant message contains LanguageModelToolCallPart(s), the immediately next message MUST be a User message with LanguageModelToolResultPart(s) with matching callIds. This commit adds a validateAndRepairToolSequence function that: - Identifies assistant messages with tool calls - Checks that the next message is a user message with matching tool results - Adds placeholder ToolResultParts for any missing tool results - Creates user messages with placeholder results when no user message follows This fixes the 'Invalid request: Tool call part must be followed by a User message with a LanguageModelToolResultPart with a matching callId' error that occurs when using MCP servers (like JIRA/Confluence) with the vscode-lm provider in versions after 3.36.
PR Review Summary✅ LGTM - Approved This PR adds a well-implemented validation and repair mechanism for tool call/result sequences in the VSCode Language Model API integration. What This PR DoesThe PR addresses Issue #10843 where API calls fail when using MCP servers with the vscode-lm provider due to strict message ordering requirements. The solution:
Code Quality Assessment
Minor Observations (Non-blocking)
Test VerificationAll 27 tests pass in the vscode-lm-format test suite ✅ Mention @roomote in a comment to request specific changes to this pull request or fix all unresolved issues. |
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.
✅ LGTM - Ready for human approval
This well-implemented fix addresses Issue #10843 by adding proper validation and repair of tool call/result sequences for the VSCode Language Model API.
Summary:
- Clean, well-structured implementation with good separation of concerns
- All 27 tests pass ✅
- Excellent error handling (gracefully handles null/undefined inputs)
- Helpful debug logging when repairs are made
Minor suggestion (non-blocking): The JSDoc for validateAndRepairToolSequence mentions "Orphaned tool results (no preceding tool call): Removes them" but the implementation only adds missing results - consider updating the documentation for accuracy.
Summary
This PR attempts to address Issue #10843. Feedback and guidance are welcome.
Problem
After version 3.36, API calls fail when using MCP servers (JIRA/Confluence) with the vscode-lm provider, showing the error:
Root Cause
The VSCode Language Model API requires strict message ordering:
LanguageModelToolCallPart(s)LanguageModelToolResultPart(s)with matching callIdsThis requirement can be violated in edge cases like conversation condensation, resume scenarios, or when multiple MCP tool calls occur.
Solution
Added a
validateAndRepairToolSequencefunction insrc/api/transform/vscode-lm-format.tsthat:ToolResultPartsto the user messageThe validation function is called in
vscode-lm.tsafter converting messages to VSCode LM format.Changes
src/api/transform/vscode-lm-format.ts: AddedvalidateAndRepairToolSequencefunction with helper functions for extracting tool call and result IDssrc/api/providers/vscode-lm.ts: Integrated the validation function in thecreateMessagemethodsrc/api/transform/__tests__/vscode-lm-format.spec.ts: Added comprehensive tests covering:Testing
All 27 tests pass in the vscode-lm-format test suite.
Important
Adds validation and repair for tool call/result sequences in VSCode LM messages to ensure API compliance.
validateAndRepairToolSequenceinvscode-lm-format.tsto ensure tool call/result sequence in VSCode LM messages.validateAndRepairToolSequenceincreateMessagemethod invscode-lm.ts.vscode-lm-format.spec.tsfor empty input, no tool calls, matched tool calls/results, missing tool results, multiple tool calls, and consecutive assistant messages.This description was created by
for 40d6b5e. You can customize this summary. It will automatically update as commits are pushed.