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
#-quicklispreads as “if Quicklisp is not loaded”: run as a script, the file loads~/quicklisp/setup.lispfirst. With this idiom the same file works both as a script and in a REPL that already has Quicklisp.com.inuoe.jzonis a fast JSON library.jzon:parseturns JSON into hash tables and vectors;jzon:stringifyturns a Lisp string into an escaped JSON string literal.formatbuilds the request body, andjzon:stringifyescapes the prompt. Pasting user text into JSON by hand breaks on the first quote or backslash in the prompt; the onestringifycall prevents that.curl -sPOSTs to thegenerateContentendpoint with the API key in thex-goog-api-keyheader.uiop:getenvreads 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 headerx-goog-api-key: NIL.- A successful reply nests the text at
candidates[0].content.parts[0].text. The final expression reaches it withgethashandaref. - A failed request (bad key, unknown model, rate limit) returns JSON with an
errorobject instead ofcandidates. Theunlesscheck turns that into a Lisp error that carries Google’s own message, for exampleGemini: 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.