Calling Google Gemini

File: gemini.lisp

This program calls Google’s Gemini API and parses the JSON reply in about thirty lines, including error handling and the demo call.

The code

 1 ;; sbcl --script gemini.lisp   (needs Quicklisp + GOOGLE_API_KEY)
 2 #-quicklisp (load "~/quicklisp/setup.lisp")
 3 (ql:quickload :com.inuoe.jzon :silent t)
 4 
 5 (defun gemini (prompt &optional (model "gemini-3.5-flash-lite"))
 6   (let* ((base
 7           "https://generativelanguage.googleapis.com/v1beta/models/")
 8          (url (format nil "~a~a:generateContent" base model))
 9          (key (or (uiop:getenv "GOOGLE_API_KEY")
10                   (error "GOOGLE_API_KEY is not set")))
11          (body (format nil
12                        "{\"contents\":[{\"parts\":[{\"text\":~a}]}]}"
13                        (com.inuoe.jzon:stringify prompt)))
14          (j (com.inuoe.jzon:parse
15              (uiop:run-program
16               (list "curl" "-s" url
17                     "-H" "Content-Type: application/json"
18                     "-H" (format nil "x-goog-api-key: ~a" key)
19                     "-d" body)
20               :output :string)))
21          (candidates (gethash "candidates" j)))
22     (unless candidates
23       (error "Gemini: ~a"
24              (let ((e (gethash "error" j)))
25                (if e (gethash "message" e) "no candidates in reply"))))
26     (gethash "text"
27              (aref (gethash "parts"
28                             (gethash "content" (aref candidates 0)))
29                    0))))
30 
31 (print (gemini "what is 1 + 10?"))

How it works

  • #-quicklisp reads as “if Quicklisp is not loaded”: run as a script, the file loads ~/quicklisp/setup.lisp first. With this idiom the same file works both as a script and in a REPL that already has Quicklisp.
  • com.inuoe.jzon is a fast JSON library. jzon:parse turns JSON into hash tables and vectors; jzon:stringify turns a Lisp string into an escaped JSON string literal.
  • format builds the request body, and jzon:stringify escapes the prompt. Pasting user text into JSON by hand breaks on the first quote or backslash in the prompt; the one stringify call prevents that.
  • curl -s POSTs to the generateContent endpoint with the API key in the x-goog-api-key header. uiop:getenv reads the key from the environment, and the (or ... (error ...)) idiom fails at once with a clear message when the key is missing, instead of sending the header x-goog-api-key: NIL.
  • A successful reply nests the text at candidates[0].content.parts[0].text. The final expression reaches it with gethash and aref.
  • A failed request (bad key, unknown model, rate limit) returns JSON with an error object instead of candidates. The unless check turns that into a Lisp error that carries Google’s own message, for example Gemini: models/no-such-model is not found for API version v1beta....

Running it

1 $ export GOOGLE_API_KEY=...   # from https://aistudio.google.com
2 $ sbcl --script gemini.lisp

Actual output (the wording varies between runs, as with any language model):

1 "1 + 10 = 11"

Using it

Pass a second argument to switch models: (gemini "summarize this file" "gemini-3.5-flash"). The function works well with later examples in the book: summarize the output of the web-scraping chapter, or answer questions about documents ranked by the BM25 chapter. Failures signal ordinary Lisp errors, so you can wrap calls in handler-case to retry after a rate limit or fall back to the local Ollama model in the next chapter.