LanguageModelSession
Manages conversation state and provides all generation methods — text, streaming, structured, and JSON Schema.
Constructor
new LanguageModelSession(options?: {
instructions?: string;
model?: SystemLanguageModel;
tools?: Tool[];
})| Parameter | Default | Description |
|---|---|---|
instructions | undefined | System prompt for the session |
model | Default model | A configured SystemLanguageModel |
tools | [] | Tools available during generation |
Methods
respond()
Generate a text response.
respond(prompt: string, options?: {
options?: GenerationOptions
}): Promise<string>respondWithSchema()
Generate structured output matching a GenerationSchema.
respondWithSchema(prompt: string, schema: GenerationSchema, options?: {
options?: GenerationOptions
}): Promise<GeneratedContent>Returns a GeneratedContent with typed property access.
respondWithJsonSchema()
Generate structured output from a JSON Schema object.
respondWithJsonSchema(prompt: string, schema: object, options?: {
options?: GenerationOptions
}): Promise<GeneratedContent>Returns a GeneratedContent with toObject() for the full result.
streamResponse()
Stream a response token-by-token.
streamResponse(prompt: string, options?: {
options?: GenerationOptions
}): AsyncIterable<string>Each yielded string contains only the new tokens since the last iteration.
cancel()
Cancel an in-progress request. Advisory — the response may complete before cancellation takes effect.
cancel(): voiddispose()
Release the native session. Access transcript before calling this.
dispose(): voidProperties
isResponding
readonly isResponding: booleantrue while a generation request is in progress.
transcript
readonly transcript: TranscriptThe session's conversation history. See Transcript.
Static Methods
fromTranscript()
Create a new session from a saved transcript.
static fromTranscript(transcript: Transcript, options?: {
instructions?: string;
model?: SystemLanguageModel;
tools?: Tool[];
}): LanguageModelSession