pathwaycom/pathway · error · ValueError

'topic' must be a non-empty string; got {topic!r}. Kafka doe

Error message

'topic' must be a non-empty string; got {topic!r}. Kafka does not allow empty topic names.

What it means

After all alias handling and list unwrapping, pw.io.kafka.read requires the final topic value to be a non-empty string; anything else (None, empty string, an int, etc.) raises ValueError echoing the offending value. Kafka itself rejects invalid topic names, but Pathway's message is more actionable.

Source

Thrown at python/pathway/io/kafka/__init__.py:361

            raise TypeError(
                f"'topic_names' must be a str or a list of str; got "
                f"{type(topic_names).__name__}"
            )
    if isinstance(topic, list):
        if not topic:
            raise ValueError(
                "'topic' must be a non-empty string; got an empty list. "
                "Kafka does not allow empty topic names."
            )
        warnings.warn(
            "'topic' should be a str, not list. First element will be used.",
            DeprecationWarning,
            stacklevel=_stacklevel + 4,
        )
        topic = topic[0]

    if not isinstance(topic, str) or not topic:
        raise ValueError(
            f"'topic' must be a non-empty string; got {topic!r}. "
            "Kafka does not allow empty topic names."
        )

    check_deprecated_kwargs(kwargs, ["topic_names"], stacklevel=_stacklevel + 4)

    data_storage = api.DataStorage(
        storage_type="kafka",
        rdkafka_settings=rdkafka_settings,
        topic=topic,
        parallel_readers=parallel_readers,
        start_from_timestamp_ms=start_from_timestamp_ms,
        mode=internal_connector_mode(mode),
    )

    # TODO: support case when the key is scalar and the value is json
    schema, data_format = construct_schema_and_data_format(
        format,

View on GitHub (pinned to fa2f74a464)

Solutions

  1. Pass a concrete non-empty string: topic='test-topic'.
  2. If the topic comes from config, default it: topic = os.environ.get('KAFKA_TOPIC') or 'test-topic' — and strip whitespace.
  3. Kafka topic names also must match [a-zA-Z0-9._-]{1,255}; validate that pattern for user-supplied names.

Example fix

# before
topic = os.environ.get("KAFKA_TOPIC")  # unset -> None
t = pw.io.kafka.read(rdkafka_settings, topic=topic)
# after
topic = os.environ.get("KAFKA_TOPIC") or "test-topic"
t = pw.io.kafka.read(rdkafka_settings, topic=topic)
Defensive patterns

Strategy: validation

Validate before calling

import re
KAFKA_TOPIC_RE = re.compile(r"^[a-zA-Z0-9._-]{1,255}$")

def valid_kafka_topic(name) -> bool:
    return isinstance(name, str) and bool(KAFKA_TOPIC_RE.match(name))

assert valid_kafka_topic(topic), f"bad Kafka topic: {topic!r}"
t = pw.io.kafka.read(rdkafka_settings, topic=topic)

Type guard

def valid_kafka_topic(name) -> bool:
    import re
    return isinstance(name, str) and bool(re.match(r"^[a-zA-Z0-9._-]{1,255}$", name))

Prevention

When it happens

Trigger: pw.io.kafka.read(rdkafka_settings, topic=''), topic=None, or topic=123. This is the last topic check — it fires when the value survived the alias/list branches but is still not a usable string.

Common situations: Topic read from an env variable that is unset (yielding '' or None); passing a topic name containing only whitespace; passing a non-string identifier (int id from a config system) by mistake.

Related errors


AI-assisted analysis of pathwaycom/pathway@fa2f74a464 (2026-08-15). Data as JSON: /api/errors/9ad17af914f0586f. Report an issue: GitHub.