InstructorLite.Adapters.Gemini (instructor_lite v1.3.0)

Copy Markdown View Source

Gemini adapter.

This adapter is implemented using Text generation endpoint configured for structured output

Params

params argument should be shaped as a models.GenerateBody request body

Example

InstructorLite.instruct(
  %{contents: [%{role: "user", parts: [%{text: "John is 25yo"}]}]},
  response_model: %{name: :string, age: :integer},
  json_schema: %{
    type: "object",
    required: [:age, :name],
    properties: %{name: %{type: "string"}, age: %{type: "integer"}}
  },
  adapter: InstructorLite.Adapters.Gemini,
  adapter_context: [
    model: "gemini-1.5-flash-8b",
    api_key: Application.fetch_env!(:instructor_lite, :gemini_key)
  ]
)
{:ok, %{name: "John", age: 25}}

Specifying model

Note how, unlike other adapters, the Gemini adapter expects model under adapter_context.

JSON Schema

The adapter uses Gemini's responseJsonSchema field which accepts standard JSON Schema, including features like $ref, $defs, title, and additionalProperties that Gemini's older responseSchema field does not support.

Chat Completions Compatible

If any of the limitations above are important to you, know that Gemini also provides a chat completion endpoint. You can use it with the InstructorLite.Adapters.ChatCompletionsCompatible adapter.

Summary

Functions

Parse text generation endpoint response in search of plain text output.

Puts systemInstruction and updates generationConfig in params with prompt based on json_schema and notes.

Parse text generation endpoint response.

Updates params with prompt for retrying a request.

Make request to Gemini API

Functions

find_output(response, opts)

Parse text generation endpoint response in search of plain text output.

Can return:

  • {:ok, text_output} on success.
  • {:error, :refusal, prompt_feedback} if request was blocked.
  • {:error, :unexpected_response, response} if response is of unexpected shape.

initial_prompt(params, opts)

Puts systemInstruction and updates generationConfig in params with prompt based on json_schema and notes.

parse_response(response, opts)

Parse text generation endpoint response.

Can return:

  • {:ok, parsed_json} on success.
  • {:error, :refusal, prompt_feedback} if request was blocked.
  • {:error, :unexpected_response, response} if response is of unexpected shape.

retry_prompt(params, resp_params, errors, response, opts)

Updates params with prompt for retrying a request.

send_request(params, opts)

Make request to Gemini API

Options

  • :api_key (String.t/0) - Required. Gemini API key

  • :http_client (atom/0) - Any module that follows Req.post/2 interface The default value is Req.

  • :http_options (keyword/0) - Options passed to http_client.post/2 The default value is [receive_timeout: 60000].

  • :url (String.t/0) - API endpoint to use for sending requests The default value is "https://generativelanguage.googleapis.com/v1beta/models/{model}:generateContent".

  • :model (String.t/0) - Gemini model The default value is "gemini-3.5-flash-lite".