MyNixOS website logo
Description

Auto-generated Gemini API Client for Haskell.

Unofficial Haskell client for Google GenAI API, including Gemini.

This library is auto-generated from the OpenAPI specification, using openapi-generator.

Google API reference: https://ai.google.dev/api

Haskell Google GenAI Client

Unofficial low-level Haskell client for the Google Gemini API (also known as GenAI API or Generative Language API).

This library provides a type-safe way to interact with Google's Gemini API services directly from your Haskell code.

Note

  • The code is automatically generated from the OpenAPI specification using openapi-generator.
  • APIs include text/image/audio generation, embeddings, model tuning, semantic retrieval API, and more. See Google API reference for full details.

Development

First, run nix develop to enter the development shell. Non-Nix users should install the dependencies such as openapi-generator and pre-commit.

# Generate the Haskell client code from the OpenAPI specification
./run-generator.sh
# Format the code and check for linting issues
pre-commit run --all-files
# Build the project
cabal build
# Run the tests
cabal test

Example Usage

Here is a working example that uses 'generateContent' API to generate text using Gemini model:

{-# LANGUAGE OverloadedStrings #-}
import GenAI.Client
import Network.HTTP.Client (responseBody)
import Network.HTTP.Client.TLS (newTlsManager)
import Network.HTTP.Types (QueryItem)
import Data.ByteString.Lazy qualified as LBS

apiKeyParam :: QueryItem
apiKeyParam = ("key", Just "<GEMINI_API_KEY>")

model :: Model2
model = Model2 "gemini-2.0-flash-001"

requestBody :: GenerateContentRequest
requestBody = mkGenerateContentRequest [content] "gemini-2.0-flash-001" where
  content = mkContent { contentParts = Just [textPart] }
  textPart = mkPart { partText = Just query }
  query = "What is the capital of Korea?"

request :: ClientRequest GenerateContent MimeJSON GenerateContentResponse MimeJSON
request = flip addQuery [apiKeyParam] $ flip setBodyParam requestBody $ generateContent model

main :: IO ()
main = do
  config <- withStdoutLogging =<< newConfig
  manager <- newTlsManager
  response <- dispatchLbs manager config request
  LBS.putStr $ responseBody response

Running code above will produce:

{
  "candidates": [
    {
      "content": {
        "parts": [
          {
            "text": "The capital of South Korea is **Seoul**.\n"
          }
        ],
        "role": "model"
      },
      "finishReason": "STOP",
      "avgLogprobs": -0.024313041940331459
    }
  ],
  "usageMetadata": {
    "promptTokenCount": 7,
    "candidatesTokenCount": 8,
    "totalTokenCount": 15,
    "promptTokensDetails": [
      {
        "modality": "TEXT",
        "tokenCount": 7
      }
    ],
    "candidatesTokensDetails": [
      {
        "modality": "TEXT",
        "tokenCount": 8
      }
    ]
  },
  "modelVersion": "gemini-2.0-flash-001",
  "responseId": "ZQc_aPa7JcXWnvgP3LDNsAw"
}
Metadata

Version

0.1.0

License

Platforms (76)

    Darwin
    FreeBSD
    Genode
    GHCJS
    Linux
    MMIXware
    NetBSD
    none
    OpenBSD
    Redox
    Solaris
    WASI
    Windows
Show all
  • aarch64-darwin
  • aarch64-freebsd
  • aarch64-genode
  • aarch64-linux
  • aarch64-netbsd
  • aarch64-none
  • aarch64-windows
  • aarch64_be-none
  • arm-none
  • armv5tel-linux
  • armv6l-linux
  • armv6l-netbsd
  • armv6l-none
  • armv7a-linux
  • armv7a-netbsd
  • armv7l-linux
  • armv7l-netbsd
  • avr-none
  • i686-cygwin
  • i686-freebsd
  • i686-genode
  • i686-linux
  • i686-netbsd
  • i686-none
  • i686-openbsd
  • i686-windows
  • javascript-ghcjs
  • loongarch64-linux
  • m68k-linux
  • m68k-netbsd
  • m68k-none
  • microblaze-linux
  • microblaze-none
  • microblazeel-linux
  • microblazeel-none
  • mips-linux
  • mips-none
  • mips64-linux
  • mips64-none
  • mips64el-linux
  • mipsel-linux
  • mipsel-netbsd
  • mmix-mmixware
  • msp430-none
  • or1k-none
  • powerpc-linux
  • powerpc-netbsd
  • powerpc-none
  • powerpc64-linux
  • powerpc64le-linux
  • powerpcle-none
  • riscv32-linux
  • riscv32-netbsd
  • riscv32-none
  • riscv64-linux
  • riscv64-netbsd
  • riscv64-none
  • rx-none
  • s390-linux
  • s390-none
  • s390x-linux
  • s390x-none
  • vc4-none
  • wasm32-wasi
  • wasm64-wasi
  • x86_64-cygwin
  • x86_64-darwin
  • x86_64-freebsd
  • x86_64-genode
  • x86_64-linux
  • x86_64-netbsd
  • x86_64-none
  • x86_64-openbsd
  • x86_64-redox
  • x86_64-solaris
  • x86_64-windows