Skip to content

LanguageModelSession

Manages conversation state and provides all generation methods — text, streaming, structured, and JSON Schema.

Constructor

ts
new LanguageModelSession(options?: {
  instructions?: string;
  model?: SystemLanguageModel;
  tools?: Tool[];
})
ParameterDefaultDescription
instructionsundefinedSystem prompt for the session
modelDefault modelA configured SystemLanguageModel
tools[]Tools available during generation

Methods

respond()

Generate a text response.

ts
respond(prompt: string, options?: {
  options?: GenerationOptions
}): Promise<string>

respondWithSchema()

Generate structured output matching a GenerationSchema.

ts
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.

ts
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.

ts
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.

ts
cancel(): void

dispose()

Release the native session. Access transcript before calling this.

ts
dispose(): void

Properties

isResponding

ts
readonly isResponding: boolean

true while a generation request is in progress.

transcript

ts
readonly transcript: Transcript

The session's conversation history. See Transcript.

Static Methods

fromTranscript()

Create a new session from a saved transcript.

ts
static fromTranscript(transcript: Transcript, options?: {
  instructions?: string;
  model?: SystemLanguageModel;
  tools?: Tool[];
}): LanguageModelSession

Released under the Apache 2.0 License.