Skip to main content

Gemini Generate Content Agent

This integration calls Google's Gemini generate-content API with your input text and returns the model's text output.

Required secrets

SecretWhat it is
GOOGLE_GEMINI_API_KEYAPI key for Gemini generate-content requests.

Where to get the credentials

Create an API key in Google AI Studio, then store it as a project secret named GOOGLE_GEMINI_API_KEY.

Turn support

Single-turn. Each evaluation case is sent as an independent request.

Limitations and notes

  • The model is set in the copied agent code. Edit the agent if you need a different one.
  • Calls are billed to your own Google account, subject to its quotas and rate limits.

How the agent works

The copied agent is a single JavaScript function. This walkthrough shows the pattern so you can adapt it for a custom agent of your own.

The entry point is process, which receives input (the evaluation case text) and returns the result:

export async function process(input) {
try {
const apiKey = String(env.GOOGLE_GEMINI_API_KEY ?? "").trim();
const { text } = parseInput(input);
// ...

Secrets are read from the env object by name. Here the Gemini API key is read from env.GOOGLE_GEMINI_API_KEY (the secret listed in Required secrets above).

Input is normalized so it accepts either plain text or a JSON object. A JSON object can carry the prompt under text, prompt, or input; anything that is not parseable JSON is used verbatim:

const parseInput = (input) => {
const raw = String(input ?? "").trim();
if (!raw) {
return { text: "" };
}

try {
const parsed = JSON.parse(raw);
if (typeof parsed === "string") {
return { text: parsed.trim() };
}
if (parsed && typeof parsed === "object" && !Array.isArray(parsed)) {
return {
text: String(parsed.text ?? parsed.prompt ?? parsed.input ?? "").trim(),
};
}
} catch {}

return { text: raw };
};

The request is a POST with the API key passed as a query parameter, a JSON content type, and a contents array carrying the user text:

const response = await fetch(
`${ENDPOINT}?key=${encodeURIComponent(apiKey)}`,
{
method: "POST",
headers: {
"content-type": "application/json",
},
body: JSON.stringify({
contents: [
{
role: "user",
parts: [{ text }],
},
],
}),
},
);

The response body is read as text, then parsed as JSON. The assistant text is pulled from the first candidate that has non-empty content.parts text:

const extractResponseText = (payload) => {
const candidates = Array.isArray(payload?.candidates)
? payload.candidates
: [];

for (const candidate of candidates) {
const parts = Array.isArray(candidate?.content?.parts)
? candidate.content.parts
: [];
const text = parts
.map(extractTextFromPart)
.filter(Boolean)
.join("\n")
.trim();
if (text) {
return text;
}
}

return "";
};

Errors are returned, not thrown. The function returns a { ok: false, error } object for missing input, failed requests, and empty responses, and a { ok: true, text } object on success:

return {
ok: true,
text: outputText,
model_version:
typeof payload?.modelVersion === "string" ? payload.modelVersion : null,
// ...
};

Adapting it for your own agent

To point the same skeleton at a different service, change:

  • the ENDPOINT URL,
  • how the request authenticates (here the key is a query parameter; many APIs use an authorization header instead),
  • the request body shape your service expects,
  • the response field you extract the text from in extractResponseText, and
  • the secret key you read from env (and declare it in the required-secrets table).

Set it up

  1. Copy this integration from the registry — see Registry integrations.
  2. Create the project secret above — see Project secrets.
  3. Preview the agent to confirm it works.