invoke-ai/InvokeAI · error · ValueError

Invalid regex: {e}

Error message

Invalid regex: {e}

What it means

validate_url_regex is a pydantic field validator that compiles the configured URL-filter regex with re.compile(); if re.error is raised the ValueError 'Invalid regex: {e}' surfaces. This guarantees the app never stores an un-compilable regex used for URL filtering.

Source

Thrown at invokeai/app/services/config/config_default.py:62

    "external_openai_api_key",
    "external_openai_base_url",
    "external_seedream_api_key",
    "external_seedream_base_url",
)


class URLRegexTokenPair(BaseModel):
    url_regex: str = Field(description="Regular expression to match against the URL")
    token: str = Field(description="Token to use when the URL matches the regex")

    @field_validator("url_regex")
    @classmethod
    def validate_url_regex(cls, v: str) -> str:
        """Validate that the value is a valid regex."""
        try:
            re.compile(v)
        except re.error as e:
            raise ValueError(f"Invalid regex: {e}")
        return v


class InvokeAIAppConfig(BaseSettings):
    """Invoke's global app configuration.

    Typically, you won't need to interact with this class directly. Instead, use the `get_config` function from `invokeai.app.services.config` to get a singleton config object.

    Attributes:
        host: IP address to bind to. Use `0.0.0.0` to serve to your local network.
        port: Port to bind to.
        allow_origins: Allowed CORS origins.
        allow_credentials: Allow CORS credentials.
        allow_methods: Methods allowed for CORS.
        allow_headers: Headers allowed for CORS.
        ssl_certfile: SSL certificate file for HTTPS. See https://www.uvicorn.dev/settings/#https.
        ssl_keyfile: SSL key file for HTTPS. See https://www.uvicorn.dev/settings/#https.
        log_tokenization: Enable logging of parsed prompt tokens.

View on GitHub (pinned to 0b6a024f2f)

Solutions

  1. Fix the regex syntax reported in the message (e.g. close the group, escape the bracket)
  2. Validate the pattern quickly in Python: re.compile(your_pattern) before putting it in config
  3. Prefer raw strings in YAML/ENV to preserve backslashes (use single quotes in YAML)

Example fix

// before (invokeai.yaml)
url_regex: '.*\.(png|jpg'   # unclosed group
// after
url_regex: '.*\.(png|jpg)$'
Defensive patterns

Strategy: validation

Validate before calling

import re
try:
    re.compile(cfg.url_regex)
except re.error as e:
    raise ValueError(f"fix url_regex in config: {e}")

Try / catch

try:
    InvokeAIAppConfig(url_regex=pattern)
except ValueError as e:
    if str(e).startswith('Invalid regex'):
        print('Fix the regex in invokeai.yaml:', e)

Prevention

When it happens

Trigger: Setting the url_regex config field (env var, config file, or CLI) to a syntactically invalid regex such as 'foo(' , '[unclosed' , or '*quantifier'.

Common situations: Hand-edited invokeai.yaml with an escaping mistake (e.g. '\.' vs '.') ; quoting issues where backslashes are eaten by YAML/env parsing; porting a JavaScript regex with unsupported syntax into Python.

Related errors


AI-assisted analysis of invoke-ai/InvokeAI@0b6a024f2f (2026-08-29). Data as JSON: /api/errors/b4f46aff88ffbb71. Report an issue: GitHub.