Functions
chat
fn (model: Str, message: Str): Str
fn (model: Str, message: Str, system: Str): Str
fn (model: Str, message: Str, system: Str?, max-tokens: Int?): Str
fn (model: Str, message: Str, opts: Map): Str
fn (model: Str, message: Str, system: Str?, opts: Map): Str
fn (model: Str, message: Str, system: Str?, max-tokens: Int?, opts: Map): Str
Simple chat - send a message and get a response string. This is the highest-level convenience function for single-turn chat.
Accepts 2 to 4 arguments: model, message, optional system instruction, and optional max tokens. Any of those forms also takes a trailing opts Map (see ::gemini::api/resolve).
Example
answer ::gemini::chat/chat("gemini-2.0-flash", "What is the capital of France?")
// => "The capital of France is Paris."
With system instruction
answer ::gemini::chat/chat("gemini-2.0-flash", "What is 2+2?", "You are a math tutor. Reply concisely.")
// => "4"
With max tokens
answer ::gemini::chat/chat("gemini-2.0-flash", "Write a haiku about coding.", null, 100)
With a per-call API key
answer ::gemini::chat/chat("gemini-2.0-flash", "Hello!", {api-key: ::hot::ctx/get("tenant.gemini.key")})
extract-chunk-text
fn (event): Str
Extract the text content from a streaming SSE event. Returns an empty string if no text content is found. Handles NDJSON format where multiple JSON objects are separated by newlines.
Example
::gemini::chat/extract-chunk-text({data: {candidates: [{content: {parts: [{text: "Hello"}]}}]}}) // "Hello"
::gemini::chat/extract-chunk-text({data: "{\"candidates\":[{\"content\":{\"parts\":[{\"text\":\"Hi\"}]}}]}"}) // "Hi"
::gemini::chat/extract-chunk-text({data: null}) // ""
extract-text-from-data
fn (data): Str
The text of the first part of the first candidate in a parsed response
or stream chunk, or an empty string when there is none. For a raw stream
event, use extract-chunk-text.
Example
::gemini::chat/extract-text-from-data({candidates: [{content: {parts: [{text: "Hi"}]}}]}) // "Hi"
::gemini::chat/extract-text-from-data({candidates: []}) // ""
generate
fn (model: Str, request: GenerateContentRequest): GenerateContentResponse
fn (model: Str, request: GenerateContentRequest, opts: Map): GenerateContentResponse
Generate content (non-streaming). Sends a request to the Gemini API and returns the full response with candidates.
Example
::chat ::gemini::chat
response ::chat/generate("gemini-2.0-flash", ::chat/GenerateContentRequest({
contents: [::chat/Content({
role: "user",
parts: [::chat/Part({text: "Say hello!"})]
})],
generationConfig: ::chat/GenerationConfig({maxOutputTokens: 50})
}))
text first(response.candidates).content.parts[0].text
With system instruction
::chat ::gemini::chat
response ::chat/generate("gemini-2.0-flash", ::chat/GenerateContentRequest({
contents: [::chat/Content({
role: "user",
parts: [::chat/Part({text: "What is 2+2?"})]
})],
systemInstruction: ::chat/Content({
role: "user",
parts: [::chat/Part({text: "You are a math assistant. Reply with just the answer."})]
}),
generationConfig: ::chat/GenerationConfig({maxOutputTokens: 50})
}))
generate-stream
fn (model: Str, request: GenerateContentRequest): StreamingGenerateResponse
fn (model: Str, request: GenerateContentRequest, opts: Map): StreamingGenerateResponse
Generate content with streaming. Returns a response with status, headers, and an iterator body that yields SSE events.
Each event contains a partial GenerateContentResponse with text chunks in candidates[0].content.parts[0].text. Use extract-chunk-text to extract text from each event.
Example
::chat ::gemini::chat
response ::chat/generate-stream("gemini-2.0-flash", ::chat/GenerateContentRequest({
contents: [::chat/Content({
role: "user",
parts: [::chat/Part({text: "Count from 1 to 5."})]
})],
generationConfig: ::chat/GenerationConfig({maxOutputTokens: 100})
}))
response.status // => 200
events collect(response.body)
image-part
fn (mime-type: Str, base64-data: Str): Part
Create an image content part from base64-encoded data. Used for sending images inline in a Content message.
Example
part ::gemini::chat/image-part("image/png", "iVBORw0KGgo=")
part.inlineData.mimeType // => "image/png"
is-data-done
fn (data): Bool
True when the first candidate of a parsed response or stream chunk has a
finishReason. For a raw stream event, use is-stream-done.
Example
::gemini::chat/is-data-done({candidates: [{finishReason: "STOP"}]}) // true
::gemini::chat/is-data-done({candidates: [{content: {parts: []}}]}) // false
is-stream-done
fn (event): Bool
Check if a streaming event indicates the stream is complete. Returns true when a candidate has a finishReason set (e.g. "STOP"). Handles NDJSON format.
Example
::gemini::chat/is-stream-done({data: {candidates: [{finishReason: "STOP"}]}}) // true
::gemini::chat/is-stream-done({data: {candidates: [{content: {parts: []}}]}}) // false
model-content
fn (text: Str): Content
Create a Content message with the "model" role from a text string. Useful for building multi-turn conversation history.
Example
content ::gemini::chat/model-content("Hello! How can I help?")
content.role // => "model"
first(content.parts).text // => "Hello! How can I help?"
text-part
fn (text: Str): Part
Create a text content part for use in Content messages.
Example
part ::gemini::chat/text-part("Hello, world!")
part.text // => "Hello, world!"
user-content
fn (text: Str): Content
Create a Content message with the "user" role from a text string. Shorthand for building Content with a single text Part.
Example
content ::gemini::chat/user-content("Hello world")
content.role // => "user"
first(content.parts).text // => "Hello world"
Types
Candidate
Candidate type {
content: Content?,
finishReason: FinishReason?,
safetyRatings: Vec<Map>?,
index: Int?
}
Content
Content type {
role: Role,
parts: Vec<Part>
}
FinishReason
FinishReason type "STOP" | "MAX_TOKENS" | "SAFETY" | "RECITATION" | "OTHER"
GenerateContentRequest
GenerateContentRequest type {
contents: Vec<Content>,
systemInstruction: Content?,
generationConfig: GenerationConfig?,
safetySettings: Vec<SafetySetting>?
}
GenerateContentResponse
GenerateContentResponse type {
candidates: Vec<Candidate>?,
promptFeedback: PromptFeedback?,
usageMetadata: UsageMetadata?
}
GenerationConfig
GenerationConfig type {
temperature: Dec?,
topP: Dec?,
topK: Int?,
maxOutputTokens: Int?,
stopSequences: Vec<Str>?
}
InlineData
InlineData type {
mimeType: Str,
data: Str
}
Part
Part type {
text: Str?,
inlineData: InlineData?
}
PromptFeedback
PromptFeedback type {
blockReason: Str?,
safetyRatings: Vec<Map>?
}
Role
Role type "user" | "model"
SafetySetting
SafetySetting type {
category: Str,
threshold: Str
}
StreamingGenerateResponse
StreamingGenerateResponse type {
status: Int,
headers: Map,
body: Any
}
UsageMetadata
UsageMetadata type {
promptTokenCount: Int?,
candidatesTokenCount: Int?,
totalTokenCount: Int?
}