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
Role
Who authored the message.
public LiteRtMessageRole Role { get; }
Property Value
Text
The text content, or null when the message carries only tool calls or tool results.
public string? Text { get; }
Property Value
ToolCalls
Tool calls the assistant requested on a Model turn (empty otherwise).
public IReadOnlyList<LiteRtToolCall> ToolCalls { get; }
Property Value
ToolResults
Tool outputs carried by a Tool turn (empty otherwise).
public IReadOnlyList<LiteRtToolResult> ToolResults { get; }
Property Value
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
jsonstring
Returns
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
textstringtoolCallsIReadOnlyList<LiteRtToolCall>
Returns
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
messagesIEnumerable<LiteRtMessage>
Returns
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
textstring
Returns
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
resultsLiteRtToolResult[]
Returns
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
resultsIEnumerable<LiteRtToolResult>
Returns
User(string)
A user message.
public static LiteRtMessage User(string text)
Parameters
textstring
Returns
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
textstringattachmentsIReadOnlyList<LiteRtAttachment>