.Net: Bug: OpenAI Assistant throws Invalid URI: The Uri string is too long.
Describe the bug The Semantic Kernel C# library isn't properly handling images for OpenAI assistants. The issue lies in how the AssistantMessageFactory creates the underlying image content which can exceed the 65k limit of System.Uri.
To Reproduce Steps to reproduce the behavior:
- Setup an OpenAI assistant agent
- Setup a group chat (one agent is fine)
- Add a ChatMessageContent with ImageContent item(s)
- Observe the System.UriFormatException: Invalid URI: The Uri string is too long.
Expected behavior I would expect Semantic Kernel to use the appropriate OpenAI overloads to avoid this error
Screenshots N/A
Platform
- Language: C#
- Source: Microsoft.SemanticKernel, v1.48.0
- AI model: OpenAI 4o (2024-11-20)
- IDE: Visual Studio
- OS: Windows
Additional context There was a similar issue reported here for the OpenAI library where the user was able to work around the problem by supplying a list of bytes. https://github.com/openai/openai-dotnet/issues/138
It's worth noting that the ChatCompletions API doesn't suffer from this issue. Appears to only occur when using an OpenAI Assistant.
System.UriFormatException: Invalid URI: The Uri string is too long.
at System.Uri.CreateThis(String uri, Boolean dontEscape, UriKind uriKind, UriCreationOptions& creationOptions)
at Microsoft.SemanticKernel.Agents.OpenAI.Internal.AssistantMessageFactory.GetMessageContents(ChatMessageContent message)+MoveNext()
at OpenAI.Assistants.AssistantClient.CreateMessageAsync(String threadId, MessageRole role, IEnumerable1 content, MessageCreationOptions options, CancellationToken cancellationToken) at Microsoft.SemanticKernel.Agents.OpenAI.Internal.AssistantThreadActions.CreateMessageAsync(AssistantClient client, String threadId, ChatMessageContent message, CancellationToken cancellationToken) at Microsoft.SemanticKernel.Agents.OpenAI.OpenAIAssistantChannel.ReceiveAsync(IEnumerable1 history, CancellationToken cancellationToken)
at Microsoft.SemanticKernel.Agents.AgentChat.GetOrCreateChannelAsync(Agent agent, CancellationToken cancellationToken)
at Microsoft.SemanticKernel.Agents.AgentChat.InvokeStreamingAgentAsync(Agent agent, CancellationToken cancellationToken)+MoveNext()
at Microsoft.SemanticKernel.Agents.AgentChat.InvokeStreamingAgentAsync(Agent agent, CancellationToken cancellationToken)+System.Threading.Tasks.Sources.IValueTaskSource<System.Boolean>.GetResult()
at Microsoft.SemanticKernel.Agents.AgentGroupChat.InvokeStreamingAsync(Agent agent, CancellationToken cancellationToken)+MoveNext()
at Microsoft.SemanticKernel.Agents.AgentGroupChat.InvokeStreamingAsync(Agent agent, CancellationToken cancellationToken)+MoveNext()
at Microsoft.SemanticKernel.Agents.AgentGroupChat.InvokeStreamingAsync(Agent agent, CancellationToken cancellationToken)+System.Threading.Tasks.Sources.IValueTaskSource<System.Boolean>.GetResult()
at Microsoft.SemanticKernel.Agents.AgentGroupChat.InvokeStreamingAsync(CancellationToken cancellationToken)+MoveNext()
at Microsoft.SemanticKernel.Agents.AgentGroupChat.InvokeStreamingAsync(CancellationToken cancellationToken)+MoveNext()
at Microsoft.SemanticKernel.Agents.AgentGroupChat.InvokeStreamingAsync(CancellationToken cancellationToken)+System.Threading.Tasks.Sources.IValueTaskSource<System.Boolean>.GetResult()
Facing same issue here! SK version v1.51.0
I'm wondering if loading the image with FileReferenceContent produces equivalent interpretation from the model or not