class

Fm::Session

Inherits Reference < Object

A session that interacts with the language model.

A session maintains conversation context between requests, enabling multi-turn conversations. Create a new session for each conversation or reuse for multi-turn dialogue.

model = Fm::SystemLanguageModel.new
session = Fm::Session.new(model)
response = session.respond("Hello!")
puts response.content

Constructors

from_transcript(model : SystemLanguageModel, transcript_json : String, *, instructions : String | Nil = nil, tools : Array(Tool) | Nil = nil, adapters : Array(Adapter) | Nil = nil) : self

Creates a session from a transcript JSON string.

Optionally restores instructions, tools, and adapters so the resumed session behaves identically to the original.

json = session.transcript_json
restored = Fm::Session.from_transcript(model, json,
  instructions: "Be helpful.",
  tools: [my_tool],
  adapters: [my_adapter],
)
Source
new(model : SystemLanguageModel, *, instructions : String | Nil = nil, tools : Array(Tool) | Nil = nil, adapters : Array(Adapter) | Nil = nil)

Creates a new session with optional instructions, tools, and/or adapters.

session = Fm::Session.new(model)
session = Fm::Session.new(model, instructions: "Be helpful.")
session = Fm::Session.new(model, tools: [my_tool])
session = Fm::Session.new(model, adapters: [my_adapter])
session = Fm::Session.new(model, instructions: "Be helpful.", tools: [my_tool], adapters: [my_adapter])
Source

Instance methods

cancel

Cancels an ongoing stream operation.

Source
finalize
Source
prewarm(prompt_prefix : String | Nil = nil) : Nil

Prewarms the model with an optional prompt prefix.

Source
respond(prompt : String, options : GenerationOptions = GenerationOptions.default) : Response

Sends a prompt and waits for the complete response.

response = session.respond("What is the capital of France?")
puts response.content
Source
respond(prompt : String, options : GenerationOptions = GenerationOptions.default, *, timeout : Time::Span) : Response

Sends a prompt and waits for the response, with a timeout.

Source
respond_json(prompt : String, schema_json : String, options : GenerationOptions = GenerationOptions.default) : String

Sends a prompt and returns a structured JSON response matching a schema.

schema = %({"type":"object","properties":{"name":{"type":"string"}},"required":["name"]})
json = session.respond_json("Generate a person", schema)
Source
respond_structured(type : T.class, prompt : String, options : GenerationOptions = GenerationOptions.default) : T forall T

Sends a prompt and returns a deserialized structured response.

The type T must include JSON::Serializable and Fm::Generable.

struct Person
  include JSON::Serializable
  include Fm::Generable

  getter name : String
  getter age : Int32
end

person = session.respond_structured(Person, "Generate a fictional person")
puts person.name
Source
responding?

Checks if the session is currently generating a response.

Source
stream(prompt : String, options : GenerationOptions = GenerationOptions.default, &block : String -> ) : Nil

Sends a prompt and streams the response via a block.

The block receives each text chunk as it arrives from the model. This method blocks until streaming is complete.

session.stream("Tell me a story") do |chunk|
  print chunk
end
Source
stream_json(prompt : String, schema_json : String, options : GenerationOptions = GenerationOptions.default, &block : String -> ) : Nil

Streams a structured JSON response matching a schema.

Source
transcript_json

Gets the session transcript as a JSON string.

Source