HKUDS/Vibe-Trading · error · ValueError

CashFlowSeries members must be CashFlow, got {type(item).__n

Error message

CashFlowSeries members must be CashFlow, got {type(item).__name__}

What it means

CashFlowSeries is a homogeneous container: every element of flows must be a CashFlow instance. A non-CashFlow member (dict, tuple, subclass-less duck-typed object) fails with the offending type name before the series can normalize currency and sort by date.

Source

Thrown at agent/src/entities/cashflow.py:230

    """

    flows: tuple[CashFlow, ...] = ()
    currency: str | None = None
    pre_translated: bool = False

    def __post_init__(self) -> None:
        """Validate membership and currency coherence, then order by date.

        Raises:
            ValueError: If a member is not a ``CashFlow``, or if
                ``pre_translated`` is set without naming a reporting currency.
            CurrencyMismatchError: If the flows span multiple currencies while
                ``pre_translated`` is False, or contradict a declared currency.
        """
        flows = tuple(self.flows)
        for item in flows:
            if not isinstance(item, CashFlow):
                raise ValueError(
                    f"CashFlowSeries members must be CashFlow, got "
                    f"{type(item).__name__}"
                )

        declared = (
            normalize_currency(self.currency) if self.currency is not None else None
        )
        present = {flow.currency for flow in flows}

        if self.pre_translated:
            if declared is None:
                raise ValueError(
                    "pre_translated=True asserts the flows were already converted, "
                    "so the reporting currency must be named explicitly via "
                    "currency=..."
                )
        else:
            if len(present) > 1:

View on GitHub (pinned to 80ffdda44c)

Solutions

  1. Map each row to CashFlow(...) before constructing the series
  2. If mixing types intentionally, filter to CashFlow instances first

Example fix

# before
CashFlowSeries(flows=raw_rows)  # list of dicts
# after
flows = [CashFlow(**r) for r in raw_rows]
CashFlowSeries(flows=flows)
Defensive patterns

Strategy: type-guard

Validate before calling

from agent.src.entities.cashflow import CashFlow
flows = [f for f in candidate_flows if isinstance(f, CashFlow)]
assert len(flows) == len(candidate_flows), 'non-CashFlow members present'

Type guard

from agent.src.entities.cashflow import CashFlow
def all_cashflows(seq) -> bool:
    return all(isinstance(f, CashFlow) for f in seq)

Try / catch

try:
    series = CashFlowSeries(flows=flows)
except ValueError as e:
    if 'must be CashFlow' in str(e):
        flows = [CashFlow(**f) for f in flows if not isinstance(f, CashFlow)]

Prevention

When it happens

Trigger: CashFlowSeries(flows=[{'amount': 100}, cashflow_instance]) or passing a list of raw dicts/tuples from a loader that never mapped them to entities.

Common situations: Forgetting to convert loaded rows to CashFlow objects; mixing entity types in a generic pipeline; a mock/stub object used in tests instead of the real dataclass.

Related errors


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