ScrapeGraphAI/Scrapegraph-ai · error · KeyError

model_tokens not specified

Error message

model_tokens not specified

What it means

Thrown by AbstractGraph._create_llm when the config's llm dict contains a 'model_instance' (a pre-built LangChain chat model) but no 'model_tokens' key. ScrapeGraphAI needs the token limit of the model to size prompts and truncation, and it cannot look it up from an arbitrary instance, so it requires you to state it explicitly. The original KeyError is re-raised with the friendlier message 'model_tokens not specified'.

Source

Thrown at scrapegraphai/graphs/abstract_graph.py:155

        rate_limit_params = llm_params.pop("rate_limit", {})

        if rate_limit_params:
            requests_per_second = rate_limit_params.get("requests_per_second")
            max_retries = rate_limit_params.get("max_retries")
            if requests_per_second is not None:
                with warnings.catch_warnings():
                    warnings.simplefilter("ignore")
                    llm_params["rate_limiter"] = InMemoryRateLimiter(
                        requests_per_second=requests_per_second
                    )
            if max_retries is not None:
                llm_params["max_retries"] = max_retries

        if "model_instance" in llm_params:
            try:
                self.model_token = llm_params["model_tokens"]
            except KeyError as exc:
                raise KeyError("model_tokens not specified") from exc
            return llm_params["model_instance"]

        known_providers = {
            "openai",
            "azure_openai",
            "google_genai",
            "google_vertexai",
            "ollama",
            "oneapi",
            "nvidia",
            "groq",
            "anthropic",
            "bedrock",
            "mistralai",
            "hugging_face",
            "deepseek",
            "ernie",
            "fireworks",

View on GitHub (pinned to 532dfffbf6)

Solutions

  1. Add 'model_tokens' next to 'model_instance' in the llm config, e.g. {'model_instance': llm, 'model_tokens': 128000}.
  2. Check scrapegraphai/models/models_tokens.py for your model's token count and use that value.
  3. Alternatively drop model_instance and pass 'model_provider' + 'model' so the library builds and measures the model itself.

Example fix

# before
llm = ChatOpenAI(model='gpt-4o', api_key=key)
graph = SmartScraperGraph(prompt=..., config={'llm': {'model_instance': llm}})

# after
llm = ChatOpenAI(model='gpt-4o', api_key=key)
graph = SmartScraperGraph(prompt=..., config={'llm': {'model_instance': llm, 'model_tokens': 128000}})
Defensive patterns

Strategy: validation

Validate before calling

llm_cfg = config.get('llm', {})
if 'model_instance' in llm_cfg and 'model_tokens' not in llm_cfg:
    raise SystemExit("llm config with model_instance must also set model_tokens (see models_tokens.py)")

Type guard

def has_model_tokens(cfg: dict) -> bool:
    llm = cfg.get('llm', {})
    return 'model_instance' not in llm or isinstance(llm.get('model_tokens'), int)

Try / catch

try:
    graph = SmartScraperGraph(prompt=p, config=config)
except KeyError as e:
    if 'model_tokens' in str(e):
        config['llm']['model_tokens'] = 128000
        graph = SmartScraperGraph(prompt=p, config=config)
    else:
        raise

Prevention

When it happens

Trigger: Passing config = {'llm': {'model_instance': ChatOpenAI(...)}} without a 'model_tokens' entry. Any graph constructor (SmartScraperGraph, SearchGraph, ...) whose llm config includes model_instance but omits model_tokens hits this immediately in __init__.

Common situations: Users migrating from older versions where model_tokens was optional or inferred; copy-pasting examples that build a custom ChatOpenAI/ChatAnthropic instance but forgetting the token metadata; using a fine-tuned or self-hosted model whose name is not in models_tokens.

Related errors


AI-assisted analysis of ScrapeGraphAI/Scrapegraph-ai@532dfffbf6 (2026-08-28). Data as JSON: /api/errors/9813907726fa2e00. Report an issue: GitHub.