HKUDS/Vibe-Trading · error · Trading212ConfigError

invalid Trading 212 config at {path}: {exc}

Error message

invalid Trading 212 config at {path}: {exc}

What it means

Raised by load_config when the Trading 212 config file exists but cannot be read or parsed: reading raises OSError, or the JSON content is invalid (ValueError/JSONDecodeError), or Trading212Config.from_mapping rejects the payload. It chains the original exception so the underlying cause is preserved.

Source

Thrown at agent/src/trading/connectors/trading212/sdk.py:147

    cfg = Trading212Config.from_mapping(base)
    clean = {k: v for k, v in dict(overrides or {}).items() if k in _OVERRIDE_KEYS and v not in (None, "")}
    return cfg.with_overrides(**clean) if clean else cfg


def config_path() -> Path:
    """Return the user-level Trading 212 config path."""
    return get_runtime_root() / CONFIG_FILENAME


def load_config() -> Trading212Config:
    """Load Trading 212 settings from ``~/.vibe-trading/trading212.json``."""
    path = config_path()
    if not path.exists():
        return Trading212Config()
    try:
        return Trading212Config.from_mapping(json.loads(path.read_text(encoding="utf-8")))
    except (OSError, ValueError, json.JSONDecodeError) as exc:
        raise Trading212ConfigError(f"invalid Trading 212 config at {path}: {exc}") from exc


def save_config(config: Trading212Config) -> Path:
    """Persist Trading 212 settings with owner-only permissions."""
    path = config_path()
    path.parent.mkdir(parents=True, exist_ok=True)
    path.write_text(json.dumps(asdict(config), indent=2, ensure_ascii=False) + "\n", encoding="utf-8")
    try:
        path.chmod(0o600)
    except OSError:
        pass
    return path


def check_status(config: Trading212Config | None = None) -> dict[str, Any]:
    """Check REST readiness and config completeness without mutating broker state."""
    cfg = config or load_config()
    report: dict[str, Any] = {

View on GitHub (pinned to 80ffdda44c)

Solutions

  1. Inspect the {path} in the message and run `python -m json.tool <path>` to find the JSON syntax error
  2. Fix or restore the file's contents to a valid Trading212Config mapping
  3. If keys were rejected, compare against Trading212Config.from_mapping's expected field names
  4. As a last resort, delete/rename the file — load_config returns a default Trading212Config when it does not exist

Example fix

# before (config.json contains: {"api_key": "k",})
# after
{"api_key": "k"}
Defensive patterns

Strategy: validation

Validate before calling

import json
from pathlib import Path

def config_file_ok(path: Path) -> bool:
    if not path.exists():
        return True  # load_config returns defaults
    try:
        json.loads(path.read_text(encoding="utf-8"))
    except (OSError, ValueError):
        return False
    return True

Try / catch

try:
    config = load_config()
except Trading212ConfigError as exc:
    logger.error("bad config file: %s", exc)
    config = Trading212Config()  # fall back to defaults / re-run setup

Prevention

When it happens

Trigger: Calling build_config, check_status, get_account_snapshot, get_account_cash, get_account_metadata, or get_positions when the config file at config_path() exists but contains malformed JSON, is unreadable (permissions, deleted mid-read), or has keys/values that fail Trading212Config.from_mapping validation.

Common situations: Hand-edited config file with a trailing comma or typo; empty file created accidentally; config written by an old version with renamed keys; file with wrong ownership/permissions; BOM or non-UTF8 encoding.

Related errors


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