Skip to content

unexpected keyword argument 'model_tokens' for Anthropic model #1002

@andywatkins-ancoris

Description

@andywatkins-ancoris

Describe the bug
Using "model": "anthropic/claude-3-7-sonnet-latest", I got a warning message saying I needed to specify model_tokens.

Then, when I did specify model_tokens, I got an error saying
"Messages.create() got an unexpected keyword argument 'model_tokens'"

To Reproduce
I'm following the tutorial at https://scrapegraph-ai.readthedocs.io/en/latest/getting_started/examples.html
but have swapped out the model.

graph_config = {
    "llm": {
        # "api_key": openai_key,
        "api_key": anthropic_key,
        # "model": "openai/gpt-4o",
        "model": "anthropic/claude-3-7-sonnet-latest",
    },
}

When I ran this, the output started like so:

Max input tokens for model anthropic/claude-3-7-sonnet-latest not found,
                    please specify the model_tokens parameter in the llm section of the graph configuration.
                    Using default token size: 8192

So, I modified my graph_config to include model_tokens, like this, expecting this might solve the problem:

graph_config = {
    "llm": {
        # "api_key": openai_key,
        "api_key": anthropic_key,
        # "model": "openai/gpt-4o",
        "model": "anthropic/claude-3-7-sonnet-latest",
        "model_tokens": 4000,   # ADDED model_tokens TO RESOLVE THE ABOVE ERROR
    },
}

When I ran the program again, I got a Python stacktrace saying

TypeError: Messages.create() got an unexpected keyword argument 'model_tokens'

here's the whole stacktrace (I've redacted personal information from the path)

Error during chain execution: Messages.create() got an unexpected keyword argument 'model_tokens'
Traceback (most recent call last):
  File "/redacted_path/scrapegraph.py", line 51, in <module>
    result = smart_scraper_graph.run()
             ^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/redacted_path/venv/lib/python3.12/site-packages/scrapegraphai/graphs/smart_scraper_graph.py", line 296, in run
    self.final_state, self.execution_info = self.graph.execute(inputs)
                                            ^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/redacted_path/venv/lib/python3.12/site-packages/scrapegraphai/graphs/base_graph.py", line 363, in execute
    state, exec_info = self._execute_standard(initial_state)
                       ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/redacted_path/venv/lib/python3.12/site-packages/scrapegraphai/graphs/base_graph.py", line 308, in _execute_standard
    raise e
  File "/redacted_path/venv/lib/python3.12/site-packages/scrapegraphai/graphs/base_graph.py", line 281, in _execute_standard
    result, node_exec_time, cb_data = self._execute_node(
                                      ^^^^^^^^^^^^^^^^^^^
  File "/redacted_path/venv/lib/python3.12/site-packages/scrapegraphai/graphs/base_graph.py", line 205, in _execute_node
    result = current_node.execute(state)
             ^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/redacted_path/venv/lib/python3.12/site-packages/scrapegraphai/nodes/generate_answer_node.py", line 193, in execute
    answer = self.invoke_with_timeout(
             ^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/redacted_path/venv/lib/python3.12/site-packages/scrapegraphai/nodes/generate_answer_node.py", line 79, in invoke_with_timeout
    response = chain.invoke(inputs)
               ^^^^^^^^^^^^^^^^^^^^
  File "/redacted_path/venv/lib/python3.12/site-packages/langchain_core/runnables/base.py", line 3046, in invoke
    input_ = context.run(step.invoke, input_, config)
             ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/redacted_path/venv/lib/python3.12/site-packages/langchain_core/language_models/chat_models.py", line 395, in invoke
    self.generate_prompt(
  File "/redacted_path/venv/lib/python3.12/site-packages/langchain_core/language_models/chat_models.py", line 980, in generate_prompt
    return self.generate(prompt_messages, stop=stop, callbacks=callbacks, **kwargs)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/redacted_path/venv/lib/python3.12/site-packages/langchain_core/language_models/chat_models.py", line 799, in generate
    self._generate_with_cache(
  File "/redacted_path/venv/lib/python3.12/site-packages/langchain_core/language_models/chat_models.py", line 1045, in _generate_with_cache
    result = self._generate(
             ^^^^^^^^^^^^^^^
  File "/redacted_path/venv/lib/python3.12/site-packages/langchain_anthropic/chat_models.py", line 1498, in _generate
    data = self._create(payload)
           ^^^^^^^^^^^^^^^^^^^^^
  File "/redacted_path/venv/lib/python3.12/site-packages/langchain_anthropic/chat_models.py", line 1357, in _create
    return self._client.messages.create(**payload)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/redacted_path/venv/lib/python3.12/site-packages/anthropic/_utils/_utils.py", line 283, in wrapper
    return func(*args, **kwargs)
           ^^^^^^^^^^^^^^^^^^^^^
TypeError: Messages.create() got an unexpected keyword argument 'model_tokens'

Expected behavior
I expect model_tokens to work, as per the error message I received

Desktop/Laptop environment

  • OS: macOS
  • Python 3.12.7 (in a venv)
  • scrapegraphai==1.61.0

Additional context
For context, I'm a seasoned Python programmer.

However, I'm completely new to Scrapegraph, and to writing AI agents. Maybe this can be easily explained by something simple that I've missed.

I tried searching the documentation (https://docs.scrapegraphai.com/introduction) for key words like model_tokens but could not find anything.

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't working

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions