Skip to content

DataContent causes UriFormatException - Invalid URI: The Uri string is too long in 1.0.0-rc5 #7444

@nor0x

Description

@nor0x

Description

i have this code that is trying to send a history of TextContent and DataContent to an agent.

var contents = new List<AIContent>();
contents.Add(new Microsoft.Extensions.AI.TextContent("Describe the image"));
contents.Add(new DataContent(new ReadOnlyMemory<byte>(someBytes), "image/jpg"));
var history = new List<ChatMessage>();
history.Add(new ChatMessage(ChatRole.User, contents));
var imageCaption = await _agent.RunAsync(history);

With the latest release of Microsoft.Extensions.AI, i am getting the following error:

   System.Exception {System.UriFormatException}
   {"Invalid URI: The Uri string is too long."}

   at System.Uri.CreateThis(String uri, Boolean dontEscape, UriKind uriKind, UriCreationOptions& creationOptions)
   at System.Uri..ctor(String uriString)
   at Microsoft.Extensions.AI.OpenAIResponsesChatClient.<ToOpenAIResponseItems>d__20.MoveNext()
   at Microsoft.Extensions.AI.OpenAIResponsesChatClient.<GetResponseAsync>d__7.MoveNext()
   at Microsoft.Extensions.AI.FunctionInvokingChatClient.<GetResponseAsync>d__38.MoveNext()
   at Microsoft.Agents.AI.ChatClientAgent.<RunCoreAsync>d__27.MoveNext()
   at Microsoft.Agents.AI.ChatClientAgent.<RunCoreAsync>d__27.MoveNext()

Previously i was using 1.0.0-rc2 (10.3.0) which worked fine, but after upgrading to 1.0.0-rc5 (10.4.0), i am getting this error. I have not changed any code other than the version of Microsoft.Extensions.AI.

I think i have narrowed down the issue to this PR #7349 which changed how DataContent is handled:

BEFORE:

case DataContent dataContent when dataContent.HasTopLevelMediaType("image"):
         (parts ??= []).Add(ResponseContentPart.CreateInputImagePart(BinaryData.FromBytes(dataContent.Data), dataContent.MediaType, GetImageDetail(item)));

AFTER:

case DataContent dataContent when dataContent.HasTopLevelMediaType("image"):
         (parts ??= []).Add(ResponseContentPart.CreateInputImagePart(new Uri(dataContent.Uri), GetImageDetail(item)));

I know that with .NET 10 the Uri Lenght limits were removed:
https://learn.microsoft.com/en-us/dotnet/core/compatibility/networking/10.0/uri-length-limits-removed

for now unfortunately i'm stuck with .NET 8 which is a LTS and still in support - do you have some thoughts about the change @stephentoub ? Thanks

Regression?

Yes 1.0.0-rc2 was working fine

Known Workarounds

No response

Configuration

Microsoft.Extension.Ai 1.0.0-rc5
.NET 8

Other information

No response

Metadata

Metadata

Assignees

No one assigned

    Labels

    area-aiMicrosoft.Extensions.AI libraries

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions