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
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.
Puts systemInstruction and updates generationConfig in params with prompt based on json_schema and notes.
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.
Updates params with prompt for retrying a request.
Make request to Gemini API
Options
:api_key(String.t/0) - Required. Gemini API key:http_client(atom/0) - Any module that followsReq.post/2interface The default value isReq.:http_options(keyword/0) - Options passed tohttp_client.post/2The 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".