{"record":{"id":"9813907726fa2e00","repo":"ScrapeGraphAI/Scrapegraph-ai","slug":"model-tokens-not-specified","errorCode":null,"errorMessage":"model_tokens not specified","messagePattern":"model_tokens not specified","errorType":"exception","errorClass":"KeyError","httpStatus":null,"severity":"error","filePath":"scrapegraphai/graphs/abstract_graph.py","lineNumber":155,"sourceCode":"        rate_limit_params = llm_params.pop(\"rate_limit\", {})\n\n        if rate_limit_params:\n            requests_per_second = rate_limit_params.get(\"requests_per_second\")\n            max_retries = rate_limit_params.get(\"max_retries\")\n            if requests_per_second is not None:\n                with warnings.catch_warnings():\n                    warnings.simplefilter(\"ignore\")\n                    llm_params[\"rate_limiter\"] = InMemoryRateLimiter(\n                        requests_per_second=requests_per_second\n                    )\n            if max_retries is not None:\n                llm_params[\"max_retries\"] = max_retries\n\n        if \"model_instance\" in llm_params:\n            try:\n                self.model_token = llm_params[\"model_tokens\"]\n            except KeyError as exc:\n                raise KeyError(\"model_tokens not specified\") from exc\n            return llm_params[\"model_instance\"]\n\n        known_providers = {\n            \"openai\",\n            \"azure_openai\",\n            \"google_genai\",\n            \"google_vertexai\",\n            \"ollama\",\n            \"oneapi\",\n            \"nvidia\",\n            \"groq\",\n            \"anthropic\",\n            \"bedrock\",\n            \"mistralai\",\n            \"hugging_face\",\n            \"deepseek\",\n            \"ernie\",\n            \"fireworks\",","sourceCodeStart":137,"sourceCodeEnd":173,"githubUrl":"https://github.com/ScrapeGraphAI/Scrapegraph-ai/blob/532dfffbf6ee823a6c9cf8cfedc24a93bf026780/scrapegraphai/graphs/abstract_graph.py#L137-L173","documentation":"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'.","triggerScenarios":"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__.","commonSituations":"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.","solutions":["Add 'model_tokens' next to 'model_instance' in the llm config, e.g. {'model_instance': llm, 'model_tokens': 128000}.","Check scrapegraphai/models/models_tokens.py for your model's token count and use that value.","Alternatively drop model_instance and pass 'model_provider' + 'model' so the library builds and measures the model itself."],"exampleFix":"# before\nllm = ChatOpenAI(model='gpt-4o', api_key=key)\ngraph = SmartScraperGraph(prompt=..., config={'llm': {'model_instance': llm}})\n\n# after\nllm = ChatOpenAI(model='gpt-4o', api_key=key)\ngraph = SmartScraperGraph(prompt=..., config={'llm': {'model_instance': llm, 'model_tokens': 128000}})","handlingStrategy":"validation","validationCode":"llm_cfg = config.get('llm', {})\nif 'model_instance' in llm_cfg and 'model_tokens' not in llm_cfg:\n    raise SystemExit(\"llm config with model_instance must also set model_tokens (see models_tokens.py)\")","typeGuard":"def has_model_tokens(cfg: dict) -> bool:\n    llm = cfg.get('llm', {})\n    return 'model_instance' not in llm or isinstance(llm.get('model_tokens'), int)","tryCatchPattern":"try:\n    graph = SmartScraperGraph(prompt=p, config=config)\nexcept KeyError as e:\n    if 'model_tokens' in str(e):\n        config['llm']['model_tokens'] = 128000\n        graph = SmartScraperGraph(prompt=p, config=config)\n    else:\n        raise","preventionTips":["Always pair model_instance with an explicit model_tokens integer.","Look up token limits in scrapegraphai/models/models_tokens.py rather than guessing.","Wrap graph construction in a config-validation helper for user-supplied configs."],"tags":["configuration","llm","model-instance","token-limit"],"backgroundTag":"missing-required-config-option","analyzedSha":"532dfffbf6ee823a6c9cf8cfedc24a93bf026780","analyzedAt":"2026-08-28T15:19:38.821Z","schemaVersion":2},"datasetVersion":"2026-08-28T16:17:29.566Z"}