@@ -32,6 +32,61 @@ def test_llm_missing_tokens(monkeypatch, capsys):
3232 assert "Max input tokens for model" in captured
3333
3434
35+ def test_llm_missing_tokens_sets_defaulted_flag (monkeypatch ):
36+ """An unknown model must record that the 8192 window is a fallback.
37+
38+ The warning is only a log line, so a caller reading ``model_token`` sees
39+ 8192 and cannot tell whether that is the model's real limit or the
40+ default. A silent 8192 window truncates long pages and changes the answer
41+ without failing, which is the failure mode reported in #1121.
42+ """
43+ from scrapegraphai .graphs import abstract_graph
44+
45+ monkeypatch .setattr (
46+ abstract_graph , "models_tokens" , {"openai" : {"gpt-3.5-turbo" : 4096 }}
47+ )
48+ llm_config = {"model" : "openai/not-known-model" , "openai_api_key" : "test" }
49+ with patch .object (TestGraph , "_create_graph" , return_value = Mock (nodes = [])):
50+ graph = TestGraph ("Test prompt" , {"llm" : llm_config })
51+
52+ assert graph .model_token == 8192
53+ assert graph .model_tokens_defaulted is True
54+
55+
56+ def test_known_model_does_not_set_defaulted_flag (monkeypatch ):
57+ """A model with a known limit must not be flagged as defaulted."""
58+ from scrapegraphai .graphs import abstract_graph
59+
60+ monkeypatch .setattr (
61+ abstract_graph , "models_tokens" , {"openai" : {"gpt-3.5-turbo" : 4096 }}
62+ )
63+ llm_config = {"model" : "openai/gpt-3.5-turbo" , "openai_api_key" : "test" }
64+ with patch .object (TestGraph , "_create_graph" , return_value = Mock (nodes = [])):
65+ graph = TestGraph ("Test prompt" , {"llm" : llm_config })
66+
67+ assert graph .model_token == 4096
68+ assert graph .model_tokens_defaulted is False
69+
70+
71+ def test_explicit_model_tokens_does_not_set_defaulted_flag (monkeypatch ):
72+ """An explicit model_tokens is authoritative, not a fallback."""
73+ from scrapegraphai .graphs import abstract_graph
74+
75+ monkeypatch .setattr (
76+ abstract_graph , "models_tokens" , {"openai" : {"gpt-3.5-turbo" : 4096 }}
77+ )
78+ llm_config = {
79+ "model" : "openai/not-known-model" ,
80+ "openai_api_key" : "test" ,
81+ "model_tokens" : 1000000 ,
82+ }
83+ with patch .object (TestGraph , "_create_graph" , return_value = Mock (nodes = [])):
84+ graph = TestGraph ("Test prompt" , {"llm" : llm_config })
85+
86+ assert graph .model_token == 1000000
87+ assert graph .model_tokens_defaulted is False
88+
89+
3590def test_burr_kwargs ():
3691 """Test that burr_kwargs configuration correctly sets use_burr and burr_config on the graph."""
3792 dummy_graph = Mock ()
0 commit comments