{"record":{"id":"7ad6c843fe38a58e","repo":"microsoft/autogen","slug":"invalid-topic-type-self-type-must-match-the-pa","errorCode":null,"errorMessage":"Invalid topic type: {self.type}. Must match the pattern: ^[\\w\\-\\.\\:\\=]+\\Z","messagePattern":"Invalid topic type: (.+?)\\. Must match the pattern: \\^\\[\\\\w\\\\-\\\\\\.\\\\:\\\\=\\]\\+\\\\Z","errorType":"validation","errorClass":"ValueError","httpStatus":null,"severity":"error","filePath":"python/packages/autogen-core/src/autogen_core/_topic.py","lineNumber":35,"sourceCode":"    \"\"\"\n\n    type: str\n    \"\"\"Type of the event that this topic_id contains. Adhere's to the cloud event spec.\n\n    Must match the pattern: ^[\\\\w\\\\-\\\\.\\\\:\\\\=]+\\\\Z\n\n    Learn more here: https://github.com/cloudevents/spec/blob/main/cloudevents/spec.md#type\n    \"\"\"\n\n    source: str\n    \"\"\"Identifies the context in which an event happened. Adhere's to the cloud event spec.\n\n    Learn more here: https://github.com/cloudevents/spec/blob/main/cloudevents/spec.md#source-1\n    \"\"\"\n\n    def __post_init__(self) -> None:\n        if is_valid_topic_type(self.type) is False:\n            raise ValueError(f\"Invalid topic type: {self.type}. Must match the pattern: ^[\\\\w\\\\-\\\\.\\\\:\\\\=]+\\\\Z\")\n\n    def __str__(self) -> str:\n        return f\"{self.type}/{self.source}\"\n\n    @classmethod\n    def from_str(cls, topic_id: str) -> Self:\n        \"\"\"Convert a string of the format ``type/source`` into a TopicId\"\"\"\n        items = topic_id.split(\"/\", maxsplit=1)\n        if len(items) != 2:\n            raise ValueError(f\"Invalid topic id: {topic_id}\")\n        type, source = items[0], items[1]\n        return cls(type, source)\n","sourceCodeStart":17,"sourceCodeEnd":48,"githubUrl":"https://github.com/microsoft/autogen/blob/027ecf0a379bcc1d09956d46d12d44a3ad9cee14/python/packages/autogen-core/src/autogen_core/_topic.py#L17-L48","documentation":"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.","triggerScenarios":"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.","commonSituations":"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.","solutions":["Sanitize the type string: replace disallowed characters, e.g. `re.sub(r\"[^\\w\\-.:=]\", \"_\", raw)`.","Use stable snake_case/kebab-case identifiers for topic types (they are routing keys, not display names).","Validate user-supplied topic types with the same regex before constructing TopicId."],"exampleFix":"# before\ntopic = TopicId(\"orders/created eu\", \"order-service\")  # ValueError\n\n# after\nimport re\ntopic_type = re.sub(r\"[^\\w\\-.:=]\", \"_\", \"orders/created eu\")\ntopic = TopicId(topic_type, \"order-service\")","handlingStrategy":"validation","validationCode":"import re\n\n_TOPIC_TYPE_RE = re.compile(r\"^[\\w\\-\\.:\\=]+$\")\n\ndef is_valid_topic_type_str(t: str) -> bool:\n    return bool(_TOPIC_TYPE_RE.match(t))\n\nassert is_valid_topic_type_str(topic_type), f\"bad topic type: {topic_type!r}\"","typeGuard":null,"tryCatchPattern":"try:\n    topic = TopicId(topic_type, source)\nexcept ValueError as e:\n    topic_type = re.sub(r\"[^\\w\\-.:=]\", \"_\", topic_type)\n    topic = TopicId(topic_type, source)","preventionTips":["Sanitize any topic type derived from user input, file paths, or LLM output.","Never include '/' in a topic type — it is reserved as the type/source separator.","Centralize topic type creation behind one validated helper in your codebase."],"tags":["python","autogen-core","topics","pubsub","validation","cloudevents"],"backgroundTag":null,"analyzedSha":"027ecf0a379bcc1d09956d46d12d44a3ad9cee14","analyzedAt":"2026-08-15T03:38:00.719Z","schemaVersion":2},"datasetVersion":"2026-08-15T22:17:37.221Z"}