EN 中文
Back to Overview

LiteRtLm_Result

Inference result wrapper structure. Passes incremental text, complete JSON data, and engine status within the callback function.

01. Memory Validity Timeline (Crucial Contract)

sequenceDiagram participant DLL as Wrapper DLL Internal participant App as User Callback Function DLL->>App: Call LiteRtLmCallback(Result) Note over App: [text_chunk Pointer Valid Zone] App->>App: Must perform string copy here (strcpy/FString) App-->>DLL: Callback Returns (Exit Scope) Note over DLL: [Memory pointed to is released/reused] Note right of App: NEVER access text_chunk outside callback!
Memory Contract Warning

The text_chunk and full_json_chunk members point to internal circular buffers within the DLL. Once the callback function finishes execution and returns, these memory addresses may be overwritten immediately.

02. Member Variable Details

Member Type Description
text_chunk const char* Incremental text fragment.
Contains only the characters generated in this step. Used for streaming typewriter effects.
full_json_chunk const char* Complete response JSON.
Contains full information including tool_calls, content, and stats. Recommended to parse this field at the end of generation for structured data.
bIsDone int Termination flag.
1 indicates inference is complete (or interrupted); 0 means generation is still in progress.
tokens_per_sec float Instantaneous generation rate.
Tokens generated per second, used for real-time UI performance monitoring.

03. Unreal Engine (UE5) Integration Example

void OnInferenceResult(LiteRtLm_Result Result, void* UserPtr) {
    // 1. Memory Safety: Convert to engine-safe type immediately (Deep Copy)
    FString ChunkString(UTF8_TO_TCHAR(Result.text_chunk));
    
    // 2. Schedule to Game Thread UI
    AsyncTask(ENamedThreads::GameThread, [ChunkString, bDone = Result.bIsDone]() {
        // Update UI Text Block...
        if (bDone) {
            UE_LOG(LogTemp, Log, TEXT("Generation Finished"));
        }
    });
}