Skip to content

fix: pop model_tokens so it is not forwarded to the model client#1100

Merged
VinciGit00 merged 1 commit into
ScrapeGraphAI:mainfrom
dylanpfirn:fix/model-tokens-leak
Jul 8, 2026
Merged

fix: pop model_tokens so it is not forwarded to the model client#1100
VinciGit00 merged 1 commit into
ScrapeGraphAI:mainfrom
dylanpfirn:fix/model-tokens-leak

Conversation

@dylanpfirn

Copy link
Copy Markdown
Contributor

Description

Fixes #1099.

On the plain llm-config path, AbstractGraph._create_llm() reads model_tokens into self.model_token but never removes it from llm_params. llm_params is then splatted into init_chat_model(**llm_params), so model_tokens is forwarded to the model client. With a ChatOpenAI client (any OpenAI-compatible base_url) this surfaces as:

TypeError: Completions.create() got an unexpected keyword argument 'model_tokens'. Did you mean 'max_tokens'?

This one-line change pops model_tokens after it is consumed, so it can't leak to the client. It mirrors the existing model_instance path, which already returns before the key can reach a client (and is why that path works today).

Repro (before this patch)

from scrapegraphai.graphs import SmartScraperGraph

graph_config = {
    "llm": {
        "api_key": "<key>",
        "model": "openai/gpt-4o-mini",
        "base_url": "https://<any-openai-compatible-endpoint>/v1",
        "model_tokens": 128000,
    },
}
SmartScraperGraph(prompt="Summarize", source="https://example.com", config=graph_config).run()

Change

else:
    self.model_token = llm_params["model_tokens"]

# Consumed by ScrapeGraphAI; must not be forwarded to the model client.
llm_params.pop("model_tokens", None)

Type of change

  • Bug fix (non-breaking change which fixes an issue)

Notes

Verified locally against an OpenAI-compatible endpoint (Cloudflare AI Gateway /compat): with the patch, passing model_tokens via plain config no longer raises and the value is correctly used for chunk sizing.

On the plain llm-config path, _create_llm read model_tokens but never
removed it from llm_params, so it was splatted into init_chat_model and
forwarded to the client. With ChatOpenAI (any OpenAI-compatible base_url)
this surfaced as: Completions.create() got an unexpected keyword argument
'model_tokens'. Pop the key after consuming it, matching the model_instance
path which already returns before the key can leak.

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
@dosubot dosubot Bot added size:XS This PR changes 0-9 lines, ignoring generated files. bug Something isn't working labels Jul 8, 2026
@dosubot dosubot Bot added the lgtm This PR has been approved by a maintainer label Jul 8, 2026
@VinciGit00 VinciGit00 merged commit d2b970c into ScrapeGraphAI:main Jul 8, 2026
4 checks passed
github-actions Bot pushed a commit that referenced this pull request Jul 8, 2026
## [2.1.5](v2.1.4...v2.1.5) (2026-07-08)

### Bug Fixes

* pop model_tokens so it is not forwarded to the model client ([#1100](#1100)) ([d2b970c](d2b970c))

### Docs

* **readme:** add Open Source vs Managed API comparison ([#1091](#1091)) ([ef3523b](ef3523b))

### CI

* run only deterministic unit suites in Test Suite workflow ([#1095](#1095)) ([037a42e](037a42e))
@github-actions

github-actions Bot commented Jul 8, 2026

Copy link
Copy Markdown

🎉 This PR is included in version 2.1.5 🎉

The release is available on:

Your semantic-release bot 📦🚀

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

bug Something isn't working lgtm This PR has been approved by a maintainer released on @stable size:XS This PR changes 0-9 lines, ignoring generated files.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

model_tokens leaks to OpenAI client on plain llm config path (TypeError: unexpected keyword argument 'model_tokens')

2 participants