Skip to content

"Vibe-Trading: Your Personal Trading Agent"

fix(robinhood): reject malformed portfolio buying power

lorenzozanee0fff715f80b1
diff --git a/agent/src/trading/connectors/robinhood/mcp.py b/agent/src/trading/connectors/robinhood/mcp.py
index d7b421b5d..beaca2fa8 100644
--- a/agent/src/trading/connectors/robinhood/mcp.py
+++ b/agent/src/trading/connectors/robinhood/mcp.py
@@ -189,15 +189,13 @@ def portfolio_summary(envelope: Any) -> dict[str, Any]:
if not isinstance(currency, str) or not currency.strip():
raise RobinhoodShapeError(f"{tool} currency is missing")
buying_power = data.get("buying_power")
- raw_buying_power = buying_power.get("buying_power") if isinstance(buying_power, dict) else None
+ if not isinstance(buying_power, dict):
+ raise RobinhoodShapeError(f"{tool} buying_power is not an object")
+ raw_buying_power = buying_power.get("buying_power")
return {
"portfolio_value": _decimal_text(data.get("total_value"), "total_value", tool),
"cash": _decimal_text(data["cash"], "cash", tool) if data.get("cash") is not None else None,
- "buying_power": (
- _decimal_text(raw_buying_power, "buying_power.buying_power", tool)
- if raw_buying_power is not None
- else None
- ),
+ "buying_power": _decimal_text(raw_buying_power, "buying_power.buying_power", tool),
"currency": currency.strip().upper(),
"non_equity_values": {
field: _decimal_text(data[field], field, tool) if data.get(field) is not None else None
diff --git a/agent/tests/test_robinhood_mcp_mapping.py b/agent/tests/test_robinhood_mcp_mapping.py
index 46c5497d2..140a7c61e 100644
--- a/agent/tests/test_robinhood_mcp_mapping.py
+++ b/agent/tests/test_robinhood_mcp_mapping.py
@@ -55,6 +55,14 @@ def test_an_omitted_non_equity_value_is_unknown_not_zero() -> None:
assert mcp.portfolio_summary(envelope)["non_equity_values"]["crypto_value"] is None
+def test_portfolio_summary_rejects_an_unmapped_buying_power_shape() -> None:
+ envelope = rh.portfolio()
+ envelope["structured_content"]["data"]["buying_power"] = ["5000.00"]
+
+ with pytest.raises(mcp.RobinhoodShapeError, match="buying_power is not an object"):
+ mcp.portfolio_summary(envelope)
+
+
def test_position_rows_map_symbol_quantity_and_cost() -> None:
rows = mcp.position_rows(
rh.positions(