HKUDS/Vibe-Trading · error · ValueError

{field_name} cannot contain whitespace, got {value!r}

Error message

{field_name} cannot contain whitespace, got {value!r}

What it means

normalize_currency uppercases and strips the code but refuses codes containing internal whitespace (e.g. 'US D'), since whitespace indicates a malformed code or a wrong field was passed.

Source

Thrown at agent/src/entities/models.py:91

    ``USDT``, and rejecting them would push callers into faking a code.

    Args:
        value: Raw currency code, e.g. ``" usd "``.
        field_name: Name used in the error message, for caller context.

    Returns:
        The canonical code, e.g. ``"USD"``.

    Raises:
        ValueError: If the code is empty, not a string, or contains whitespace.
    """
    if not isinstance(value, str):
        raise ValueError(f"{field_name} must be a string, got {type(value).__name__}")
    cleaned = value.strip().upper()
    if not cleaned:
        raise ValueError(f"{field_name} is required and cannot be empty")
    if any(ch.isspace() for ch in cleaned):
        raise ValueError(f"{field_name} cannot contain whitespace, got {value!r}")
    return cleaned


def normalize_date(value: date | datetime | str, *, field_name: str = "date") -> date:
    """Coerce a supported date input to a plain ``datetime.date``.

    ``datetime`` instances are truncated to their date, because a cash flow
    settles on a day, not at a timestamp; keeping the time component would make
    two flows on the same day compare unequal for no economic reason.

    Strings must be ISO-8601 (``YYYY-MM-DD``, optionally with a time part).
    Ambiguous regional formats such as ``03/04/2024`` are rejected rather than
    guessed, since guessing day-first vs month-first is a silent wrong answer.

    Args:
        value: A ``date``, a ``datetime``, or an ISO-8601 string.
        field_name: Name used in the error message, for caller context.

View on GitHub (pinned to 80ffdda44c)

Solutions

  1. Pass the ISO alpha-3 code only ('USD'), not a name
  2. Strip/normalize upstream: code.replace('\xa0','').split()[0] if safe
  3. Map names to codes before constructing models

Example fix

# before
EntityRate(currency='US Dollars', ...)
# after
EntityRate(currency='USD', ...)
Defensive patterns

Strategy: validation

Validate before calling

ccy = ''.join(raw.split()).upper()
assert ' ' not in ccy

Type guard

def is_valid_currency(v: str) -> bool:
    v = v.strip().upper()
    return len(v) == 3 and v.isalpha()

Try / catch

except ValueError as e:
    if 'whitespace' in str(e): map name→ISO code and retry

Prevention

When it happens

Trigger: Passing a currency string with embedded whitespace such as 'US D', 'usd ', or accidentally a full label like 'US Dollars' that contains spaces after stripping.

Common situations: Passing human-readable currency names instead of codes, or copy-paste artifacts with non-breaking spaces.

Understand the failure class

Background: "Must be a positive integer", "Invalid value", "Unsupported": the invalid-argument-value error family, when a library rejects the value you pass — this error's family across 35 libraries.

Related errors


AI-assisted analysis of HKUDS/Vibe-Trading@80ffdda44c (2026-08-28). Data as JSON: /api/errors/3c75631afe918be2. Report an issue: GitHub.