dotnet / dotnet/extensions

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

Open
#7,444 2 comments 0 reactions 0 assignees View on GitHub
area-ai
Dominant language
C#
Stars
3.2k
Forks
894
Avg merge
1d 12h
Merged PRs (30d)
23

Description

### Description

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

```csharp
var contents = new List();
contents.Add(new Microsoft.Extensions.AI.TextContent("Describe the image"));
contents.Add(new DataContent(new ReadOnlyMemory(someBytes), "image/jpg"));
var history = new List();
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:

```csharp
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.d__20.MoveNext()
at Microsoft.Extensions.AI.OpenAIResponsesChatClient.d__7.MoveNext()
at Microsoft.Extensions.AI.FunctionInvokingChatClient.d__38.MoveNext()
at Microsoft.Agents.AI.ChatClientAgent.d__27.MoveNext()
at Microsoft.Agents.AI.ChatClientAgent.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 https://github.com/dotnet/extensions/pull/7349 which changed how DataContent is handled:

BEFORE:

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

AFTER:

```csharp
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_

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.