openai / openai-dotnet

The official .NET library for the OpenAI API
https://www.nuget.org/packages/OpenAI/AbsoluteLatest
MIT License
1.04k stars 99 forks source link

CompleteChat requests with the tools and the name field set for the assistant messages results in "The model produced invalid content" response #162

Closed NikolaMorena closed 1 week ago

NikolaMorena commented 1 month ago

Confirm this is not an issue with the underlying OpenAI API

Confirm this is not an issue with Azure OpenAI

Describe the bug

The .Net CompleteChat requests that include tools and have name field set for the assistant messages produce "The model produced invalid content" response in 20-30% of the cases.

To Reproduce

Start with official example https://github.com/openai/openai-dotnet?tab=readme-ov-file#how-to-use-chat-completions-with-tools-and-function-calling . Ensure it works as expected for you. Modify the example by setting ParticipantName for each AssistantChatMessage before adding it to the messages list. Ask for the weather 10-20 times. In 20-30% of the cases, the API call fails with the following message: "The model produced invalid content. Consider modifying your prompt if you are seeing this error persistently."

Code snippets

Instead:
messages.Add(new AssistantChatMessage(chatCompletion));
...use...
var acm = new AssistantChatMessage(chatCompletion);
acm.ParticipantName = "Debbie";
messages.Add(acm);

OS

winOs

.NET version

.Net 8.0

Library version

2.0.0.-beta.8

joseharriaga commented 1 month ago

Thank you for reaching out, @NikolaMorena ! From what I have seen, this error can happen when the function calling API is called incorrectly. Here are a few ideas:

  1. You can start by verifying that you're using the following classes: ToolChatMessage, ChatTool, ChatToolCall, and ChatToolChoice.
  2. In particular, be careful not to confuse them or mix them with other similar classes such as: FunctionChatMessage, ChatFunction, ChatFunctionCall, and ChatFunctionChoice.
  3. Another thing to look out for is to verify that you're passing all the right tool call IDs, names, parameters, etc. in the right orders.
joseharriaga commented 1 week ago

I will close this issue for now. Let us know if you're still having issues. Thank you!