Vertex AI Generate Content Agent
This integration sends your input text to Google Vertex AI and returns the generated response text.
Required secrets
| Secret | What it is |
|---|---|
GOOGLE_VERTEX_AI_API_KEY | API key for Vertex AI generate-content requests. |
Where to get the credentials
Provision an API key for Vertex AI in your Google Cloud project, then store it as
a project secret named GOOGLE_VERTEX_AI_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 Cloud project, 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_VERTEX_AI_API_KEY ?? "").trim();
const { text } = parseInput(input);
// ...
Secrets are read from the env object by name. Here the Vertex AI API key is
read from env.GOOGLE_VERTEX_AI_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
ENDPOINTURL, - how the request authenticates (here the key is a query parameter; many APIs use
an
authorizationheader 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
- Copy this integration from the registry — see Registry integrations.
- Create the project secret above — see Project secrets.
- Preview the agent to confirm it works.