Skip to content

Fix error when requesting completion to Copilot Chat without tools #30007

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

Merged
merged 1 commit into from
May 6, 2025
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Fix error when requesting completion to Copilot Chat without tools
The API will return a Bad Request (with no error message) when tools were used previously in the conversation but no tools are provided as part of a new request.

Inserting a dummy tool seems to circumvent this error.

Co-authored-by: Bennet Bo Fenner <[email protected]>
  • Loading branch information
as-cii and bennetbo committed May 6, 2025
commit 96e8ad89360275752f3810c34fda170b26c33182
20 changes: 18 additions & 2 deletions crates/language_models/src/provider/copilot_chat.rs
Original file line number Diff line number Diff line change
Expand Up @@ -437,6 +437,7 @@ impl CopilotChatLanguageModel {
}
}

let mut tool_called = false;
let mut messages: Vec<ChatMessage> = Vec::new();
for message in request_messages {
let text_content = {
Expand Down Expand Up @@ -477,6 +478,7 @@ impl CopilotChatLanguageModel {
let mut tool_calls = Vec::new();
for content in &message.content {
if let MessageContent::ToolUse(tool_use) = content {
tool_called = true;
tool_calls.push(ToolCall {
id: tool_use.id.to_string(),
content: copilot::copilot_chat::ToolCallContent::Function {
Expand Down Expand Up @@ -504,7 +506,7 @@ impl CopilotChatLanguageModel {
}
}

let tools = request
let mut tools = request
.tools
.iter()
.map(|tool| Tool::Function {
Expand All @@ -514,7 +516,21 @@ impl CopilotChatLanguageModel {
parameters: tool.input_schema.clone(),
},
})
.collect();
.collect::<Vec<_>>();

// The API will return a Bad Request (with no error message) when tools
// were used previously in the conversation but no tools are provided as
// part of this request. Inserting a dummy tool seems to circumvent this
// error.
if tool_called && tools.is_empty() {
tools.push(Tool::Function {
function: copilot::copilot_chat::Function {
name: "noop".to_string(),
description: "No operation".to_string(),
parameters: serde_json::json!({}),
},
});
}

Ok(CopilotChatRequest {
intent: true,
Expand Down
Loading