{"record":{"id":"41035ff81000506a","repo":"apache/beam","slug":"either-a-topic-or-subscription-must-be-provided","errorCode":null,"errorMessage":"Either a topic or subscription must be provided.","messagePattern":"Either a topic or subscription must be provided\\.","errorType":"validation","errorClass":"ValueError","httpStatus":null,"severity":"error","filePath":"sdks/python/apache_beam/io/gcp/pubsub.py","lineNumber":538,"sourceCode":"  def __init__(\n      self,\n      topic: Optional[str] = None,\n      subscription: Optional[str] = None,\n      id_label: Optional[str] = None,\n      with_attributes: bool = False,\n      timestamp_attribute: Optional[str] = None):\n    self.coder = coders.BytesCoder()\n    self.full_topic = topic\n    self.full_subscription = subscription\n    self.topic_name = None\n    self.subscription_name = None\n    self.id_label = id_label\n    self.with_attributes = with_attributes\n    self.timestamp_attribute = timestamp_attribute\n\n    # Perform some validation on the topic and subscription.\n    if not (topic or subscription):\n      raise ValueError('Either a topic or subscription must be provided.')\n    if topic and subscription:\n      raise ValueError('Only one of topic or subscription should be provided.')\n\n    if topic:\n      self.project, self.topic_name = parse_topic(topic)\n    if subscription:\n      self.project, self.subscription_name = parse_subscription(subscription)\n\n  def display_data(self):\n    return {\n        'id_label': DisplayDataItem(self.id_label,\n                                    label='ID Label Attribute').drop_if_none(),\n        'topic': DisplayDataItem(self.full_topic,\n                                 label='Pubsub Topic').drop_if_none(),\n        'subscription': DisplayDataItem(\n            self.full_subscription, label='Pubsub Subscription').drop_if_none(),\n        'with_attributes': DisplayDataItem(\n            self.with_attributes, label='With Attributes').drop_if_none(),","sourceCodeStart":520,"sourceCodeEnd":556,"githubUrl":"https://github.com/apache/beam/blob/12126d8942aaf848030c478b4c6a28c6af861c66/sdks/python/apache_beam/io/gcp/pubsub.py#L520-L556","documentation":"_PubSubSource.__init__ requires exactly one of topic or subscription to be specified; a ValueError is raised when neither is provided. This is an upfront argument-validation guard before any pipeline expansion.","triggerScenarios":"Calling ReadFromPubSub() (or _PubSubSource) with no topic and no subscription, e.g. both arguments coming from empty/None config values or unset pipeline parameters.","commonSituations":"Config-driven pipelines where the pubsub source key is missing from YAML/JSON config; variables resolved to None from environment lookups; refactored code that dropped the default topic.","solutions":["Pass either topic='projects/<project>/topics/<topic>' or subscription='projects/<project>/subscriptions/<sub>'","Check that the config value feeding the argument is not None/empty before building the transform","Default the value at config load time and fail early with a clearer message"],"exampleFix":"// before\nReadFromPubSub(topic=os.environ.get('TOPIC'))  # TOPIC unset -> None\n// after\ntopic = os.environ['TOPIC']  # fail fast if missing\nassert topic, 'TOPIC must be set'\nReadFromPubSub(topic=topic)","handlingStrategy":"validation","validationCode":"assert bool(topic) != bool(subscription) or topic, 'need topic or subscription'\nif not (topic or subscription):\n    raise ValueError('no Pub/Sub source configured')","typeGuard":null,"tryCatchPattern":"try:\n    beam.io.ReadFromPubSub(topic=topic, subscription=subscription)\nexcept ValueError as e:\n    if 'Either a topic or subscription must be provided' in str(e):\n        raise SystemExit('Configure PUBSUB_TOPIC or PUBSUB_SUBSCRIPTION before running')\n    raise","preventionTips":["Fail at config-load time when neither topic nor subscription is present","Use required-config validation before pipeline construction","Never rely on env vars without defaults being set"],"tags":["python","pubsub","validation","apache-beam"],"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-14T16:17:12.679Z"}