microsoft/autogen · error · ValueError

Invalid topic type: {self.type}. Must match the pattern: ^[\

Error message

Invalid topic type: {self.type}. Must match the pattern: ^[\w\-\.\:\=]+\Z

What it means

TopicId.__post_init__ validates the topic type against ^[\w\-\.\:=]+$ (word chars, hyphen, dot, colon, equals; no spaces, slashes, or unicode punctuation). This mirrors the CloudEvents 'type' constraint so topic types are safe as routing keys across transports. A TopicId constructed with a non-conforming type raises ValueError immediately at dataclass construction.

Source

Thrown at python/packages/autogen-core/src/autogen_core/_topic.py:35

    """

    type: str
    """Type of the event that this topic_id contains. Adhere's to the cloud event spec.

    Must match the pattern: ^[\\w\\-\\.\\:\\=]+\\Z

    Learn more here: https://github.com/cloudevents/spec/blob/main/cloudevents/spec.md#type
    """

    source: str
    """Identifies the context in which an event happened. Adhere's to the cloud event spec.

    Learn more here: https://github.com/cloudevents/spec/blob/main/cloudevents/spec.md#source-1
    """

    def __post_init__(self) -> None:
        if is_valid_topic_type(self.type) is False:
            raise ValueError(f"Invalid topic type: {self.type}. Must match the pattern: ^[\\w\\-\\.\\:\\=]+\\Z")

    def __str__(self) -> str:
        return f"{self.type}/{self.source}"

    @classmethod
    def from_str(cls, topic_id: str) -> Self:
        """Convert a string of the format ``type/source`` into a TopicId"""
        items = topic_id.split("/", maxsplit=1)
        if len(items) != 2:
            raise ValueError(f"Invalid topic id: {topic_id}")
        type, source = items[0], items[1]
        return cls(type, source)

View on GitHub (pinned to 027ecf0a37)

Solutions

  1. Sanitize the type string: replace disallowed characters, e.g. `re.sub(r"[^\w\-.:=]", "_", raw)`.
  2. Use stable snake_case/kebab-case identifiers for topic types (they are routing keys, not display names).
  3. Validate user-supplied topic types with the same regex before constructing TopicId.

Example fix

# before
topic = TopicId("orders/created eu", "order-service")  # ValueError

# after
import re
topic_type = re.sub(r"[^\w\-.:=]", "_", "orders/created eu")
topic = TopicId(topic_type, "order-service")
Defensive patterns

Strategy: validation

Validate before calling

import re

_TOPIC_TYPE_RE = re.compile(r"^[\w\-\.:\=]+$")

def is_valid_topic_type_str(t: str) -> bool:
    return bool(_TOPIC_TYPE_RE.match(t))

assert is_valid_topic_type_str(topic_type), f"bad topic type: {topic_type!r}"

Try / catch

try:
    topic = TopicId(topic_type, source)
except ValueError as e:
    topic_type = re.sub(r"[^\w\-.:=]", "_", topic_type)
    topic = TopicId(topic_type, source)

Prevention

When it happens

Trigger: Constructing `TopicId("my topic", "src")` (space), `TopicId("a/b", "src")` (slash — reserved as the type/source separator), or building a topic type from unvalidated user input; also auto-generated types that interpolate arbitrary strings.

Common situations: Deriving topic types from user-typed names, file paths, or LLM output without sanitizing; switching from TypeSubscription to custom topic types where the source string was previously free-form; strings containing whitespace or '/' from JSON config.

Related errors


AI-assisted analysis of microsoft/autogen@027ecf0a37 (2026-08-15). Data as JSON: /api/errors/7ad6c843fe38a58e. Report an issue: GitHub.