Skip to content

"Vibe-Trading: Your Personal Trading Agent"

fix(options-payoff): return an error envelope when the result overflows to inf

Rudy Celekli912a2c4ed280
diff --git a/agent/src/tools/options_payoff_tool.py b/agent/src/tools/options_payoff_tool.py
index b4b117e6e..7b7f1da32 100644
--- a/agent/src/tools/options_payoff_tool.py
+++ b/agent/src/tools/options_payoff_tool.py
@@ -222,7 +222,12 @@ class OptionsPayoffTool(BaseTool):
"Scenario P&L is mark-to-market and does not deduct a hypothetical exit commission.",
],
}
- return json.dumps(payload, ensure_ascii=False, allow_nan=False)
+ try:
+ return json.dumps(payload, ensure_ascii=False, allow_nan=False)
+ except ValueError as exc:
+ # Extreme finite inputs (e.g. a ~1e308 multiplier) overflow to inf
+ # in the results; report that instead of raising out of execute().
+ return _error(f"non-serializable numeric result: {exc}")
def _coerce_legs(raw: Any) -> list[OptionLeg]:
diff --git a/agent/tests/test_options_payoff_tool.py b/agent/tests/test_options_payoff_tool.py
index 6c2790d76..ac32ce4d2 100644
--- a/agent/tests/test_options_payoff_tool.py
+++ b/agent/tests/test_options_payoff_tool.py
@@ -134,3 +134,16 @@ def test_tool_returns_error_envelopes(kwargs: dict[str, object], expected: str)
payload = json.loads(OptionsPayoffTool().execute(**kwargs))
assert payload["status"] == "error"
assert expected in payload["error"]
+
+
+def test_overflowing_result_returns_error_envelope_not_exception() -> None:
+ """A finite but extreme multiplier overflows the P&L to inf; it must not raise."""
+ raw = OptionsPayoffTool().execute(
+ legs=[{"option_type": "call", "strike": 100.0, "qty": 10}],
+ entry_spot=100.0,
+ expiry_days=30,
+ multiplier=1e308,
+ )
+ payload = json.loads(raw)
+ assert payload["status"] == "error"
+ assert "non-serializable" in payload["error"]