OpenBB-finance/OpenBB · error · ValueError

Trace '{trace}' not found

Error message

Trace '{trace}' not found

What it means

In OpenBBFigure.add_legend_label (openbb_figure.py:798), when a `trace` name is supplied the method iterates self.data looking for a trace whose .name matches; if the for-loop completes without a break, the else-branch raises `ValueError(f"Trace '{trace}' not found")`. The label would have inherited the target trace's mode/marker/yaxis, so an unknown name cannot be attached.

Source

Thrown at openbb_platform/obbject_extensions/charting/openbb_charting/core/openbb_figure.py:798

        ValueError
            If trace is not found
        ValueError
            If label is not specified and trace is not specified
        """
        if trace:
            for trace_ in self.data:
                if trace_.name == trace:  # type: ignore
                    for arg, default in zip(
                        [label, mode, marker, line_dash],
                        [trace, trace_.mode, trace_.marker, trace_.line_dash],  # type: ignore
                    ):
                        if not arg and default:
                            arg = default  # noqa: PLW2901

                    kwargs.update(dict(yaxis=trace_.yaxis))  # type: ignore
                    break
            else:
                raise ValueError(f"Trace '{trace}' not found")

        if not label:
            raise ValueError("Label must be specified")

        self.add_scatter(
            x=[None],
            y=[None],
            mode=mode or "lines",
            name=label,
            marker=marker or dict(),
            line_dash=line_dash or "solid",
            legendrank=legendrank,
            **kwargs,
        )

    def show(  # noqa: PLR0915
        self,
        *args,

View on GitHub (pinned to 3e071fcc2c)

Solutions

  1. List existing names and use one verbatim: print([t.name for t in fig.data]).
  2. Ensure the target scatter exists first — call add_scatter/add_candle before add_legend_label.
  3. If you don't need style inheritance, call add_legend_label(label="...") without `trace` (label alone is valid).
  4. Guard the call: if trace not in {t.name for t in fig.data}: skip or log.

Example fix

# before
fig.add_legend_label(trace="close", label="Support")  # ValueError: Trace 'close' not found

# after
names = [t.name for t in fig.data]
if "close" in names:
    fig.add_legend_label(trace="close", label="Support")
else:
    fig.add_legend_label(label="Support")
Defensive patterns

Strategy: validation

Validate before calling

trace_names = {t.name for t in fig.data if t.name}
if trace not in trace_names:
    print(f"available traces: {sorted(trace_names)}")
else:
    fig.add_legend_label(trace=trace, label="Support")

Type guard

def trace_exists(fig, name: str) -> bool:
    return any(t.name == name for t in fig.data)

Try / catch

try:
    fig.add_legend_label(trace=name, label=text)
except ValueError as e:
    if "not found" in str(e):
        logger.warning("skipping legend label for missing trace %s", name)
    else:
        raise

Prevention

When it happens

Trigger: Calling fig.add_legend_label(trace="AAPL Close", label="...") when no trace in fig.data has name exactly "AAPL Close" — wrong casing, renamed traces, or calling before the scatter was added.

Common situations: Trace names built dynamically (symbol + column concatenation) that drift from what was plotted; calling add_legend_label on a fresh figure before add_scatter; traces whose name is None (unnamed) so the equality never holds.

Related errors


AI-assisted analysis of OpenBB-finance/OpenBB@3e071fcc2c (2026-08-14). Data as JSON: /api/errors/c90c68eeabe1f01e. Report an issue: GitHub.