Table of Contents

Class ContentExtensions

Namespace
GenerativeAI
Assembly
GenerativeAI.dll

Provides extension methods for the Content class, allowing operations such as adding parts, handling inline and remote files, adding text, and extracting code or JSON blocks.

public static class ContentExtensions
Inheritance
ContentExtensions
Inherited Members

Methods

AddInlineData(Content, string, string)

Adds inline data (for images, audio, etc.) as a new Part to this Content.

public static void AddInlineData(this Content content, string data, string mimeType)

Parameters

content Content

The Content to which the inline data will be added.

data string

The base64-encoded data to include.

mimeType string

The MIME type of the data.

AddInlineFile(Content, string, string)

Adds a file as inline base64-encoded data to the content.

public static void AddInlineFile(this Content content, string filePath, string role)

Parameters

content Content

The content to add the file to.

filePath string

The path to the file to add.

role string

The role parameter (currently unused but kept for interface compatibility).

Exceptions

ArgumentException

Thrown when content is null.

AddPart(Content, Part)

Adds a single Part to this Content.

public static void AddPart(this Content content, Part part)

Parameters

content Content

The Content to which the Part will be added.

part Part

The Part to add.

AddParts(Content, IEnumerable<Part>)

Adds multiple Part objects to this Content.

public static void AddParts(this Content content, IEnumerable<Part> parts)

Parameters

content Content

The Content to which the Part objects will be added.

parts IEnumerable<Part>

The collection of Part objects to add.

AddRemoteFile(Content, RemoteFile)

Adds a remote file to the GenerateContentRequest using its URL and MIME type.

public static void AddRemoteFile(this Content request, RemoteFile file)

Parameters

request Content

The GenerateContentRequest to which the remote file will be added.

file RemoteFile

The RemoteFile to be added.

AddRemoteFile(Content, string, string)

Adds a remote file reference as a new Part to this Content.

public static void AddRemoteFile(this Content content, string fileUri, string mimeType)

Parameters

content Content

The Content to which the remote file reference will be added.

fileUri string

The URL of the remote file.

mimeType string

The MIME type of the remote file.

AddText(Content, string)

Adds a single text Part to this Content.

public static void AddText(this Content content, string text)

Parameters

content Content

The Content instance.

text string

The text to add.

ExtractCodeBlocks(Content)

Extracts all CodeBlock from the text Parts of Content object.

public static List<CodeBlock> ExtractCodeBlocks(this Content content)

Parameters

content Content

The Content from which to extract code blocks.

Returns

List<CodeBlock>

A list of CodeBlock instances found within the Content.

ExtractJsonBlocks(Content)

Extracts all JSON string from the text Parts of Content object.

public static List<JsonBlock> ExtractJsonBlocks(this Content content)

Parameters

content Content

The Content from which to extract code blocks.

Returns

List<JsonBlock>

A list of CodeBlock instances found within the Content.

ToObject<T>(Content, JsonSerializerOptions?)

Converts the JSON content of a Content object into an instance of the specified type.

public static T? ToObject<T>(this Content content, JsonSerializerOptions? options = null) where T : class

Parameters

content Content

The Content object containing JSON data to be converted.

options JsonSerializerOptions

The JsonSerializerOptions to customize the deserialization process, or null to use default options.

Returns

T

An instance of type T if conversion succeeds, or null if no valid JSON data is found or deserialization fails.

Type Parameters

T

The target type to which the JSON content will be deserialized. Must be a class.

Remarks

For NativeAOT/Trimming JsonSerializerOptions are required, you can ignore it if you have already specified a resolver in