Skip to content

Commit 08275f6

Browse files
authored
.NET: Improve session cast error message quality and consistency (#3973)
* Improve session cast error messge consistency * Update changelog
1 parent 36d52a1 commit 08275f6

13 files changed

Lines changed: 19 additions & 18 deletions

File tree

dotnet/src/Microsoft.Agents.AI.A2A/A2AAgent.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,7 @@ protected override ValueTask<JsonElement> SerializeSessionCoreAsync(AgentSession
8181

8282
if (session is not A2AAgentSession typedSession)
8383
{
84-
throw new InvalidOperationException("The provided session is not compatible with the agent. Only sessions created by the agent can be serialized.");
84+
throw new InvalidOperationException($"The provided session type '{session.GetType().Name}' is not compatible with this agent. Only sessions of type '{nameof(A2AAgentSession)}' can be serialized by this agent.");
8585
}
8686

8787
return new(typedSession.Serialize(jsonSerializerOptions));
@@ -256,7 +256,7 @@ private async ValueTask<A2AAgentSession> GetA2ASessionAsync(AgentSession? sessio
256256

257257
if (session is not A2AAgentSession typedSession)
258258
{
259-
throw new InvalidOperationException($"The provided session type {session.GetType()} is not compatible with the agent. Only A2A agent created sessions are supported.");
259+
throw new InvalidOperationException($"The provided session type '{session.GetType().Name}' is not compatible with this agent. Only sessions of type '{nameof(A2AAgentSession)}' can be used by this agent.");
260260
}
261261

262262
return typedSession;

dotnet/src/Microsoft.Agents.AI.CopilotStudio/CopilotStudioAgent.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ protected override ValueTask<JsonElement> SerializeSessionCoreAsync(AgentSession
6060

6161
if (session is not CopilotStudioAgentSession typedSession)
6262
{
63-
throw new InvalidOperationException("The provided session is not compatible with the agent. Only sessions created by the agent can be serialized.");
63+
throw new InvalidOperationException($"The provided session type '{session.GetType().Name}' is not compatible with this agent. Only sessions of type '{nameof(CopilotStudioAgentSession)}' can be serialized by this agent.");
6464
}
6565

6666
return new(typedSession.Serialize(jsonSerializerOptions));
@@ -84,7 +84,7 @@ protected override async Task<AgentResponse> RunCoreAsync(
8484
session ??= await this.CreateSessionAsync(cancellationToken).ConfigureAwait(false);
8585
if (session is not CopilotStudioAgentSession typedSession)
8686
{
87-
throw new InvalidOperationException("The provided session is not compatible with the agent. Only sessions created by the agent can be used.");
87+
throw new InvalidOperationException($"The provided session type '{session.GetType().Name}' is not compatible with this agent. Only sessions of type '{nameof(CopilotStudioAgentSession)}' can be used by this agent.");
8888
}
8989

9090
typedSession.ConversationId ??= await this.StartNewConversationAsync(cancellationToken).ConfigureAwait(false);
@@ -123,7 +123,7 @@ protected override async IAsyncEnumerable<AgentResponseUpdate> RunCoreStreamingA
123123
session ??= await this.CreateSessionAsync(cancellationToken).ConfigureAwait(false);
124124
if (session is not CopilotStudioAgentSession typedSession)
125125
{
126-
throw new InvalidOperationException("The provided session is not compatible with the agent. Only sessions created by the agent can be used.");
126+
throw new InvalidOperationException($"The provided session type '{session.GetType().Name}' is not compatible with this agent. Only sessions of type '{nameof(CopilotStudioAgentSession)}' can be used by this agent.");
127127
}
128128

129129
typedSession.ConversationId ??= await this.StartNewConversationAsync(cancellationToken).ConfigureAwait(false);

dotnet/src/Microsoft.Agents.AI.DurableTask/CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
- Changed AIAgent.SerializeSession to AIAgent.SerializeSessionAsync ([#3879](https://github.com/microsoft/agent-framework/pull/3879))
1515
- Changed ChatHistory and AIContext Providers to have pipeline semantics ([#3806](https://github.com/microsoft/agent-framework/pull/3806))
1616
- Marked all `RunAsync<T>` overloads as `new`, added missing ones, and added support for primitives and arrays ([#3803](https://github.com/microsoft/agent-framework/pull/3803))
17+
- Improve session cast error message quality and consistency ([#3973](https://github.com/microsoft/agent-framework/pull/3973))
1718

1819
## v1.0.0-preview.251204.1
1920

dotnet/src/Microsoft.Agents.AI.DurableTask/DurableAIAgent.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ protected override ValueTask<JsonElement> SerializeSessionCoreAsync(AgentSession
5555

5656
if (session is not DurableAgentSession durableSession)
5757
{
58-
throw new InvalidOperationException("The provided session is not compatible with the agent. Only sessions created by the agent can be serialized.");
58+
throw new InvalidOperationException($"The provided session type '{session.GetType().Name}' is not compatible with this agent. Only sessions of type '{nameof(DurableAgentSession)}' can be serialized by this agent.");
5959
}
6060

6161
return new(durableSession.Serialize(jsonSerializerOptions));

dotnet/src/Microsoft.Agents.AI.DurableTask/DurableAIAgentProxy.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ protected override ValueTask<JsonElement> SerializeSessionCoreAsync(AgentSession
2020

2121
if (session is not DurableAgentSession durableSession)
2222
{
23-
throw new InvalidOperationException("The provided session is not compatible with the agent. Only sessions created by the agent can be serialized.");
23+
throw new InvalidOperationException($"The provided session type '{session.GetType().Name}' is not compatible with this agent. Only sessions of type '{nameof(DurableAgentSession)}' can be serialized by this agent.");
2424
}
2525

2626
return new(durableSession.Serialize(jsonSerializerOptions));

dotnet/src/Microsoft.Agents.AI.GitHub.Copilot/GitHubCopilotAgent.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -104,7 +104,7 @@ protected override ValueTask<JsonElement> SerializeSessionCoreAsync(AgentSession
104104

105105
if (session is not GitHubCopilotAgentSession typedSession)
106106
{
107-
throw new InvalidOperationException("The provided session is not compatible with the agent. Only sessions created by the agent can be serialized.");
107+
throw new InvalidOperationException($"The provided session type '{session.GetType().Name}' is not compatible with this agent. Only sessions of type '{nameof(GitHubCopilotAgentSession)}' can be serialized by this agent.");
108108
}
109109

110110
return new(typedSession.Serialize(jsonSerializerOptions));
@@ -139,7 +139,7 @@ protected override async IAsyncEnumerable<AgentResponseUpdate> RunCoreStreamingA
139139
if (session is not GitHubCopilotAgentSession typedSession)
140140
{
141141
throw new InvalidOperationException(
142-
$"The provided session type {session.GetType()} is not compatible with the agent. Only GitHub Copilot agent created sessions are supported.");
142+
$"The provided session type '{session.GetType().Name}' is not compatible with this agent. Only sessions of type '{nameof(GitHubCopilotAgentSession)}' can be used by this agent.");
143143
}
144144

145145
// Ensure the client is started

dotnet/src/Microsoft.Agents.AI.Workflows/WorkflowHostAgent.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,7 @@ protected override ValueTask<JsonElement> SerializeSessionCoreAsync(AgentSession
7474

7575
if (session is not WorkflowSession workflowSession)
7676
{
77-
throw new InvalidOperationException("The provided session is not compatible with the agent. Only sessions created by the agent can be serialized.");
77+
throw new InvalidOperationException($"The provided session type '{session.GetType().Name}' is not compatible with this agent. Only sessions of type '{nameof(WorkflowSession)}' can be serialized by this agent.");
7878
}
7979

8080
return new(workflowSession.Serialize(jsonSerializerOptions));

dotnet/src/Microsoft.Agents.AI/ChatClient/ChatClientAgent.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -366,7 +366,7 @@ protected override ValueTask<JsonElement> SerializeSessionCoreAsync(AgentSession
366366

367367
if (session is not ChatClientAgentSession typedSession)
368368
{
369-
throw new InvalidOperationException("The provided session is not compatible with the agent. Only sessions created by the agent can be serialized.");
369+
throw new InvalidOperationException($"The provided session type '{session.GetType().Name}' is not compatible with this agent. Only sessions of type '{nameof(ChatClientAgentSession)}' can be serialized by this agent.");
370370
}
371371

372372
return new(typedSession.Serialize(jsonSerializerOptions));
@@ -674,7 +674,7 @@ private async Task
674674
session ??= await this.CreateSessionAsync(cancellationToken).ConfigureAwait(false);
675675
if (session is not ChatClientAgentSession typedSession)
676676
{
677-
throw new InvalidOperationException("The provided session is not compatible with the agent. Only sessions created by the agent can be used.");
677+
throw new InvalidOperationException($"The provided session type '{session.GetType().Name}' is not compatible with this agent. Only sessions of type '{nameof(ChatClientAgentSession)}' can be used by this agent.");
678678
}
679679

680680
// Supplying messages when continuing a background response is not allowed.

dotnet/tests/Microsoft.Agents.AI.Hosting.AGUI.AspNetCore.IntegrationTests/BasicStreamingTests.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -357,7 +357,7 @@ protected override ValueTask<JsonElement> SerializeSessionCoreAsync(AgentSession
357357
{
358358
if (session is not FakeAgentSession fakeSession)
359359
{
360-
throw new InvalidOperationException("The provided session is not compatible with the agent. Only sessions created by the agent can be serialized.");
360+
throw new InvalidOperationException($"The provided session type '{session.GetType().Name}' is not compatible with this agent. Only sessions of type '{nameof(FakeAgentSession)}' can be serialized by this agent.");
361361
}
362362

363363
return new(JsonSerializer.SerializeToElement(fakeSession, jsonSerializerOptions));

dotnet/tests/Microsoft.Agents.AI.Hosting.AGUI.AspNetCore.IntegrationTests/ForwardedPropertiesTests.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -345,7 +345,7 @@ protected override ValueTask<JsonElement> SerializeSessionCoreAsync(AgentSession
345345
{
346346
if (session is not FakeAgentSession fakeSession)
347347
{
348-
throw new InvalidOperationException("The provided session is not compatible with the agent. Only sessions created by the agent can be serialized.");
348+
throw new InvalidOperationException($"The provided session type '{session.GetType().Name}' is not compatible with this agent. Only sessions of type '{nameof(FakeAgentSession)}' can be serialized by this agent.");
349349
}
350350

351351
return new(JsonSerializer.SerializeToElement(fakeSession, jsonSerializerOptions));

0 commit comments

Comments
 (0)