Files
agent-framework/dotnet/src/Microsoft.Extensions.AI.Agents.OpenAI/AgentRunResponseExtensions.cs
T
Mark Wallace 624709e5d1 Add some OpenAI and Foundry extension methods (#225)
* Add some OpenAI specific extensions

* Update samples and extension methods

* Apply suggestion from @Copilot

Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>

* Apply suggestion from @Copilot

Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>

* Add extension methods for creating agents using the Assistant API

* Add orchestration sample

* Add orchestration sample

* Sample for the Foundry alignment document

* Address code review feedback

* Rename provider samples

* Sample showing how to get an AI agent for Foundry SDK

* Add OpenAI chat completion based implementation of AIAgent

* Split OpenAI client extension methods by client type

* Remove OpenAIClient extension methods

* Rename AsRunnableAgent

* Fix XML comments

---------

Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
2025-07-31 18:08:17 +00:00

53 lines
2.6 KiB
C#

// Copyright (c) Microsoft. All rights reserved.
using Microsoft.Extensions.AI;
using Microsoft.Extensions.AI.Agents;
using Microsoft.Shared.Diagnostics;
using OpenAI.Chat;
namespace OpenAI;
/// <summary>
/// Provides extension methods for <see cref="AgentRunResponse"/> to extract native OpenAI response objects
/// from the Microsoft Extensions AI Agent framework responses.
/// </summary>
/// <remarks>
/// These extensions enable developers to access the underlying OpenAI SDK objects when working with
/// AI agents that are backed by OpenAI services. The methods extract strongly-typed OpenAI responses
/// from the <see cref="AgentRunResponse.RawRepresentation"/> property, providing a bridge between
/// the Microsoft Extensions AI framework and the native OpenAI SDK types.
/// </remarks>
public static class AgentRunResponseExtensions
{
/// <summary>
/// Extracts a native OpenAI <see cref="ChatCompletion"/> object from an <see cref="AgentRunResponse"/>.
/// </summary>
/// <param name="agentResponse">The agent response containing the raw OpenAI representation.</param>
/// <returns>The native OpenAI <see cref="ChatCompletion"/> object.</returns>
/// <exception cref="ArgumentNullException">Thrown when <paramref name="agentResponse"/> is <see langword="null"/>.</exception>
/// <exception cref="ArgumentException">
/// Thrown when the <see cref="AgentRunResponse.RawRepresentation"/> is not a <see cref="ChatCompletion"/> object.
/// This typically occurs when the agent response was not generated by an OpenAI chat completion service
/// or when the underlying representation has been modified or corrupted.
/// </exception>
/// <remarks>
/// <para>
/// This method provides access to the native OpenAI <see cref="ChatCompletion"/> object that was used
/// to generate the agent response. This is useful when you need to access OpenAI-specific properties
/// or metadata that are not exposed through the Microsoft Extensions AI abstractions.
/// </para>
/// </remarks>
public static ChatCompletion AsChatCompletion(this AgentRunResponse agentResponse)
{
Throw.IfNull(agentResponse);
if (agentResponse.RawRepresentation is ChatResponse chatResponse)
{
return chatResponse.RawRepresentation is ChatCompletion chatCompletion
? chatCompletion
: throw new ArgumentException("ChatResponse.RawRepresentation must be a ChatCompletion");
}
throw new ArgumentException("AgentRunResponse.RawRepresentation must be a ChatResponse");
}
}