n8n-io/n8n · error · Error
Invalid response format from Gemini API
Error message
Invalid response format from Gemini API
What it means
Thrown by the GoogleGemini image edit operation when the API response from /v1beta/{model}:generateContent fails the isGenerateContentResponse type guard. This guard checks that the response is an object with a 'candidates' array where each candidate has a 'content' object with a 'parts' array. A response that doesn't match this shape is considered malformed. Note: this is a plain Error, not a NodeOperationError.
Source
Thrown at packages/@n8n/nodes-langchain/nodes/vendors/GoogleGemini/actions/image/edit.operation.ts:207
{
role: 'user',
parts: [...fileParts, { text: prompt }],
},
],
generationConfig,
};
const response: unknown = await apiRequest.call(
this,
'POST',
`/v1beta/${model}:generateContent`,
{
body,
},
);
if (!isGenerateContentResponse(response)) {
throw new Error('Invalid response format from Gemini API');
}
const promises = response.candidates.map(async (candidate) => {
const imagePart = candidate.content.parts.find((part) => 'inlineData' in part);
// Check if imagePart exists and has inlineData with actual data
if (!imagePart?.inlineData?.data) {
throw new Error('No image data returned from Gemini API');
}
const mimeType = imagePart.inlineData.mimeType;
const fileName = getFilenameFromMimeType(mimeType, 'image', 'png');
const bufferOut = Buffer.from(imagePart.inlineData.data, 'base64');
const binaryOut = await this.helpers.prepareBinaryData(bufferOut, fileName, mimeType);
return {
binary: {
[outputKey]: binaryOut,
},View on GitHub (pinned to 5ac6606e81)
Solutions
- Inspect the actual API response body — the error fires because it doesn't match the expected shape. Log or debug the raw response to see what the API returned.
- Verify the API key is valid and has Gemini API access.
- Confirm the model ID is correct and supports image generation (e.g. models/gemini-2.5-flash-image-preview).
- Check for safety filter blocks — the response may contain promptFeedback with safety ratings instead of candidates. Adjust the prompt to avoid triggering filters.
- Check the Google AI status page for service incidents.
Defensive patterns
Strategy: type-guard
Type guard
function isGenerateContentResponse(response: unknown): response is {
candidates: Array<{ content: { parts: Array<Record<string, unknown>> } }>;
} {
if (typeof response !== 'object' || response === null) return false;
const obj = response as Record<string, unknown>;
if (!('candidates' in obj) || !Array.isArray(obj.candidates)) return false;
return obj.candidates.every((c: unknown) =>
typeof c === 'object' && c !== null &&
'content' in (c as object) &&
typeof (c as Record<string, unknown>).content === 'object' &&
'parts' in ((c as Record<string, unknown>).content as object) &&
Array.isArray(((c as Record<string, unknown>).content as Record<string, unknown>).parts)
);
} Try / catch
try {
const response = await apiRequest.call(this, 'POST', `/v1beta/${model}:generateContent`, { body });
if (!isGenerateContentResponse(response)) {
// log the raw response for debugging and surface a user-friendly error
console.error('Unexpected Gemini response:', JSON.stringify(response));
throw new Error('Gemini API returned an unexpected response format. Check API key, model ID, and safety settings.');
}
} catch (error) {
// handle or rethrow
throw error;
} Prevention
- Verify the Gemini API key is valid and has the correct permissions before running.
- Confirm the model ID supports image generation (use 'models/gemini-2.5-flash-image-preview').
- Check for safety filter blocks by inspecting the full response including promptFeedback.
- Monitor Google AI Platform status for service disruptions.
- Log raw API responses during development to catch schema changes early.
When it happens
Trigger: The Gemini API returns a response without a 'candidates' array, or candidates exist but lack 'content.parts'. This can happen when the API returns an error object instead of a generation result (e.g. quota error, invalid API key, model unavailable, safety block with no candidates), when the API version changes the response schema, or when a network proxy mangles the response body.
Common situations: API key invalid or expired (returns error envelope, not candidates); model name incorrect or deprecated; safety filter blocks the request and returns promptFeedback without candidates; API rate limit returns error body; Gemini API version change altering response structure; proxy/gateway stripping or modifying response fields.
Related errors
- Invalid images parameter format
- A non-empty prompt is required.
- No image data returned from Gemini API
- Expected Supabase credentials host to be a string
- A non-empty prompt is required.
AI-assisted analysis of n8n-io/n8n@5ac6606e81 (2026-08-12).
Data as JSON: /api/errors/dccbf6b5d8dda9e8.
Report an issue: GitHub.