Table of Contents

Class MicrosoftExtensions

Namespace
GenerativeAI.Microsoft.Extensions
Assembly
GenerativeAI.Microsoft.dll

Provides extension methods for interoperability between Google_GenerativeAI and Microsoft.Extensions.AI models.

public static class MicrosoftExtensions
Inheritance
MicrosoftExtensions
Inherited Members

Methods

AsEmbeddingGenerator(IPlatformAdapter, string)

Creates a GenerativeAIEmbeddingGenerator from a platform adapter and optional model name.

public static GenerativeAIEmbeddingGenerator AsEmbeddingGenerator(this IPlatformAdapter adapter, string modelName = "models/gemini-embedding-001")

Parameters

adapter IPlatformAdapter

The platform adapter to use for API calls.

modelName string

The name of the embedding model to use. Defaults to "models/gemini-embedding-001".

Returns

GenerativeAIEmbeddingGenerator

A new instance of GenerativeAIEmbeddingGenerator.

AsEmbeddingGenerator(string, string)

Creates a GenerativeAIEmbeddingGenerator from an API key and optional model name.

public static GenerativeAIEmbeddingGenerator AsEmbeddingGenerator(this string apiKey, string modelName = "models/gemini-embedding-001")

Parameters

apiKey string

The API key for authenticating with Google AI.

modelName string

The name of the embedding model to use. Defaults to "models/gemini-embedding-001".

Returns

GenerativeAIEmbeddingGenerator

A new instance of GenerativeAIEmbeddingGenerator.

GetFunctions(ChatResponse)

Retrieves the first occurrence of a function call content from the provided Microsoft.Extensions.AI.ChatResponse message contents.

public static List<FunctionCallContent>? GetFunctions(this ChatResponse response)

Parameters

response ChatResponse

The Microsoft.Extensions.AI.ChatResponse object containing the messages and their associated contents.

Returns

List<FunctionCallContent>

A Microsoft.Extensions.AI.FunctionCallContent object if a function call is present; otherwise, null.

PromoteOversizedAttachmentsAsync(IList<ChatMessage>, GenerativeModel, CancellationToken)

Auto-promotes a Microsoft.Extensions.AI.DataContent to the Files API when its payload exceeds Gemini's 20 MB inline_data limit. Walks the message list, uploads any oversized binary attachments via model's Files client, and rewrites those entries to point at the resulting remote file URI. Cheap when nothing is oversized — the loop just short-circuits. Call this BEFORE ToGenerateContentRequest(IEnumerable<ChatMessage>, ChatOptions?) so the converted parts use FileData instead of InlineData.

public static Task PromoteOversizedAttachmentsAsync(this IList<ChatMessage> chatMessages, GenerativeModel model, CancellationToken cancellationToken = default)

Parameters

chatMessages IList<ChatMessage>
model GenerativeModel
cancellationToken CancellationToken

Returns

Task

ToAiContents(List<Part>?, ChatOptions?)

Transforms a list of Part objects into a collection of Microsoft.Extensions.AI.AIContent instances.

public static IList<AIContent> ToAiContents(this List<Part>? parts, ChatOptions? options = null)

Parameters

parts List<Part>

The list of Part objects to be converted.

options ChatOptions

Optional chat options for context-aware transformations.

Returns

IList<AIContent>

A collection of Microsoft.Extensions.AI.AIContent instances derived from the provided Part objects, or null if the input list is null.

ToChatMessages(List<Content>?, ChatOptions?)

Converts a GenerateContentResponse instance into a Microsoft.Extensions.AI.ChatMessage.

public static IEnumerable<ChatMessage> ToChatMessages(this List<Content>? contents, ChatOptions? options = null)

Parameters

contents List<Content>

The Content to be transformed into a Microsoft.Extensions.AI.ChatMessage.

options ChatOptions

Optional chat options used while converting the response (for example, tool schemas).

Returns

IEnumerable<ChatMessage>

A Microsoft.Extensions.AI.ChatMessage representing the data contained in the GenerateContentResponse.

ToChatResponse(GenerateContentResponse?, ChatOptions?)

Transforms a GenerateContentResponse into a Microsoft.Extensions.AI.ChatResponse.

public static ChatResponse? ToChatResponse(this GenerateContentResponse? response, ChatOptions? options = null)

Parameters

response GenerateContentResponse

The GenerateContentResponse instance to convert.

options ChatOptions

