microsoft/semantic-kernel · 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

Raised by TopicId.__post_init__ when the 'type' field fails the regex ^[\w\-\.\:\=]+\Z (one or more of: word characters, hyphen, dot, colon, equals). An empty type, or one containing any other character (space, slash, @, etc.), raises ValueError. The pattern follows the CloudEvents type spec.

Source

Thrown at python/semantic_kernel/agents/runtime/core/topic.py:45

    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:
        """Validate the topic type and source."""
        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:
        """Convert the TopicId to a string."""
        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 c028a0c7dc)

Solutions

  1. Sanitize the type string so it matches ^[\w\-\.\:\=]+$.
  2. Replace disallowed characters (e.g. '/' -> '.' or '_', ' ' -> '_').
  3. Validate with is_valid_topic_type before constructing TopicId.

Example fix

// before
topic = TopicId(type='com.example/requests', source='abc')  # '/' not allowed -> ValueError

// after
from semantic_kernel.agents.runtime.core.topic import is_valid_topic_type
type_str = 'com.example.requests'  # sanitized
assert is_valid_topic_type(type_str)
topic = TopicId(type=type_str, source='abc')
Defensive patterns

Strategy: validation

Validate before calling

import re
from semantic_kernel.agents.runtime.core.topic import is_valid_topic_type

def safe_topic_type(value: str) -> str:
    cleaned = re.sub(r'[^\w\-.\:=]', '.', value)
    if not cleaned or not is_valid_topic_type(cleaned):
        raise ValueError('topic type is empty or still invalid after sanitization')
    return cleaned

Type guard

from semantic_kernel.agents.runtime.core.topic import is_valid_topic_type

def is_valid_topic(t: str) -> bool:
    return is_valid_topic_type(t)

Prevention

When it happens

Trigger: Constructing TopicId(type, source) where type contains disallowed characters (space, '/', '@', etc.) or is an empty string; deriving the type from free-form input or file paths that include '/'.

Common situations: Deriving topic type from user input, URLs, or file paths containing slashes/spaces; empty topic type from a missing config value.

Related errors


AI-assisted analysis of microsoft/semantic-kernel@c028a0c7dc (2026-08-13). Data as JSON: /api/errors/81a8183873e2d102. Report an issue: GitHub.