google-gemini/gemini-cli · error · Error

Failed to create chat object: ${getErrorMessage(e)}

Error message

Failed to create chat object: ${getErrorMessage(e)}

What it means

Catch-all thrown by createChatObject when constructing or initializing GeminiChat raised. The original error is reported via reportError and re-thrown wrapped with getErrorMessage(e). The underlying cause is whatever made the GeminiChat start fail.

Source

Thrown at packages/core/src/agents/local-executor.ts:1096

      const chat = new GeminiChat(
        this.executionContext,
        systemInstruction,
        [{ functionDeclarations: tools }],
        startHistory,
        undefined,
        undefined,
      );
      await chat.initialize(undefined, 'subagent');
      return chat;
    } catch (e: unknown) {
      await reportError(
        e,
        `Error initializing Gemini chat for agent ${this.definition.name}.`,
        startHistory,
        'startChat',
      );
      // Re-throw as a more specific error after reporting.
      throw new Error(`Failed to create chat object: ${getErrorMessage(e)}`);
    }
  }

  /**
   * Executes function calls requested by the model and returns the results.
   *
   * @returns A new `Content` object for history, any submitted output, and completion status.
   */
  private async processFunctionCalls(
    chat: GeminiChat,
    model: string,
    functionCalls: FunctionCall[],
    signal: AbortSignal,
    promptId: string,
    onWaitingForConfirmation?: (waiting: boolean) => void,
  ): Promise<{
    nextMessage: Content;
    submittedOutput: string | null;

View on GitHub (pinned to 5024443c72)

Solutions

  1. Read the wrapped message (getErrorMessage(e)) — it identifies whether it is auth, model-not-found, schema, or network.
  2. Verify the API key / ADC and that the chosen model is enabled for the project.
  3. Inspect each FunctionDeclaration passed in `tools`; simplify schemas and remove unsupported types until the call succeeds.
  4. Check quota/billing; retry on transient network errors.
  5. Run with debug logging to capture the reportError output for the full original stack.

Example fix

// before: a tool schema with an unsupported type
{ name: 'x', parameters: { type: 'object', properties: { a: { type: 'any' } } } }

// after: use a supported JSON-Schema type
{ name: 'x', parameters: { type: 'object', properties: { a: { type: 'string' } } } }
Defensive patterns

Strategy: try-catch

Validate before calling

// Validate tool schemas and API key before creating the chat object.
function validateTools(tools) {
  for (const t of tools) {
    if (!t.name) throw new Error('Tool missing name');
    if (!t.parameters || t.parameters.type !== 'object')
      throw new Error(`Tool ${t.name} parameters must be an object schema`);
  }
}
if (!process.env['GEMINI_API_KEY'] && !process.env['GOOGLE_APPLICATION_CREDENTIALS'])
  throw new Error('No Gemini credentials configured');

Try / catch

try {
  await executor.createChatObject(inputs, tools);
} catch (e) {
  if (e instanceof Error && e.message.startsWith('Failed to create chat object')) {
    // inspect inner cause from logs (reportError), fix schema/key/model
  }
  throw e;
}

Prevention

When it happens

Trigger: new GeminiChat(...) or chat.initialize(undefined, 'subagent') throwing — e.g. invalid/missing API key, requested model not available for the project, malformed FunctionDeclaration schema in the tools array, network/transport failure reaching the model endpoint.

Common situations: GEMINI_API_KEY / ADC not set or revoked; model name typo or model not enabled in the project; a tool's FunctionDeclaration has an invalid JSON schema (e.g. unsupported types, missing required fields); quota/billing disabled; transient network outage.

Related errors


AI-assisted analysis of google-gemini/gemini-cli@5024443c72 (2026-08-12). Data as JSON: /api/errors/24356f13fcb846d4. Report an issue: GitHub.