Table of Contents

Class LiteRtMessage

Namespace
LiteRtLmSharp
Assembly
LiteRtLmSharp.dll

One message in a conversation history, used to restore a conversation: pass a list of these as History and the engine re-prefills them when the conversation is created. Build them with the factory methods (User(string), Model(string, IReadOnlyList<LiteRtToolCall>?), Tool(IEnumerable<LiteRtToolResult>), System(string)); capture an assistant turn from a live reply with ToMessage(bool).

The LiteRT-LM C API exposes no way to read a conversation's history back, so the round-trip is caller-owned: record each turn as it happens, persist with Serialize(IEnumerable<LiteRtMessage>), and reload with Deserialize(string) (or feed the serialized JSON straight to HistoryJson). Restoring replays the history through prefill; it is not a zero-cost KV-cache snapshot.

public sealed class LiteRtMessage
Inheritance
LiteRtMessage
Inherited Members

Properties

Attachments

Image/audio attachments on a User turn (empty otherwise). Restored into the history alongside the text and re-encoded through prefill, so a later turn can refer back to earlier images/audio. Requires the engine loaded with the matching VisionBackend / AudioBackend.

public IReadOnlyList<LiteRtAttachment> Attachments { get; }

Property Value

IReadOnlyList<LiteRtAttachment>

Role

Who authored the message.

public LiteRtMessageRole Role { get; }

Property Value

LiteRtMessageRole

Text

The text content, or null when the message carries only tool calls or tool results.

public string? Text { get; }

Property Value

string

ToolCalls

Tool calls the assistant requested on a Model turn (empty otherwise).

public IReadOnlyList<LiteRtToolCall> ToolCalls { get; }

Property Value

IReadOnlyList<LiteRtToolCall>

ToolResults

Tool outputs carried by a Tool turn (empty otherwise).

public IReadOnlyList<LiteRtToolResult> ToolResults { get; }

Property Value

IReadOnlyList<LiteRtToolResult>

Methods

Deserialize(string)

Parses a messages-array JSON (as produced by Serialize(IEnumerable<LiteRtMessage>)) back into a typed history. "assistant" is normalized to Model. Throws ArgumentException when the JSON is not an array, or when a message carries an unknown or missing role — rejecting it beats silently misattributing the turn. Content parts of unknown types are skipped (text, tool responses, tool calls, and image/audio attachments are preserved).

public static IReadOnlyList<LiteRtMessage> Deserialize(string json)

Parameters

json string

Returns

IReadOnlyList<LiteRtMessage>

Model(string, IReadOnlyList<LiteRtToolCall>?)

An assistant ("model") message, optionally carrying the tool calls it requested.

public static LiteRtMessage Model(string text, IReadOnlyList<LiteRtToolCall>? toolCalls = null)

Parameters

text string
toolCalls IReadOnlyList<LiteRtToolCall>

Returns

LiteRtMessage

Serialize(IEnumerable<LiteRtMessage>)

Serializes a history to the LiteRT-LM messages-array JSON — the same format HistoryJson accepts and Deserialize(string) reads. Persist this string to restore the conversation later.

public static string Serialize(IEnumerable<LiteRtMessage> messages)

Parameters

messages IEnumerable<LiteRtMessage>

Returns

string

System(string)

A system message. Prefer SystemMessage for the active system prompt — set the system prompt in one place only (the native side prepends SystemMessage before the history, so doing both yields two system turns).

public static LiteRtMessage System(string text)

Parameters

text string

Returns

LiteRtMessage

Tool(params LiteRtToolResult[])

A tool-results message, carrying the outputs you executed for the assistant's tool calls.

public static LiteRtMessage Tool(params LiteRtToolResult[] results)

Parameters

results LiteRtToolResult[]

Returns

LiteRtMessage

Tool(IEnumerable<LiteRtToolResult>)

A tool-results message, carrying the outputs you executed for the assistant's tool calls.

public static LiteRtMessage Tool(IEnumerable<LiteRtToolResult> results)

Parameters

results IEnumerable<LiteRtToolResult>

Returns

LiteRtMessage

User(string)

A user message.

public static LiteRtMessage User(string text)

Parameters

text string

Returns

LiteRtMessage

User(string, IReadOnlyList<LiteRtAttachment>)

A user message carrying image/audio attachments (a multimodal turn). The attachments are restored into the history alongside the text, so a later turn can refer back to them. Requires the engine loaded with the matching vision/audio backend on a multimodal model.

public static LiteRtMessage User(string text, IReadOnlyList<LiteRtAttachment> attachments)

Parameters

text string
attachments IReadOnlyList<LiteRtAttachment>

Returns

LiteRtMessage