python-poetry/poetry · error · ValueError

Invalid package definition.

Error message

Invalid package definition.

What it means

ValueError from `_validate_package` (init.py:518-520) when the package requirement string splits into more than two whitespace tokens. A valid requirement is either `name` (1 token) or `name version` (2 tokens); three or more is treated as malformed.

Source

Thrown at src/poetry/console/commands/init.py:520

        author = combine_unicode(author or default)

        if author in ["n", "no"]:
            return None

        m = AUTHOR_REGEX.match(author)
        if not m:
            raise ValueError(
                "Invalid author string. Must be in the format: "
                "John Smith <john@example.com>"
            )

        return author

    @staticmethod
    def _validate_package(package: str | None) -> str | None:
        if package and len(package.split()) > 2:
            raise ValueError("Invalid package definition.")

        return package

    @staticmethod
    def _validate_version_constraint(constraint: str | None) -> str | None:
        from poetry.core.constraints.version import parse_constraint

        constraint = (constraint or "").strip()
        if not constraint:
            return None

        try:
            parse_constraint(constraint)
        except ValueError as e:
            raise ValueError(f"Invalid version constraint: {constraint}") from e

        return constraint

View on GitHub (pinned to 92b74dcfe3)

Solutions

  1. Provide just the package name and let init resolve the version, or use `name version` with no internal spaces.
  2. Keep operator and version together: `requests>=2.28` rather than `requests >= 2.28`.
  3. Add extras later via `poetry add 'requests[security]'`.

Example fix

# before
requests >= 2.28
# after
requests>=2.28
Defensive patterns

Strategy: validation

Validate before calling

req = "requests>=2.28"
if len(req.split()) > 2:
    raise SystemExit("use 'name' or 'name version' with no internal spaces")

Type guard

def is_valid_package_string(value: str | None) -> bool:
    return not value or len(value.split()) <= 2

Prevention

When it happens

Trigger: Entering `requests >= 2.28` (with a space between operator and version, yielding 3 tokens); pasting `requests extras security`.

Common situations: PEP 508 syntax (`requests[security]>=2.28`) being split on whitespace; users expecting to add extras in this prompt.

Related errors


AI-assisted analysis of python-poetry/poetry@92b74dcfe3 (2026-08-04). Data as JSON: /data/errors/594da91abb77a4fc.json. Report an issue: GitHub.