{"record":{"id":"02ff2e30d55134ab","repo":"microsoft/semantic-kernel","slug":"agent-with-name-agentid-type-is-not-of-type-typ","errorCode":null,"errorMessage":"Agent with name {agentId.Type} is not of type {typeof(TAgent).Name}.","messagePattern":"Agent with name (.+?) is not of type (.+?)\\.","errorType":"exception","errorClass":"InvalidOperationException","httpStatus":null,"severity":"error","filePath":"dotnet/src/Agents/Runtime/InProcess/InProcessRuntime.cs","lineNumber":180,"sourceCode":"    /// <inheritdoc/>\n    public ValueTask<AgentId> GetAgentAsync(string agent, string key = AgentId.DefaultKey, bool lazy = true)\n        => this.GetAgentAsync(new AgentId(agent, key), lazy);\n\n    /// <inheritdoc/>\n    public async ValueTask<AgentMetadata> GetAgentMetadataAsync(AgentId agentId)\n    {\n        IHostableAgent agent = await this.EnsureAgentAsync(agentId).ConfigureAwait(false);\n        return agent.Metadata;\n    }\n\n    /// <inheritdoc/>\n    public async ValueTask<TAgent> TryGetUnderlyingAgentInstanceAsync<TAgent>(AgentId agentId) where TAgent : IHostableAgent\n    {\n        IHostableAgent agent = await this.EnsureAgentAsync(agentId).ConfigureAwait(false);\n\n        if (agent is not TAgent concreteAgent)\n        {\n            throw new InvalidOperationException($\"Agent with name {agentId.Type} is not of type {typeof(TAgent).Name}.\");\n        }\n\n        return concreteAgent;\n    }\n\n    /// <inheritdoc/>\n    public async ValueTask LoadAgentStateAsync(AgentId agentId, JsonElement state)\n    {\n        IHostableAgent agent = await this.EnsureAgentAsync(agentId).ConfigureAwait(false);\n        await agent.LoadStateAsync(state).ConfigureAwait(false);\n    }\n\n    /// <inheritdoc/>\n    public async ValueTask<JsonElement> SaveAgentStateAsync(AgentId agentId)\n    {\n        IHostableAgent agent = await this.EnsureAgentAsync(agentId).ConfigureAwait(false);\n        return await agent.SaveStateAsync().ConfigureAwait(false);\n    }","sourceCodeStart":162,"sourceCodeEnd":198,"githubUrl":"https://github.com/microsoft/semantic-kernel/blob/c028a0c7dc4f0814cdcbaba9d998f187a41197bf/dotnet/src/Agents/Runtime/InProcess/InProcessRuntime.cs#L162-L198","documentation":"TryGetUnderlyingAgentInstanceAsync<TAgent> resolves the agent via EnsureAgentAsync then attempts a runtime cast to TAgent; if the concrete agent is not assignable to TAgent it throws InvalidOperationException naming the registered type and the requested type. EnsureAgentAsync itself throws a different message (\"Agent with name ... not found\") when no factory exists, so this throw specifically means the agent exists but is the wrong type.","triggerScenarios":"Requesting TryGetUnderlyingAgentInstanceAsync<MyAgent>(id) when id was registered by a factory producing a different concrete type (e.g. a wrapper/wrapper-actor like a SequentialActor vs the original Agent); passing the wrong generic type argument.","commonSituations":"Confusing the orchestration 'actor' (IHostableAgent) registered by the runtime with the user-supplied Agent; generic type inferred incorrectly; inspecting an agent registered through an orchestration that wraps the original.","solutions":["Request the exact concrete type the factory produces; for orchestration-registered agents request the hostable actor type, not the original Agent.","Use TryGetUnderlyingAgentInstanceAsync<IHostableAgent> if you only need the hosted interface.","Check agent.Metadata first to determine the underlying type before the typed call."],"exampleFix":"// before\nvar agent = await runtime.TryGetUnderlyingAgentInstanceAsync<MyAgent>(agentId); // actor is not MyAgent\n\n// after\nvar hosted = await runtime.TryGetUnderlyingAgentInstanceAsync<IHostableAgent>(agentId);","handlingStrategy":"type-guard","validationCode":"AgentMetadata meta = await runtime.GetAgentMetadataAsync(agentId);\n// Then request a type compatible with meta's underlying agent type.","typeGuard":"// Request the most general hosted type first, then narrow.\nIHostableAgent hosted = await runtime.TryGetUnderlyingAgentInstanceAsync<IHostableAgent>(agentId);\nif (hosted is TAgent typed) { /* use typed */ }","tryCatchPattern":"try { return await runtime.TryGetUnderlyingAgentInstanceAsync<TAgent>(agentId); }\ncatch (InvalidOperationException) { /* wrong concrete type; fall back to IHostableAgent or fail */ }","preventionTips":["Request the concrete type the factory actually produces.","For orchestration-registered agents, expect the hostable actor, not the original Agent.","Inspect Metadata before the typed retrieval."],"tags":["inprocess-runtime","typing","casting","agents","dotnet"],"backgroundTag":null,"analyzedSha":"c028a0c7dc4f0814cdcbaba9d998f187a41197bf","analyzedAt":"2026-08-13T13:48:05.040Z","schemaVersion":2},"datasetVersion":"2026-08-13T14:17:21.547Z"}