Skip to content
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

.Net Microsoft.SemanticKernel.HttpOperationException: "Invalid parameter: messages with role 'tool' must be a response to a preceeding message with 'tool_calls'. #4679

Closed
TRybina132 opened this issue Jan 19, 2024 · 4 comments · Fixed by #5817
Assignees
Labels
bug Something isn't working kernel Issues or pull requests impacting the core kernel .NET Issue or Pull requests regarding .NET code

Comments

@TRybina132
Copy link

TRybina132 commented Jan 19, 2024

Describe the bug
I'm using IChatComplitionService with Azure OpenAI gpt-35-turbo-16k 0613 model. When user asked for something that requires calling function of plugin, it will respond as expected. But after this response when IChatComplitionService.GetStreamingChatMessageContentsAsync called next time, this exception will be thrown:

Microsoft.SemanticKernel.HttpOperationException: Invalid parameter: messages with role 'tool' must be a response to a preceeding message with 'tool_calls'.
Status: 400 (model_error)

Content:
{
  "error": {
    "message": "Invalid parameter: messages with role 'tool' must be a response to a preceeding message with 'tool_calls'.",
    "type": "invalid_request_error",
    "param": "messages.[10].role",
    "code": null
  }
}

To Reproduce
Steps to reproduce the behavior:

  1. Add any plugin to kernel
  2. Get IChatComplitionService from kernel instance
  3. Call IChatComplitionService.GetStreamingChatMessageContentsAsync or GetChatMessageContentAsync method with user message that will require executing plugin method.
  4. Response is received
  5. Repeat step 3
  6. Exception is thrown

Here is how it done in my code:

_chatHistory.AddUserMessage("Write email to test@gmail.com");
// Response is returned
 var chatCompletionResult =
            _chat.GetChatMessageContentAsync(_chatHistory, _promptExecutionSettings, Kernel);

_chatHistory.AddUserMessage("Write email to test@gmail.com");
// Exception is thrown
 var chatCompletionResult2 =
            _chat.GetChatMessageContentAsync(_chatHistory, _promptExecutionSettings, Kernel);

Expected behavior
Chat response generating without thrown exception

Platform

  • OS: Windows, Mac
  • Language: C#
  • Source: Microsoft.SemanticKernel 1.1.0

Additional context
It seems that messages with role AuthorRole.Tool in chat history cause this problem. As temporary solution I just deleting all messages with AuthorRole.Tool role from chat history before generating response. After deleting messages no exception.

var toolsMessages = _chatHistory.Where(m => m.Role == AuthorRole.Tool).ToList();
        foreach (var m in toolsMessages)
        {
            _chatHistory.Remove(m);
        }

There was similar bug like in this issue #4556 (comment)

@shawncal shawncal added .NET Issue or Pull requests regarding .NET code triage labels Jan 19, 2024
@matthewbolanos
Copy link
Member

matthewbolanos commented Jan 19, 2024

Can you confirm if you are using auto function calling? Or are you manually calling the functions? I'm interested in knowing what code path you are using to generate the chat history since it looks like it's not being created correctly.

@matthewbolanos matthewbolanos added the kernel Issues or pull requests impacting the core kernel label Jan 19, 2024
@matthewbolanos matthewbolanos added bug Something isn't working and removed triage labels Jan 19, 2024
@TRybina132
Copy link
Author

Can you confirm if you are using auto function calling? Or are you manually calling the functions? I'm interested in knowing what code path you are using to generate the chat history since it looks like it's not being created correctly.

Yes, I'm using auto function calling. And I initialize chat history like this _chatHistory = new ChatHistory();

@rliberoff
Copy link

rliberoff commented Feb 22, 2024

We are having the same problem with the FunctionCallingStepwisePlanner.

The error says:

Microsoft.SemanticKernel.HttpOperationException: 'An assistant message with 'tool_calls' must be followed by tool messages responding to each 'tool_call_id'. The following tool_call_ids did not have response messages: call_0F3u0c9PktCz3RRDtF9hqnh9
Status: 400 (model_error)

The planner configuration is:

var plannerConfig = new FunctionCallingStepwisePlannerOptions
{
    MaxTokens = options.PlannerMaxTokens,
    MaxIterations = options.PlannerMaxIterations,
    SemanticMemoryConfig = new SemanticMemoryConfig
    {
        Memory = memory,
    },
    ExecutionSettings = new OpenAIPromptExecutionSettings
    {
        User = request.UserId,
    },
};

The model we are using is GPT-3.5-1106

The version of SK is the latest: 1.4.0

Thank you.

@tdashworth
Copy link

I am experiencing the same issue with Azure Open AI (GPT-4), auto function calling although I am persisting the ChatHistory in Cosmos DB after each generation and rebuilding next user prompt.

I'm using version 1.5.0.

github-merge-queue bot pushed a commit that referenced this issue Apr 9, 2024
…er with RAG (#5817)

### Motivation and Context

Also includes a fix so the planner handles errors in the final answer
correctly.

Closes #4679 

### Description

<!-- Describe your changes, the overall approach, the underlying design.
These notes will help understanding how your code works. Thanks! -->

### Contribution Checklist

<!-- Before submitting this PR, please make sure: -->

- [ ] The code builds clean without any errors or warnings
- [ ] The PR follows the [SK Contribution
Guidelines](https://github.com/microsoft/semantic-kernel/blob/main/CONTRIBUTING.md)
and the [pre-submission formatting
script](https://github.com/microsoft/semantic-kernel/blob/main/CONTRIBUTING.md#development-scripts)
raises no violations
- [ ] All unit tests pass, and I have added new tests where possible
- [ ] I didn't break anyone 😄
LudoCorporateShark pushed a commit to LudoCorporateShark/semantic-kernel that referenced this issue Aug 25, 2024
…er with RAG (microsoft#5817)

### Motivation and Context

Also includes a fix so the planner handles errors in the final answer
correctly.

Closes microsoft#4679 

### Description

<!-- Describe your changes, the overall approach, the underlying design.
These notes will help understanding how your code works. Thanks! -->

### Contribution Checklist

<!-- Before submitting this PR, please make sure: -->

- [ ] The code builds clean without any errors or warnings
- [ ] The PR follows the [SK Contribution
Guidelines](https://github.com/microsoft/semantic-kernel/blob/main/CONTRIBUTING.md)
and the [pre-submission formatting
script](https://github.com/microsoft/semantic-kernel/blob/main/CONTRIBUTING.md#development-scripts)
raises no violations
- [ ] All unit tests pass, and I have added new tests where possible
- [ ] I didn't break anyone 😄
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working kernel Issues or pull requests impacting the core kernel .NET Issue or Pull requests regarding .NET code
Projects
Archived in project
7 participants