JuliusBrussee/caveman · error

gemini bearer credential requires a valid…

Error message

gemini bearer credential requires a valid x-goog-user-project

What it means

When authenticating Gemini with a bearer-type credential (OAuth/ADC from Gemini CLI), the adapter must forward the caller's Google Cloud quota project via x-goog-user-project; remapping the bearer token to x-goog-api-key would break OAuth. If the incoming request's x-goog-user-project header is missing or fails validGoogleQuotaProject, it refuses rather than misattribute quota/billing.

Solutions

  1. Set the request header x-goog-user-project to your Google Cloud project id
  2. Run `gcloud billing projects set-quota-project` (or gcloud config set billing/quota_project) so ADC carries a quota project
  3. Check that intermediate proxies/load balancers do not strip x-goog-user-project
  4. Use an API-key credential instead of a bearer credential if you don't need OAuth

Example fix

// before
req.Header.Set("authorization", "Bearer " + oauthToken) // no quota project
// after
req.Header.Set("authorization", "Bearer " + oauthToken)
req.Header.Set("x-goog-user-project", "my-gcp-project-id")
Defensive patterns

Strategy: validation

Validate before calling

qp := req.Header.Get("x-goog-user-project")
if strings.TrimSpace(qp) == "" {
	return errors.New("gemini OAuth requests must set x-goog-user-project")
}

Try / catch

u, err := adapter.ResolveUpstreamURL(ctx, req, route)
if err != nil && strings.Contains(err.Error(), "x-goog-user-project") {
	// return 400 telling client to set the quota-project header
}

Prevention

When it happens

Trigger: Sending a Gemini request using an OAuth/ADC bearer key without the x-goog-user-project header set, or with an invalid value (empty, wrong format).

Common situations: Clients migrated from API-key auth that never set x-goog-user-project; proxies stripping the header; users authenticating with gcloud ADC without a quota project configured (gcloud config set billing/quota_project).

Understand the failure class

Background: "API key is required" / "API key not found" / "No API key was set": the missing-api-key error family across 16 libraries — this error's family across 16 libraries.

Related errors


AI-assisted analysis of JuliusBrussee/caveman@3ee70a1026 (2026-09-20). Data as JSON: /api/errors/746ec5fb97d7d48e. Report an issue: GitHub.

Appendix: source

Thrown at proxy/providers/adapter.go:547

		if out.Get("anthropic-version") == "" {
			out.Set("anthropic-version", "2023-06-01")
		}
	case "gemini":
		if credential.Key != "" {
			if credential.Scheme == "bearer" {
				// Standalone env fallback uses this synthetic marker to reach the
				// post-sanitization key resolver. It is not OAuth and must not be
				// forwarded or subjected to OAuth quota-project validation.
				if credential.Key == "no-key-required" {
					break
				}
				// Gemini CLI OAuth/ADC credentials are bearer tokens. Remapping one to
				// x-goog-api-key breaks authentication and silently defeats OAuth wrap.
				// Google also requires the caller's quota project for user OAuth. Never
				// guess it: wrong attribution is a billing and quota correctness bug.
				quotaProject := strings.TrimSpace(req.Header.Get("x-goog-user-project"))
				if !validGoogleQuotaProject(quotaProject) {
					return nil, fmt.Errorf("gemini bearer credential requires a valid x-goog-user-project")
				}
				out.Set("authorization", "Bearer "+credential.Key)
				out.Set("x-goog-user-project", quotaProject)
			} else {
				out.Set("x-goog-api-key", credential.Key)
			}
		}
	case "vertex":
		if credential.Key != "" {
			out.Set("authorization", "Bearer "+credential.Key)
		}
		copyIfPresent(out, req.Header, "x-vertex-ai-llm-request-type")
		copyIfPresent(out, req.Header, "x-goog-user-project")
	case "azure_openai":
		if credential.Key != "" {
			// The standalone placeholder is a synthetic no-key marker used by
			// callers that explicitly allow env-backed API-key fallback. It is
			// not an Entra token and must not be forwarded as Authorization;

View on GitHub (pinned to 3ee70a1026)