apache/beam · error · ValueError

project and location must both be provided if api_key is…

Error message

project and location must both be provided if api_key is None

What it means

Raised by the Gemini model handler __init__ when no api_key is given but project or location is missing. Without an API key the handler uses Vertex AI, which requires both a GCP project and a location.

Solutions

  1. Provide both project and location when omitting api_key.
  2. Supply api_key if the Gemini Developer API is intended instead of Vertex AI.
  3. Verify the api_key config/env value is actually being read and passed (not None) if Vertex was not intended.

Example fix

// before
GeminiModelHandler(model_name='gemini-2.0-flash', project='my-proj')
// after
GeminiModelHandler(model_name='gemini-2.0-flash', project='my-proj', location='us-central1')
Defensive patterns

Strategy: validation

Validate before calling

if not api_key and not (project and location):
    raise ValueError('Vertex mode requires both project and location (or set api_key)')

Prevention

When it happens

Trigger: Constructing GeminiModelHandler(model_name=...) with neither api_key nor a complete (project, location) pair, e.g. GeminiModelHandler(project='my-proj') with location=None.

Common situations: Deploying to an environment where the API-key env var is unset so code falls into the Vertex path; config files with only project set; forgetting location when switching from Developer API to Vertex.

Understand the failure class

Background: "missing required argument" and "the following required arguments were not provided": what required-argument errors mean and how to fix them — this error's family across 20 libraries.

Related errors


AI-assisted analysis of apache/beam@12126d8942 (2026-09-13). Data as JSON: /api/errors/80344ba0046bd98f. Report an issue: GitHub.

Appendix: source

Thrown at sdks/python/apache_beam/ml/inference/gemini_inference.py:195

      self._batching_kwargs["max_batch_weight"] = max_batch_weight
    if element_size_fn is not None:
      self._batching_kwargs['element_size_fn'] = element_size_fn
    if batch_length_fn is not None:
      self._batching_kwargs['length_fn'] = batch_length_fn
    if batch_bucket_boundaries is not None:
      self._batching_kwargs['bucket_boundaries'] = batch_bucket_boundaries

    self.model_name = model_name
    self.request_fn = request_fn

    if api_key:
      if project or location:
        raise ValueError("project and location must be None if api_key is set")
      self.api_key = api_key
      self.use_vertex = False
    else:
      if project is None or location is None:
        raise ValueError(
            "project and location must both be provided if api_key is None")
      self.project = project
      self.location = location
      self.use_vertex = True

    self.use_vertex_flex_api = use_vertex_flex_api

    super().__init__(
        namespace='GeminiModelHandler',
        retry_filter=_retry_on_appropriate_service_error,
        **kwargs)

  def batch_elements_kwargs(self):
    return self._batching_kwargs

  def create_client(self) -> genai.Client:
    """Creates the GenAI client used to send requests. Creates a version for
    the Vertex AI API or the Gemini Developer API based on the arguments

View on GitHub (pinned to 12126d8942)