HKUDS/Vibe-Trading · error · TushareFallbackUnavailable

TUSHARE_TOKEN is not configured

Error message

TUSHARE_TOKEN is not configured

What it means

This error is raised by _pro_api in agent/src/tools/tushare_fallbacks.py when the configured TUSHARE_TOKEN is empty or a placeholder value. The Tushare fallback data source requires a valid API token before it can construct the pro_api client. Without it, all fallback fetchers (fund flow, dragon tiger, northbound flow, margin trading) are unusable.

Source

Thrown at agent/src/tools/tushare_fallbacks.py:25

from __future__ import annotations

from datetime import date, timedelta
from typing import Any

from src.config.accessor import get_env_config

_TUSHARE_TOKEN_PLACEHOLDERS = {"", "your-tushare-token"}


class TushareFallbackUnavailable(RuntimeError):
    """Raised when the optional Tushare fallback cannot be used."""


def _pro_api() -> Any:
    token = get_env_config().data.tushare_token.strip()
    if token in _TUSHARE_TOKEN_PLACEHOLDERS:
        raise TushareFallbackUnavailable("TUSHARE_TOKEN is not configured")
    try:
        import tushare as ts
    except Exception as exc:  # noqa: BLE001 - import errors vary by install
        raise TushareFallbackUnavailable(f"tushare import failed: {exc}") from exc
    return ts.pro_api(token)


def _records(frame: Any) -> list[dict[str, Any]]:
    if frame is None:
        return []
    if bool(getattr(frame, "empty", False)):
        return []
    if hasattr(frame, "to_dict"):
        rows = frame.to_dict("records")
        return [row for row in rows if isinstance(row, dict)]
    if isinstance(frame, list):
        return [row for row in frame if isinstance(row, dict)]
    return []

View on GitHub (pinned to 80ffdda44c)

Solutions

  1. Set TUSHARE_TOKEN to a real token from tushare.pro in your environment / .env config
  2. Remove any placeholder value (do not leave 'TUSHARE_TOKEN' or similar) — the code treats placeholders as unset
  3. Verify with a quick check that get_env_config().data.tushare_token returns a non-empty, non-placeholder string before calling fallback fetchers
  4. Treat the failure as non-fatal: catch TushareFallbackUnavailable and degrade to the primary data source

Example fix

# before
TUSHARE_TOKEN=TUSHARE_TOKEN

# after
TUSHARE_TOKEN=<real token from tushare.pro>
Defensive patterns

Strategy: validation

Validate before calling

from agent.src.config import get_env_config  # adjust import to project layout

def tushare_ready() -> bool:
    token = get_env_config().data.tushare_token.strip()
    return bool(token) and token not in {'TUSHARE_TOKEN', 'your-token-here', 'changeme'}

if not tushare_ready():
    logger.warning('Tushare fallback disabled: TUSHARE_TOKEN not set')

Try / catch

try:
    data = fetch_fund_flow(sym, days=5)
except TushareFallbackUnavailable as exc:
    if 'TUSHARE_TOKEN' in str(exc):
        logger.warning('fallback disabled: %s', exc)
        data = primary_source(sym, days=5)
    else:
        raise

Prevention

When it happens

Trigger: Calling fetch_fund_flow, fetch_dragon_tiger, fetch_northbound_flow, or fetch_margin_trading when get_env_config().data.tushare_token is unset, blank, or one of the placeholder sentinel values in _TUSHARE_TOKEN_PLACEHOLDERS (e.g. the literal 'TUSHARE_TOKEN' or 'your-token-here').

Common situations: Fresh clone without a .env file; CI environment missing the TUSHARE_TOKEN secret; template config copied with placeholder token left in place; deployment where the env var name is misspelled or scoped to a different process.

Understand the failure class

Background: "environment variable is not set" and "Missing keys in environment" errors: what missing required env var messages mean and how to fix them — this error's family across 28 libraries.

Related errors


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