Optional chat options used while converting the response (for example, tool schemas).

Returns

ChatResponse

A Microsoft.Extensions.AI.ChatResponse object if the transformation is successful; otherwise, null.

ToChatResponseUpdate(GenerateContentResponse?, ChatOptions?)

Converts a GenerateContentResponse instance into a Microsoft.Extensions.AI.ChatResponseUpdate object.

public static ChatResponseUpdate ToChatResponseUpdate(this GenerateContentResponse? response, ChatOptions? options = null)

Parameters

response GenerateContentResponse

The input GenerateContentResponse to transform into a chat response update.

options ChatOptions

Optional chat options used while converting the response (for example, tool schemas).

Returns

ChatResponseUpdate

A new Microsoft.Extensions.AI.ChatResponseUpdate object reflecting the data in the provided GenerateContentResponse.

ToGeminiEmbedContentRequest(IEnumerable<string>, EmbeddingGenerationOptions?)

Converts a collection of strings and optional embedding generation settings into an EmbedContentRequest.

public static EmbedContentRequest ToGeminiEmbedContentRequest(IEnumerable<string> values, EmbeddingGenerationOptions? options)

Parameters

values IEnumerable<string>

The collection of strings to be embedded.

options EmbeddingGenerationOptions

Optional embedding generation settings, such as model identification.

Returns

EmbedContentRequest

An EmbedContentRequest instance created from the provided values and options.

ToGenerateContentRequest(IEnumerable<ChatMessage>, ChatOptions?)

Converts a collection of Microsoft.Extensions.AI.ChatMessage objects into a GenerateContentRequest instance.

public static GenerateContentRequest ToGenerateContentRequest(this IEnumerable<ChatMessage> chatMessages, ChatOptions? options = null)

Parameters

chatMessages IEnumerable<ChatMessage>

The collection of chat messages to transform.

options ChatOptions

Optional settings to customize the generation process.

Returns

GenerateContentRequest

A GenerateContentRequest instance derived from the provided chat messages.

ToGeneratedEmbeddings(EmbedContentRequest, EmbedContentResponse)

Converts an EmbedContentRequest and an EmbedContentResponse into a Microsoft.Extensions.AI.GeneratedEmbeddings<TEmbedding> instance containing embeddings of type Embedding<float>.

public static GeneratedEmbeddings<Embedding<float>> ToGeneratedEmbeddings(EmbedContentRequest request, EmbedContentResponse response)

Parameters

request EmbedContentRequest

The request containing the embedding parameters and metadata.

response EmbedContentResponse

The response containing the embedding result.

Returns

GeneratedEmbeddings<Embedding<float>>

A Microsoft.Extensions.AI.GeneratedEmbeddings<TEmbedding> instance containing the generated embeddings and associated metadata.

ToGenerationConfig(ChatOptions?)

Maps Microsoft.Extensions.AI.ChatOptions into a GenerationConfig object used by GenerativeAI.

public static GenerationConfig? ToGenerationConfig(this ChatOptions? options)

Parameters

options ChatOptions

The chat options defining parameters for content generation.

Returns

GenerationConfig

A GenerationConfig instance or null, depending on the input.

ToJsonNodeResponse(object?)

Converts a given response object into a JsonNode representation.

public static JsonNode ToJsonNodeResponse(this object? response)

Parameters

response object

The response object to convert. It can be of type Microsoft.Extensions.AI.FunctionResultContent, JsonNode, or other compatible types.

Returns

JsonNode

A JsonNode instance representing the response. Throws an exception if the response is not a valid JSON node.

ToPart(AIContent, ChatOptions?)

Converts an Microsoft.Extensions.AI.AIContent instance into a Part object.

public static Part? ToPart(this AIContent content, ChatOptions? options = null)

Parameters

content AIContent

The AI content to transform into a part object.

options ChatOptions

Optional chat options for context-aware transformations.

Returns

Part

A Part object representing the given AI content, or null if the content type is unsupported.

WithTaskType(EmbeddingGenerationOptions, TaskType)

Sets the task type for embedding generation.

public static EmbeddingGenerationOptions WithTaskType(this EmbeddingGenerationOptions options, TaskType taskType)

Parameters

options EmbeddingGenerationOptions

The embedding generation options to modify.

taskType TaskType

The task type for which the embedding will be optimized.

Returns

EmbeddingGenerationOptions

The modified embedding generation options.