Skip to content

.NET: [Bug]: OpenAIResponsesAgentClient in samples require agentName for OpenAI #5324

Description

@spech66

Description

If you run the dotnet/samples/05-end-to-end/ with OpenAI compatible Endpoints (LLMGateway in our case) you will get the following error.

Error: HTTP 400 (invalid_request_error: missing_required_parameter) No 'agent.name' or 'metadata["entity_id"]' specified in the request.

The code in https://github.com/microsoft/agent-framework/blob/a2044829b13659ac40f7f4112f74efcce15397cc/dotnet/samples/05-end-to-end/AgentWebChat/AgentWebChat.Web/OpenAIResponsesAgentClient.cs doesn't allow setting the agentName.

We created the AgentNameInjectionPolicy seen in the code sample attached but this feels like a very hacky way. Is there a better approach or did we miss something?

Code Sample

/// Pipeline policy that injects agent.name into the request body.
internal sealed class AgentNameInjectionPolicy(string agentName, string? modelName = null) : PipelinePolicy
{
    public override void Process(PipelineMessage message, IReadOnlyList<PipelinePolicy> pipeline, int currentIndex)
    {
        InjectFields(message);
        ProcessNext(message, pipeline, currentIndex);
    }

    public override async ValueTask ProcessAsync(PipelineMessage message, IReadOnlyList<PipelinePolicy> pipeline, int currentIndex)
    {
        InjectFields(message);
        await ProcessNextAsync(message, pipeline, currentIndex);
    }

    private void InjectFields(PipelineMessage message)
    {
        if (message.Request?.Content is null) return;

        using var ms = new MemoryStream();
        message.Request.Content.WriteTo(ms);
        ms.Position = 0;

        var doc = JsonDocument.Parse(ms);
        using var outputMs = new MemoryStream();
        using var writer = new Utf8JsonWriter(outputMs);

        writer.WriteStartObject();
        foreach (var prop in doc.RootElement.EnumerateObject())
        {
            if (modelName is not null && prop.NameEquals("model"))
            {
                writer.WriteString("model", modelName);
                continue;
            }
            prop.WriteTo(writer);
        }

        writer.WritePropertyName("agent");
        writer.WriteStartObject();
        writer.WriteString("name", agentName);
        writer.WriteEndObject();

        writer.WriteEndObject();
        writer.Flush();

        message.Request.Content = BinaryContent.Create(new BinaryData(outputMs.ToArray()));
        message.Request.Headers.Remove("Content-Type");
        message.Request.Headers.Add("Content-Type", "application/json");
    }
}
internal sealed class OpenAIResponsesAgentClient(HttpClient httpClient) : AgentClientBase
{
    [...]

        options.AddPolicy(
            new AgentNameInjectionPolicy(agentName, "XXXMODELNAME"), // pass modelName here
            PipelinePosition.BeforeTransport);
[...]
        await foreach (var update in openAiClient.GetStreamingResponseAsync(messages, chatOptions, cancellationToken: cancellationToken))
        {
            yield return new AgentResponseUpdate(update);
        }
    }
}

Error Messages / Stack Traces

HTTP 400 (invalid_request_error: missing_required_parameter) No 'agent.name' or 'metadata["entity_id"]' specified in the request.

Package Versions

Microsoft.Agents.AI.Hosting 1.1.0-preview.260410.1, Microsoft.Agents.AI.OpenAI 1.1.0

.NET Version

No response

Additional Context

No response

Metadata

Metadata

Assignees

Labels

.NETUsage: [Issues, PRs], Target: .NetbugUsage: [Issues], Target: all issues (Legacy, prefer issue type: bug)

Type

Projects

Status
Done

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions