{"record":{"id":"80344ba0046bd98f","repo":"apache/beam","slug":"project-and-location-must-both-be-provided-if-api-key-is","errorCode":null,"errorMessage":"project and location must both be provided if api_key is None","messagePattern":"project and location must both be provided if api_key is None","errorType":"validation","errorClass":"ValueError","httpStatus":null,"severity":"error","filePath":"sdks/python/apache_beam/ml/inference/gemini_inference.py","lineNumber":195,"sourceCode":"      self._batching_kwargs[\"max_batch_weight\"] = max_batch_weight\n    if element_size_fn is not None:\n      self._batching_kwargs['element_size_fn'] = element_size_fn\n    if batch_length_fn is not None:\n      self._batching_kwargs['length_fn'] = batch_length_fn\n    if batch_bucket_boundaries is not None:\n      self._batching_kwargs['bucket_boundaries'] = batch_bucket_boundaries\n\n    self.model_name = model_name\n    self.request_fn = request_fn\n\n    if api_key:\n      if project or location:\n        raise ValueError(\"project and location must be None if api_key is set\")\n      self.api_key = api_key\n      self.use_vertex = False\n    else:\n      if project is None or location is None:\n        raise ValueError(\n            \"project and location must both be provided if api_key is None\")\n      self.project = project\n      self.location = location\n      self.use_vertex = True\n\n    self.use_vertex_flex_api = use_vertex_flex_api\n\n    super().__init__(\n        namespace='GeminiModelHandler',\n        retry_filter=_retry_on_appropriate_service_error,\n        **kwargs)\n\n  def batch_elements_kwargs(self):\n    return self._batching_kwargs\n\n  def create_client(self) -> genai.Client:\n    \"\"\"Creates the GenAI client used to send requests. Creates a version for\n    the Vertex AI API or the Gemini Developer API based on the arguments","sourceCodeStart":177,"sourceCodeEnd":213,"githubUrl":"https://github.com/apache/beam/blob/12126d8942aaf848030c478b4c6a28c6af861c66/sdks/python/apache_beam/ml/inference/gemini_inference.py#L177-L213","documentation":"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.","triggerScenarios":"Constructing GeminiModelHandler(model_name=...) with neither api_key nor a complete (project, location) pair, e.g. GeminiModelHandler(project='my-proj') with location=None.","commonSituations":"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.","solutions":["Provide both project and location when omitting api_key.","Supply api_key if the Gemini Developer API is intended instead of Vertex AI.","Verify the api_key config/env value is actually being read and passed (not None) if Vertex was not intended."],"exampleFix":"// before\nGeminiModelHandler(model_name='gemini-2.0-flash', project='my-proj')\n// after\nGeminiModelHandler(model_name='gemini-2.0-flash', project='my-proj', location='us-central1')","handlingStrategy":"validation","validationCode":"if not api_key and not (project and location):\n    raise ValueError('Vertex mode requires both project and location (or set api_key)')","typeGuard":null,"tryCatchPattern":null,"preventionTips":["Verify api_key resolution from env/config before choosing Vertex mode.","Require both project and location in Vertex-mode configuration templates."],"tags":["python","apache-beam","gemini","vertex-ai","authentication"],"backgroundTag":"missing-required-argument","analyzedSha":"12126d8942aaf848030c478b4c6a28c6af861c66","analyzedAt":"2026-09-13T01:50:10.254Z","contentChangedAt":"2026-09-13T01:50:10.254Z","schemaVersion":2},"datasetVersion":"2026-09-20T03:17:13.778Z"}