fix(tools): send a start date that is not a date back to the model (#1476)

- get_news and get_stock_data answer an unreadable start_date with the form to use, instead of passing it to a vendor
This commit is contained in:
Yijia-Xiao
2026-10-03 21:27:43 +00:00
parent b3ca569c4c
commit f72382414c
3 changed files with 31 additions and 4 deletions
+17
View File
@@ -135,6 +135,23 @@ def test_direct_call_without_state_is_unchanged():
assert routed.call_args.args == ("get_news", "AAPL", "2026-09-01", "2026-09-08")
@pytest.mark.unit
@pytest.mark.parametrize("tool", ["get_news", "get_stock_data"])
def test_a_start_date_that_is_not_a_date_is_sent_back_to_the_model(tool):
# A model can append a stray character to a date (#1476). No window can be
# guessed from it, so the vendor is not called and the model is told the form.
with mock.patch.object(tools, "route_to_vendor", return_value="ok") as routed:
out = getattr(tools, tool).func("NVDA", "2026-09-26\u0629", "2026-10-03", trade_date="2026-10-03")
assert not routed.called
assert "YYYY-MM-DD" in out and "start_date" in out
@pytest.mark.unit
def test_a_window_whose_start_is_not_a_date_is_refused():
with pytest.raises(ValueError, match="start_date"):
as_of_window("2026-09-26\u0629", "2026-10-03", "2026-10-03")
# --- the run date itself (#1319) -------------------------------------------------
@pytest.mark.unit
+8 -2
View File
@@ -31,7 +31,10 @@ def get_stock_data(
Returns:
str: A formatted dataframe containing the price data for the instrument over the date range.
"""
start_date, end_date = as_of_window(start_date, end_date, trade_date)
try:
start_date, end_date = as_of_window(start_date, end_date, trade_date)
except ValueError as e:
return str(e)
return route_to_vendor("get_stock_data", symbol, start_date, end_date)
@@ -181,7 +184,10 @@ def get_news(
Returns:
str: A formatted string containing news data
"""
start_date, end_date = as_of_window(start_date, end_date, trade_date)
try:
start_date, end_date = as_of_window(start_date, end_date, trade_date)
except ValueError as e:
return str(e)
return route_to_vendor("get_news", ticker, start_date, end_date)
+6 -2
View File
@@ -92,10 +92,14 @@ def as_of_window(start_date: str, end_date: str, trade_date: str) -> tuple[str,
"""``[start, end]`` with its end clamped to the run date.
A window wholly after the run date keeps its length and moves back to end there.
An unreadable end reads as the run date, but no window can be guessed from an
unreadable start, so it raises ValueError for the caller to send back (#1476).
"""
end = as_of(end_date, trade_date)
start, old_end = _parse(start_date), _parse(end_date)
if end == end_date or start is None or start <= _parse(end):
if start is None:
raise ValueError(f"start_date {start_date!r} is not a date; give it as YYYY-MM-DD")
end = as_of(end_date, trade_date)
if end == end_date or start <= _parse(end):
return start_date, end
span = (old_end - start) if old_end is not None and old_end >= start else timedelta(0)
return f"{_parse(end) - span:%Y-%m-%d}", end