HKUDS/Vibe-Trading · error · Trading212APIError
Trading 212 API returned invalid JSON.
Error message
Trading 212 API returned invalid JSON.
What it means
Raised by _request when the HTTP response has content but response.json() raises ValueError, i.e. the server returned a 2xx body that is not valid JSON. The chained exception holds the parser detail.
Source
Thrown at agent/src/trading/connectors/trading212/sdk.py:438
url,
headers=headers,
auth=auth,
params=dict(params or {}),
timeout=config.timeout,
)
except requests.RequestException as exc:
raise Trading212APIError(f"Trading 212 request failed: {exc}") from exc
if response.status_code in (401, 403):
raise Trading212APIError("Trading 212 API authentication failed: check api_key/api_secret.")
if response.status_code >= 400:
raise Trading212APIError(f"Trading 212 API returned HTTP {response.status_code}: {_error_message(response)}")
if not response.content:
return None
try:
return response.json()
except ValueError as exc:
raise Trading212APIError("Trading 212 API returned invalid JSON.") from exc
def _error_message(response: requests.Response) -> str:
try:
payload = response.json()
except ValueError:
return response.text.strip() or response.reason or "request failed"
if isinstance(payload, Mapping):
for key in ("message", "error", "detail", "title"):
value = payload.get(key)
if value:
return str(value)
return str(payload)
def _missing_fields(config: Trading212Config) -> list[str]:
missing = []
if not config.api_key:View on GitHub (pinned to 80ffdda44c)
Solutions
- Retry the request — truncated/garbled responses are often transient
- Log response headers and a body snippet to identify what non-JSON payload came back (proxy page vs API body)
- If a proxy/WAF is intercepting, bypass or configure it for the Trading 212 host
- Report/check the Trading 212 status page if it persists account-wide
Defensive patterns
Strategy: try-catch
Try / catch
try:
data = get_account_metadata(config)
except Trading212APIError as exc:
if "invalid JSON" in str(exc):
data = get_account_metadata(config) # one retry; often transient truncation
else:
raise Prevention
- Retry once on invalid-JSON responses before escalating
- Log response Content-Type/headers when debugging proxy interference
- Avoid environments with SSL-inspecting proxies for API traffic
When it happens
Trigger: Any _get call where the API (or an intermediary proxy) returns 200 with an HTML error page, plain-text body, truncated JSON, or a gateway interstitial instead of JSON.
Common situations: Captive portal / proxy injecting an HTML page; CDN or WAF challenge page; truncated response on flaky connections; API incident returning non-JSON payloads.
Understand the failure class
Background: "Invalid JSON response" and "Failed to parse response" errors: when an API answers 200 but the body isn't the JSON your library expected — this error's family across 28 libraries.
- Parsing and encoding errors: unexpected token, malformed input — why parsers reject input and how to find the real culprit.
Related errors
- invalid JSON response: {exc}
- invalid Trading 212 config at {path}: {exc}
- Trading 212 request failed: {exc}
- Trading 212 API returned HTTP {response.status_code}: {_erro
- Feishu QR login requires a JSON agent config; use ~/.vibe-tr
AI-assisted analysis of HKUDS/Vibe-Trading@80ffdda44c (2026-08-28).
Data as JSON: /api/errors/152423ce2860dd9d.
Report an issue: GitHub.