class LittleGhost::Providers::Gemini
Connects a Model to Google’s Gemini generateContent API.
Requests send messages, Tool definitions, attachments, and model settings to the configured Google endpoint. The adapter reads its API key from trusted configuration, honors cancellation and deadlines, and emits LittleGhost StreamEvents. HTTP and response-shape failures become ProviderError subclasses. It uses the built-in HTTP client and does not require Google’s SDK.
Gemini attaches an opaque thoughtSignature to a function-call response part and expects it echoed back on the next request’s matching functionCall. The adapter preserves these opaque values in the response Message metadata under gemini/thought_signatures, keyed by tool-call ID. Preserve the full assistant Message, including this provider replay metadata, when storing or rebuilding conversation history. Signatures survive provider recreation and Message#without_reasoning and replay with their original calls.
Tool results use the function name from their preceding ToolUse in the request history. Keep each tool call before its result when restoring history. Results without a preceding call retain their tool-use ID as the name.
Attributes
Provider-owned model identifier.
Public Class Methods
# File lib/little_ghost/providers/gemini.rb, line 43 def initialize(api_key:, model:, base_url: DEFAULT_BASE_URL, open_timeout: 10, read_timeout: 120, max_response_bytes: Support::HTTPClient::DEFAULT_MAX_RESPONSE_BYTES, transport: nil, **) raise CredentialError, "Gemini api_key is required" if api_key.to_s.empty? @api_key = api_key @model = model @transport = transport || Support::HTTPClient.new(base_url:, open_timeout:, read_timeout:, max_response_bytes:) end
Creates a Gemini generateContent client for model.
Source
# File lib/little_ghost/providers/gemini.rb, line 35 def self.request_options = %i[max_response_bytes open_timeout read_timeout].freeze
Request policy supported by Gemini and Vertex AI HTTP clients.
Public Instance Methods
Source
# File lib/little_ghost/providers/gemini.rb, line 74 def capabilities(metadata: {}) ModelCapabilities.new(native_structured_output: true, tools: true, tool_choice: true, supported_parameters: metadata[:supported_parameters]) end
Reports Tool and structured-output support from model metadata.
# File lib/little_ghost/providers/gemini.rb, line 53 def stream(request) return enum_for(__method__, request) unless block_given? parser = Support::SSEParser.new normalizer = Normalizer.new(model:) @transport.stream( path: endpoint, headers: request_headers(request), body: JSON.generate(request_body(request)), cancellation_token: request.cancellation_token, deadline: request.deadline ) do |chunk| parser.<<(chunk).each { |data| normalizer.consume(JSON.parse(data)).each { |event| yield event } } end parser.finish.each { |data| normalizer.consume(JSON.parse(data)).each { |event| yield event } } normalizer.finish.each { |event| yield event } rescue JSON::ParserError => error raise ProtocolError, "Google returned invalid JSON: #{error.message}" end
Streams normalized events for request.