google-gemini/gemini-cli · error · Error
projectId is not defined for CodeAssistServer.
Error message
projectId is not defined for CodeAssistServer.
What it means
Thrown by CodeAssistServer.listExperiments() when the server instance was constructed without a projectId. Several CodeAssistServer methods require a project ID to scope API requests; listExperiments enforces this eagerly with a guard. The projectId is passed during construction from setupUser() and can be undefined if no project was resolved from environment or user settings.
Source
Thrown at packages/core/src/code_assist/server.ts:357
async countTokens(req: CountTokensParameters): Promise<CountTokensResponse> {
const resp = await this.requestPost<CaCountTokenResponse>(
'countTokens',
toCountTokenRequest(req),
);
return fromCountTokenResponse(resp);
}
async embedContent(
_req: EmbedContentParameters,
): Promise<EmbedContentResponse> {
throw Error();
}
async listExperiments(
metadata: ClientMetadata,
): Promise<ListExperimentsResponse> {
if (!this.projectId) {
throw new Error('projectId is not defined for CodeAssistServer.');
}
const projectId = this.projectId;
const req: ListExperimentsRequest = {
project: projectId,
metadata: { ...metadata, duetProject: projectId },
};
return this.requestPost<ListExperimentsResponse>('listExperiments', req);
}
async retrieveUserQuota(
req: RetrieveUserQuotaRequest,
): Promise<RetrieveUserQuotaResponse> {
return this.requestPost<RetrieveUserQuotaResponse>(
'retrieveUserQuota',
req,
);
}
View on GitHub (pinned to 5024443c72)
Solutions
- Set GOOGLE_CLOUD_PROJECT or GOOGLE_CLOUD_PROJECT_ID to a valid project ID.
- Run 'gcloud config set project YOUR_PROJECT_ID' before starting the CLI.
- Ensure setupUser() completes project resolution before any CodeAssistServer method that requires projectId is called.
- In code calling listExperiments, guard: if (!server.projectId) skip or prompt for project setup.
Example fix
# Set the project ID environment variable
export GOOGLE_CLOUD_PROJECT=my-project-123
// Or guard in code before calling listExperiments
if (!server.projectId) {
console.error('Set GOOGLE_CLOUD_PROJECT before listing experiments');
return;
}
await server.listExperiments(metadata); Defensive patterns
Strategy: validation
Validate before calling
// Verify projectId is set before calling methods that require it
if (!server.projectId) {
throw new Error(
'CodeAssistServer requires a project ID. Set GOOGLE_CLOUD_PROJECT.'
);
}
await server.listExperiments(metadata); Type guard
function serverHasProjectId(
server: CodeAssistServer
): server is CodeAssistServer & { projectId: string } {
return typeof server.projectId === 'string' && server.projectId.length > 0;
} Try / catch
try {
await server.listExperiments(metadata);
} catch (e) {
if (e instanceof Error && e.message.includes('projectId is not defined')) {
console.error('Set GOOGLE_CLOUD_PROJECT before using Code Assist features.');
return;
}
throw e;
} Prevention
- Always set GOOGLE_CLOUD_PROJECT or GOOGLE_CLOUD_PROJECT_ID in the environment.
- Guard CodeAssistServer methods with a projectId check before calling.
- Complete project onboarding in setupUser before using server methods.
- Add a startup check that warns if no project is configured.
When it happens
Trigger: Calling server.listExperiments(metadata) on a CodeAssistServer instance whose projectId constructor argument was undefined. This happens when setupUser could not determine a project and the server was still instantiated.
Common situations: The GOOGLE_CLOUD_PROJECT and GOOGLE_CLOUD_PROJECT_ID env vars are unset and no default project was resolved; the user is on a tier that doesn't require a project but listExperiments was called anyway; a CodeAssistServer was constructed directly in tests without providing projectId; the OAuth flow completed but project onboarding was skipped.
Related errors
- Invalid Google Cloud Project ID: "${projectId}". The GOOGLE_
- Access to the default Cloud Shell Gemini project was denied.
- Unsupported authType: ${authType}
- Missing required environment variable '{name}'. Please ensur
- COMPUTE_ADC failed: ${adcMessage}. (LOGIN_WITH_GOOGLE fallba
AI-assisted analysis of google-gemini/gemini-cli@5024443c72 (2026-08-12).
Data as JSON: /api/errors/11b3e07dc06c1f73.
Report an issue: GitHub.