{"record":{"id":"d7af2d15b565d1ad","repo":"apache/beam","slug":"only-one-of-context-or-default-environment-may-be-specified","errorCode":null,"errorMessage":"Only one of context or default_environment may be specified.","messagePattern":"Only one of context or default_environment may be specified\\.","errorType":"validation","errorClass":"ValueError","httpStatus":null,"severity":"error","filePath":"sdks/python/apache_beam/pipeline.py","lineNumber":1075,"sourceCode":"    self.visit(Visitor())\n    return Visitor.ok\n\n  def to_runner_api(\n      self,\n      return_context: bool = False,\n      context: Optional['PipelineContext'] = None,\n      use_fake_coders: bool = False,\n      default_environment: Optional['environments.Environment'] = None\n  ) -> beam_runner_api_pb2.Pipeline:\n    \"\"\"For internal use only; no backwards-compatibility guarantees.\"\"\"\n    from apache_beam.runners import pipeline_context\n    if context is None:\n      context = pipeline_context.PipelineContext(\n          use_fake_coders=use_fake_coders,\n          component_id_map=self.component_id_map,\n          default_environment=default_environment)\n    elif default_environment is not None:\n      raise ValueError(\n          'Only one of context or default_environment may be specified.')\n\n    # The FlumeRunner is the only runner setting this option. Use getattr\n    # because other runners do not have this option.\n    context.enable_best_effort_deterministic_pickling = getattr(\n        self.runner, 'enable_best_effort_deterministic_pickling', False)\n\n    # The RunnerAPI spec requires certain transforms and side-inputs to have KV\n    # inputs (and corresponding outputs).\n    # Currently we only upgrade to KV pairs.  If there is a need for more\n    # general shapes, potential conflicts will have to be resolved.\n    # We also only handle single-input, and (for fixing the output) single\n    # output, which is sufficient.\n    # Also marks such values as requiring deterministic key coders.\n    deterministic_key_coders = not self._options.view_as(\n        TypeOptions).allow_non_deterministic_key_coders\n\n    class ForceKvInputTypes(PipelineVisitor):","sourceCodeStart":1057,"sourceCodeEnd":1093,"githubUrl":"https://github.com/apache/beam/blob/12126d8942aaf848030c478b4c6a28c6af861c66/sdks/python/apache_beam/pipeline.py#L1057-L1093","documentation":"ValueError raised in Pipeline.to_runner_api when both an explicit PipelineContext and a default_environment are supplied. The context already carries its own environment, so the two options are mutually exclusive.","triggerScenarios":"Calling pipeline.to_runner_api(context=ctx, default_environment=env), or runner APIs (run_pipeline/get_proto_pipeline) passing both parameters.","commonSituations":"Custom runners or test harnesses constructing a PipelineContext manually while also setting a default environment from options.","solutions":["Pass only one of the two: build the context with default_environment=env in its constructor, or pass default_environment=None","If you need a custom environment inside the context, set it via context.environments","Omit the explicit context and let Beam create one from default_environment"],"exampleFix":"// before\nproto = p.to_runner_api(context=ctx, default_environment=env)\n// after\nctx = pipeline_context.PipelineContext(default_environment=env)\nproto = p.to_runner_api(context=ctx)","handlingStrategy":"validation","validationCode":"assert not (context is not None and default_environment is not None), 'Pass only one of context or default_environment'","typeGuard":null,"tryCatchPattern":"try:\n    proto = p.to_runner_api(context=ctx, default_environment=env)\nexcept ValueError as e:\n    proto = p.to_runner_api(context=ctx)","preventionTips":["Read the to_runner_api signature before combining options","Construct PipelineContext with default_environment in its constructor instead","Centralize proto-serialization code in one helper that enforces exclusivity"],"tags":["python","apache-beam","pipeline","runner-api"],"backgroundTag":"mutually-exclusive-options","analyzedSha":"12126d8942aaf848030c478b4c6a28c6af861c66","analyzedAt":"2026-09-13T01:50:10.254Z","contentChangedAt":"2026-09-13T01:50:10.254Z","schemaVersion":2},"datasetVersion":"2026-09-14T16:17:12.679Z"}