Google Gemini API
The Google Gemini API provides developers with access to Google’s family of Gemini Large Language Models (LLMs), representing a significant leap forward in multimodal artificial intelligence. These API offers a streamlined and powerful interface, enabling developers to integrate these advanced reasoning and generation capabilities into their own software, pushing the boundaries of what’s possible in domains ranging from data analysis and content creation to building the next generation of intelligent, context-aware user experiences.
Example Code
In this section, we’ll explore a practical example of interacting with a modern web API by building a client for Google’s Gemini Large Language Model. The following program defines a function named gemini that takes a text prompt and returns the model’s generated response. This involves several common tasks in modern software development: retrieving sensitive information like an API key from environment variables, dynamically constructing a JSON payload according to the API’s specification, setting the correct HTTP headers for authentication and content type, and executing an HTTP POST request. Upon receiving a successful response, the code demonstrates how to parse the returned JSON data to extract the specific piece of information we need: the generated text from a complex, nested data structure. This serves as a simple example for making outbound web requests and handling the data interchange that is central to working with external services.
1 (import :std/net/request
2 :std/text/json)
3
4 (export gemini)
5
6 (def (gemini
7 prompt
8 model: (model "gemini-3-flash-preview")
9 system-prompt: (system-prompt #f))
10 (let ((api-key (getenv "GOOGLE_API_KEY")))
11 (unless api-key
12 (error "GOOGLE_API_KEY environment variable not set."))
13
14 (let* ((headers `(("Content-Type" . "application/json")
15 ("x-goog-api-key" . ,api-key)
16 ("Api-Revision" . "2026-05-20")))
17 (body-fields `(("model" . ,model)
18 ("input" . ,prompt)))
19 (body-fields (if system-prompt
20 (cons `("system_instruction" .
21 ,(list->hash-table
22 `(("parts" . (,(list->hash-table `(("text" . ,system-prompt))))))))
23 body-fields)
24 body-fields))
25 (body-data (list->hash-table body-fields))
26 (body-string (json-object->string body-data))
27 (endpoint "https://generativelanguage.googleapis.com/v1beta/interactions"))
28 (let ((response (http-post endpoint headers: headers data: body-string)))
29 (if (= (request-status response) 200)
30 (let* ((response-json (request-json response))
31 (steps (hash-ref response-json 'steps #f))
32 (model-step
33 (and (pair? steps)
34 (let loop ((ss (reverse steps)))
35 (cond
36 ((null? ss) #f)
37 ((equal? (hash-ref (car ss) 'type #f) "model_output") (car ss))
38 (else (loop (cdr ss)))))))
39 (content (and model-step (hash-ref model-step 'content #f)))
40 (first-content (and (pair? content) (car content))))
41 (if first-content
42 (hash-ref first-content 'text "")
43 (error "Gemini API: no model_output found in response")))
44 (error "Gemini API request failed"
45 status: (request-status response)
46 body: (request-text response)))))))
47
48 ;; (gemini "why is the sky blue? be very concise")
The core logic resides within the gemini function, which uses let* bindings to construct the request. It defines the required HTTP headers, including x-goog-api-key and the Api-Revision header. Next, it builds the body data as a hash table with model, input, and optional system_instruction fields, serializing it into a JSON string using json-object->string. The endpoint points to Google’s Interactions API endpoint (/v1beta/interactions). The network communication is handled by http-post.
Once http-post returns, the program checks the response status. If the request was successful (HTTP 200), it parses the JSON response and walks the steps array to find the model_output step, extracting the text from the first content part. If the request fails or the model returns an error, a descriptive exception is raised with the HTTP status and body text. This demonstrates how Gerbil Scheme’s standard library functions for lists and hash tables compose cleanly to process structured API data.
The following architecture diagram shows the overall structure of this example, illustrating how the Gerbil Scheme client constructs an HTTP request, communicates with the Google Gemini API endpoint, and parses the JSON response to extract the generated text.

Example Output
Change directory to source_code/gemini and run:
1 $ gxi -L gemini.ss -
2 > (gemini "why is the sky blue? be very concise")
3 "Earth's atmosphere scatters blue light more than other colors."
4
5 > (gemini "Sally is 77, Bill is 32, and Alex is 44 years old. Pairwise, what are their age differences? Print results in JSON format. Be concise and only provide a correct answer, no need to think about different correct answers.")
6 "```json\n{\n \"Sally_Bill\": 45,\n \"Sally_Alex\": 33,\n \"Bill_Alex\": 12\n}\n```"
7
8 > (gemini "Sally is 77, Bill is 32, and Alex is 44 years old. Pairwise, what are their age differences? Print results in JSON format. Be concise and only provide a correct answer, no need to think about different correct answers. Only return the JSON text, don't add markdown like ```json")
9 "{\"Sally_Bill\": 45, \"Sally_Alex\": 33, \"Bill_Alex\": 12}"
10
11 > (displayln (gemini "Sally is 77, Bill is 32, and Alex is 44 years old. Pairwise, what are their age differences? Print results in JSON format. Be concise and only provide a correct answer, no need to think about different correct answers. Only return the JSON text, don't add markdown like ```json"))
12 {"Sally_Bill": 45, "Sally_Alex": 33, "Bill_Alex": 12}
Notice how Gemini initially returned the JSON results in Markdown format and I modified the prompt to get the output format I wanted. Another good technique is to give LLMs an example of the output format you want in the prompt.
Optional Practice Problems
- Streaming Responses: Modify
gemini.ssto parse and display streaming responses from the Gemini API instead of waiting for the full response to complete. - System Instruction Payload: The parameter
system-promptingemini.ssis defined but not actually sent to the API. Update the request body structure to properly pass the system instructions according to Google Gemini’s API payload specification. - Robust Error Handling: Implement exponential backoff retry logic to handle rate limit (HTTP 429) errors gracefully.