Skip to content

.NET: Provide typed access to RunAgentInput.State in .NET AG-UI #8359

Description

Description

Accessing AG-UI client state currently requires recovering RunAgentInput from ChatOptions, inspecting its JsonElement, and deserializing it manually:

if (chatOptions.TryGetRunAgentInput(out RunAgentInput? input) &&
    input.State is { ValueKind: JsonValueKind.Object } state)
{
    var value = state.Deserialize(
        AppJsonSerializerContext.Default.DocumentState);
}

Provide a typed convenience API:

if (chatOptions.TryGetRunAgentState(
    AppJsonSerializerContext.Default.DocumentState,
    out DocumentState? state))
{
    Validate(state);
}

Proposed signature:

public static bool TryGetRunAgentState<T>(
    this ChatOptions options,
    JsonTypeInfo<T> jsonTypeInfo,
    out T? state);

The method should return false when no state was supplied and throw a clear deserialization exception for malformed state. Requiring JsonTypeInfo<T> keeps the API AOT-safe.

Client state remains untrusted. The application is responsible for validation and deciding whether to use it in tools or model context. This is narrower than #7745 and does not integrate client state with AgentSession.StateBag.

Code Sample

if (chatOptions.TryGetRunAgentState(
    AppJsonSerializerContext.Default.DocumentState,
    out DocumentState? state))
{
    Validate(state);
}

Language/SDK

.NET

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    .NETUsage: [Issues, PRs], Target: .NettriageUsage: [Issues], Target: All issues that still need to be triaged

    Type

    No type

    Projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions