# Collection.generate.near\_text using claude-3-5-sonnet-20241022 raises Anthropic API error: invalid\_request\_error - max\_tokens: must be greater than or equal to 1

**URL:** <https://forum.weaviate.io/t/collection-generate-near-text-using-claude-3-5-sonnet-20241022-raises-anthropic-api-error-invalid-request-error-max-tokens-must-be-greater-than-or-equal-to-1/10499>\
**Category:** Support\
**Tags:** bug, wcs, python\
**Created:** [February 21, 2025, 10:06pm UTC](https://forum.weaviate.io/t/collection-generate-near-text-using-claude-3-5-sonnet-20241022-raises-anthropic-api-error-invalid-request-error-max-tokens-must-be-greater-than-or-equal-to-1/10499 "2025-02-21T22:06:41Z")\
**Posts on this page:** 13\
**Page:** 1

<div class="post-metadata">

**Author:** ![dourillon](https://yyz1.discourse-cdn.com/flex027/user_avatar/forum.weaviate.io/dourillon/32/3643_2.png) [@dourillon](https://forum.weaviate.io/u/dourillon)\
**Post date:** [February 21, 2025, 10:06pm UTC](https://forum.weaviate.io/t/collection-generate-near-text-using-claude-3-5-sonnet-20241022-raises-anthropic-api-error-invalid-request-error-max-tokens-must-be-greater-than-or-equal-to-1/10499/1 "2025-02-21T22:06:41Z")

</div>

### Description

Using this requirements.txt file and `pip install -r requirements.txt`:

```auto
anthropic
python-dotenv
weaviate-client

```

With the following `.env` file:

```auto
# weaviate
WCD_URL=<your_wcd_url>
WCD_API_KEY=<your_wcd_key>

# openai
OPENAI_API_KEY=<your_openai_key>

# anthropic
ANTHROPIC_API_KEY=<your_anthropic_key>

```

Run the following python script:

```auto
import os

from dotenv import load_dotenv
from weaviate import WeaviateClient, connect_to_weaviate_cloud
from weaviate.classes.config import Configure
from weaviate.classes.init import Auth

load_dotenv()

def get_client() -> WeaviateClient:
    """Get a weaviate cloud client instance, using WCD_URL, WCD_API_KEY, and OPENAI_API_KEY
    environment variables.

    **Important:** Note that it is the responsibility of the caller to close the client.

    Returns:
        WeaviateClient: A configured weaviate cloud client
    """
    return connect_to_weaviate_cloud(
        cluster_url=os.environ["WCD_URL"],
        auth_credentials=Auth.api_key(os.environ["WCD_API_KEY"]),
        headers={
            "X-Anthropic-Api-Key": os.environ["ANTHROPIC_API_KEY"],
            "X-OpenAI-Api-Key": os.environ["OPENAI_API_KEY"],
        },
    )

if __name__ == " __main__":

    with get_client() as client:

        print("Creating collection...")
        collection_name = "test_collection"
        collection = client.collections.create(
            name=collection_name,
            vectorizer_config=Configure.Vectorizer.text2vec_openai(
            ),
            generative_config=Configure.Generative.anthropic(
                # model="claude-3-5-sonnet-20240620",
                model="claude-3-5-sonnet-20241022",
                max_tokens=1024,
            ),
        )

        print("Adding chunk to collection...")
        with collection.batch.dynamic() as batch:
            batch.add_object({"text": "Most dogs have 4 legs... except those that were amputated."})
        print(f"Nb of chunks in collection: {collection.aggregate.over_all().total_count}")

        print("Call generate...")
        query = "How many legs does a dog have? Answer as a pirate would."
        print(f"\tQuery: {query}")
        response = collection.generate.near_text(
            query="dog",
            limit=1,
            grouped_task=query,
        )
        print(f"\tRetrieved chunk: {response.objects[0].properties['text']}")
        print(f"\tAnswer: {response.generated}")

        print("Delete collection...")
        client.collections.delete(collection_name)

```

You should get the following error:

```auto
Creating collection...
Adding chunk to collection...
Nb of chunks in collection: 1
Call generate...
	Query: How many legs does a dog have? Answer as a pirate would.
Traceback (most recent call last):
  File "/Users/dtourillon/TouriLogica/Kwerty/dev/weaviate-generate-issue/weaviate-issue-venv/lib/python3.12/site-packages/weaviate/collections/grpc/query.py", line 478, in __call
    res = await _Retry(4).with_exponential_backoff(
          ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/Users/dtourillon/TouriLogica/Kwerty/dev/weaviate-generate-issue/weaviate-issue-venv/lib/python3.12/site-packages/weaviate/collections/grpc/retry.py", line 31, in with_exponential_backoff
    raise e
  File "/Users/dtourillon/TouriLogica/Kwerty/dev/weaviate-generate-issue/weaviate-issue-venv/lib/python3.12/site-packages/weaviate/collections/grpc/retry.py", line 28, in with_exponential_backoff
    return await f(*args, **kwargs)
           ^^^^^^^^^^^^^^^^^^^^^^^^
  File "/Users/dtourillon/TouriLogica/Kwerty/dev/weaviate-generate-issue/weaviate-issue-venv/lib/python3.12/site-packages/grpc/aio/_call.py", line 327, in __await__
    raise _create_rpc_error(
grpc.aio._call.AioRpcError: <AioRpcError of RPC that terminated with:
	status = StatusCode.UNKNOWN
	details = "Anthropic API error: invalid_request_error - max_tokens: must be greater than or equal to 1"
	debug_error_string = "UNKNOWN:Error received from peer {created_time:"2025-02-21T16:55:50.85583-05:00", grpc_status:2, grpc_message:"Anthropic API error: invalid_request_error - max_tokens: must be greater than or equal to 1"}"
>

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "/Users/dtourillon/TouriLogica/Kwerty/dev/weaviate-generate-issue/weaviate_generate_issue_main.py", line 56, in <module>
    response = collection.generate.near_text(
               ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/Users/dtourillon/TouriLogica/Kwerty/dev/weaviate-generate-issue/weaviate-issue-venv/lib/python3.12/site-packages/weaviate/syncify.py", line 23, in sync_method
    return _EventLoopSingleton.get_instance().run_until_complete(
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/Users/dtourillon/TouriLogica/Kwerty/dev/weaviate-generate-issue/weaviate-issue-venv/lib/python3.12/site-packages/weaviate/event_loop.py", line 42, in run_until_complete
    return fut.result()
           ^^^^^^^^^^^^
  File "/opt/homebrew/Cellar/python@3.12/3.12.3/Frameworks/Python.framework/Versions/3.12/lib/python3.12/concurrent/futures/_base.py", line 456, in result
    return self.__get_result()
           ^^^^^^^^^^^^^^^^^^^
  File "/opt/homebrew/Cellar/python@3.12/3.12.3/Frameworks/Python.framework/Versions/3.12/lib/python3.12/concurrent/futures/_base.py", line 401, in __get_result
    raise self._exception
  File "/Users/dtourillon/TouriLogica/Kwerty/dev/weaviate-generate-issue/weaviate-issue-venv/lib/python3.12/site-packages/weaviate/collections/queries/near_text/generate.py", line 101, in near_text
    res = await self._query.near_text(
          ^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/Users/dtourillon/TouriLogica/Kwerty/dev/weaviate-generate-issue/weaviate-issue-venv/lib/python3.12/site-packages/weaviate/collections/grpc/query.py", line 490, in __call
    raise WeaviateQueryError(str(e), "GRPC search") # pyright: ignore
    ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
weaviate.exceptions.WeaviateQueryError: Query call with protocol GRPC search failed with message <AioRpcError of RPC that terminated with:
	status = StatusCode.UNKNOWN
	details = "Anthropic API error: invalid_request_error - max_tokens: must be greater than or equal to 1"
	debug_error_string = "UNKNOWN:Error received from peer {created_time:"2025-02-21T16:55:50.85583-05:00", grpc_status:2, grpc_message:"Anthropic API error: invalid_request_error - max_tokens: must be greater than or equal to 1"}"
>.

```

### Any additional Information

The script above:

- … works using `claude-3-5-sonnet-20240620`, the second last available Claude 3.5 sonnet model
- … raises “Anthropic API error: invalid\_request\_error - max\_tokens: must be greater than or equal to 1” using `claude-3-5-sonnet-20241022`, the latest Claude 3.5 Sonnet model… despite max\_tokens being set to 1024…

---

<div class="post-metadata">

**Author:** ![DudaNogueira](https://yyz1.discourse-cdn.com/flex027/user_avatar/forum.weaviate.io/dudanogueira/32/7846_2.png) [@DudaNogueira](https://forum.weaviate.io/u/DudaNogueira)\
**Post date:** [February 24, 2025, 1:41pm UTC](https://forum.weaviate.io/t/collection-generate-near-text-using-claude-3-5-sonnet-20241022-raises-anthropic-api-error-invalid-request-error-max-tokens-must-be-greater-than-or-equal-to-1/10499/2 "2025-02-24T13:41:31Z")

</div>

hi @dourillon !!

Welcome to our community!

I was not able to reproduce this.

I tried with both models.

Here is a running version on python notebook:

```python
import os

from dotenv import load_dotenv
from weaviate import WeaviateClient, connect_to_weaviate_cloud, connect_to_local
from weaviate.classes.config import Configure
from weaviate.classes.init import Auth

load_dotenv()

def get_client() -> WeaviateClient:
    """Get a weaviate cloud client instance, using WCD_URL, WCD_API_KEY, and OPENAI_API_KEY
    environment variables.

    **Important:** Note that it is the responsibility of the caller to close the client.

    Returns:
        WeaviateClient: A configured weaviate cloud client
    """
    return connect_to_local(
        headers={
            "X-Anthropic-Api-Key": os.environ["ANTHROPIC_API_KEY"],
            "X-OpenAI-Api-Key": os.environ["OPENAI_API_KEY"],
        },
    )

with get_client() as client:

    collection_name = "test_collection"
    print("Delete collection...")
    client.collections.delete(collection_name)    
    print("Creating collection...")
    collection = client.collections.create(
        name=collection_name,
        vectorizer_config=Configure.Vectorizer.text2vec_openai(
        ),
        generative_config=Configure.Generative.anthropic(
            #model="claude-3-5-sonnet-20240620",
            model="claude-3-5-sonnet-20241022",
            max_tokens=1024,
        ),
    )

    print("Adding chunk to collection...")
    with collection.batch.dynamic() as batch:
        batch.add_object({"text": "Most dogs have 4 legs... except those that were amputated."})
    print(f"Nb of chunks in collection: {collection.aggregate.over_all().total_count}")

    print("Call generate...")
    query = "How many legs does a dog have? Answer as a pirate would."
    print(f"\tQuery: {query}")
    response = collection.generate.near_text(
        query="dog",
        limit=1,
        grouped_task=query,
    )
    print(f"\tRetrieved chunk: {response.objects[0].properties['text']}")
    print(f"\tAnswer: {response.generated}")

```

---

<div class="post-metadata">

**Author:** ![dourillon](https://yyz1.discourse-cdn.com/flex027/user_avatar/forum.weaviate.io/dourillon/32/3643_2.png) [@dourillon](https://forum.weaviate.io/u/dourillon)\
**Post date:** [February 24, 2025, 2:56pm UTC](https://forum.weaviate.io/t/collection-generate-near-text-using-claude-3-5-sonnet-20241022-raises-anthropic-api-error-invalid-request-error-max-tokens-must-be-greater-than-or-equal-to-1/10499/3 "2025-02-24T14:56:54Z")

</div>

Hi @DudaNogueira

Thanks for trying to reproduce, and sorry you were not able to.

I am new to weaviate, so the problem may very well be on my side. Apologies if that’s the case…

I tried running your notebook on my side, and just as you have, I was able to run it with both versions on Claude 3.5 models… At least, we are seeing the same! 😉

However, I can still consistently reproduce the initial reported issue on my side, so where do we go from here?

- I notice that I am using `connect_to_weaviate_cloud` while your notebook uses `connect_to_local`… could “weaviate cloud” vs. “weaviate local” make a difference? Is this something you could try on your side?
- I am creating a python venv, then activating it, then pip install -r requirements.txt, then running my script with python at command line… Any additional info you’d need that could explain the issue I’m seeing? Would it help if I report the packages I’m using with `pip freeze` ?

Cheers,

Dominique

---

<div class="post-metadata">

**Author:** ![DudaNogueira](https://yyz1.discourse-cdn.com/flex027/user_avatar/forum.weaviate.io/dudanogueira/32/7846_2.png) [@DudaNogueira](https://forum.weaviate.io/u/DudaNogueira)\
**Post date:** [February 24, 2025, 3:27pm UTC](https://forum.weaviate.io/t/collection-generate-near-text-using-claude-3-5-sonnet-20241022-raises-anthropic-api-error-invalid-request-error-max-tokens-must-be-greater-than-or-equal-to-1/10499/4 "2025-02-24T15:27:18Z")

</div>

Hi!

the connect\_to\_local will connect to a Weaviate instance running locally in docker.

This may be an issue Anthropic side:

```auto
Anthropic API error: invalid_request_error - max_tokens: must be greater than or equal to 1

```

---

<div class="post-metadata">

**Author:** ![dourillon](https://yyz1.discourse-cdn.com/flex027/user_avatar/forum.weaviate.io/dourillon/32/3643_2.png) [@dourillon](https://forum.weaviate.io/u/dourillon)\
**Post date:** [February 24, 2025, 3:36pm UTC](https://forum.weaviate.io/t/collection-generate-near-text-using-claude-3-5-sonnet-20241022-raises-anthropic-api-error-invalid-request-error-max-tokens-must-be-greater-than-or-equal-to-1/10499/5 "2025-02-24T15:36:15Z")

</div>

> the connect\_to\_local will connect to a Weaviate instance running locally in docker.

Agreed, but why do I get a different behavior using local vs could weaviate? And cloud weaviate is what I’ll use in prod.

> This may be an issue Anthropic side:  
> Anthropic API error: invalid\_request\_error - max\_tokens: must be greater than or equal to 1

Agreed, it could be on Anthropic side… but it could also be on weaviate side since I set `max_tokens=1024` when setting the generative config _in weaviate_ when creating a weaviate collection, no?

---

<div class="post-metadata">

**Author:** ![DudaNogueira](https://yyz1.discourse-cdn.com/flex027/user_avatar/forum.weaviate.io/dudanogueira/32/7846_2.png) [@DudaNogueira](https://forum.weaviate.io/u/DudaNogueira)\
**Post date:** [February 27, 2025, 6:26pm UTC](https://forum.weaviate.io/t/collection-generate-near-text-using-claude-3-5-sonnet-20241022-raises-anthropic-api-error-invalid-request-error-max-tokens-must-be-greater-than-or-equal-to-1/10499/6 "2025-02-27T18:26:58Z")

</div>

hi @dourillon !

I got the same result both running locally and running on ou cloud.

I this outcome consistent?

---

<div class="post-metadata">

**Author:** ![dourillon](https://yyz1.discourse-cdn.com/flex027/user_avatar/forum.weaviate.io/dourillon/32/3643_2.png) [@dourillon](https://forum.weaviate.io/u/dourillon)\
**Post date:** [February 28, 2025, 2:19pm UTC](https://forum.weaviate.io/t/collection-generate-near-text-using-claude-3-5-sonnet-20241022-raises-anthropic-api-error-invalid-request-error-max-tokens-must-be-greater-than-or-equal-to-1/10499/7 "2025-02-28T14:19:27Z")

</div>

Hi @DudaNogueira

Sorry you cannot seem to be able to reproduce.

Yes, in my case, the results is consistent for the past week.

In case it helps, here’s the result of `pip freeze` in my venv:

```auto
annotated-types==0.7.0
anthropic==0.46.0
anyio==4.8.0
appnope==0.1.4
asttokens==3.0.0
Authlib==1.3.1
certifi==2025.1.31
cffi==1.17.1
comm==0.2.2
cryptography==44.0.1
debugpy==1.8.12
decorator==5.2.1
distro==1.9.0
docx2txt==0.8
executing==2.2.0
grpcio==1.70.0
grpcio-health-checking==1.70.0
grpcio-tools==1.70.0
h11==0.14.0
httpcore==1.0.7
httpx==0.28.1
idna==3.10
ipykernel==6.29.5
ipython==8.32.0
jedi==0.19.2
jiter==0.8.2
jupyter_client==8.6.3
jupyter_core==5.7.2
matplotlib-inline==0.1.7
nest-asyncio==1.6.0
packaging==24.2
parso==0.8.4
pexpect==4.9.0
platformdirs==4.3.6
prompt_toolkit==3.0.50
protobuf==5.29.3
psutil==7.0.0
ptyprocess==0.7.0
pure_eval==0.2.3
pycparser==2.22
pydantic==2.10.6
pydantic_core==2.27.2
Pygments==2.19.1
pypandoc==1.15
python-dateutil==2.9.0.post0
python-dotenv==1.0.1
pyzmq==26.2.1
setuptools==75.8.0
six==1.17.0
sniffio==1.3.1
stack-data==0.6.3
tornado==6.4.2
traitlets==5.14.3
typing_extensions==4.12.2
validators==0.34.0
wcwidth==0.2.13
weaviate-client==4.11.0

```

Apart from maybe using different lib versions, not sure I can think of other reasons for you not seeing the issue consistently and me seeing it consistently?!

---

<div class="post-metadata">

**Author:** ![DudaNogueira](https://yyz1.discourse-cdn.com/flex027/user_avatar/forum.weaviate.io/dudanogueira/32/7846_2.png) [@DudaNogueira](https://forum.weaviate.io/u/DudaNogueira)\
**Post date:** [February 28, 2025, 2:52pm UTC](https://forum.weaviate.io/t/collection-generate-near-text-using-claude-3-5-sonnet-20241022-raises-anthropic-api-error-invalid-request-error-max-tokens-must-be-greater-than-or-equal-to-1/10499/8 "2025-02-28T14:52:05Z")

</div>

As you are running in our cloud, can you open a support ticket as described in [Weaviate Cloud](https://console.weaviate.cloud/support)?

Then I can access that cluster and try something with my key to see if I can replicate it.

Thanks!

---

<div class="post-metadata">

**Author:** ![dourillon](https://yyz1.discourse-cdn.com/flex027/user_avatar/forum.weaviate.io/dourillon/32/3643_2.png) [@dourillon](https://forum.weaviate.io/u/dourillon)\
**Post date:** [February 28, 2025, 3:21pm UTC](https://forum.weaviate.io/t/collection-generate-near-text-using-claude-3-5-sonnet-20241022-raises-anthropic-api-error-invalid-request-error-max-tokens-must-be-greater-than-or-equal-to-1/10499/9 "2025-02-28T15:21:17Z")

</div>

Hi @DudaNogueira

Sure, I see this

```auto
Support
For support of your cluster hosted on Weaviate Cloud, please send an e-mail to support@weaviate.io

```

So I will send an email to [support@weaviate.io](mailto:support@weaviate.io) pointing to this conversation.

Thanks for your support!

---

<div class="post-metadata">

**Author:** ![dourillon](https://yyz1.discourse-cdn.com/flex027/user_avatar/forum.weaviate.io/dourillon/32/3643_2.png) [@dourillon](https://forum.weaviate.io/u/dourillon)\
**Post date:** [February 28, 2025, 3:26pm UTC](https://forum.weaviate.io/t/collection-generate-near-text-using-claude-3-5-sonnet-20241022-raises-anthropic-api-error-invalid-request-error-max-tokens-must-be-greater-than-or-equal-to-1/10499/10 "2025-02-28T15:26:40Z")

</div>

Done, email just sent to [support@weaviate.io](mailto:support@weaviate.io).

---

<div class="post-metadata">

**Author:** ![dourillon](https://yyz1.discourse-cdn.com/flex027/user_avatar/forum.weaviate.io/dourillon/32/3643_2.png) [@dourillon](https://forum.weaviate.io/u/dourillon)\
**Post date:** [February 28, 2025, 3:34pm UTC](https://forum.weaviate.io/t/collection-generate-near-text-using-claude-3-5-sonnet-20241022-raises-anthropic-api-error-invalid-request-error-max-tokens-must-be-greater-than-or-equal-to-1/10499/11 "2025-02-28T15:34:26Z")

</div>

Confused as to where to ask my questions initially, I had also asked it there: [collection.generate.near\_text using claude-3-5-sonnet-20241022 raises Anthropic API error: invalid\_request\_error - max\_tokens: must be greater than or equal to 1 · Issue #7331 · weaviate/weaviate · GitHub](https://github.com/weaviate/weaviate/issues/7331)

Someone was able to reproduce and commented:

```auto
Thanks for the information! @dtourillon

I am able to replicate this issue on v1.28.4. However, it does not exist on v1.28.5 so must have been fixed in a recent PR. Are you able to update your Weaviate cluster to v1.28.5 or above?

```

So I will give it a try…

---

<div class="post-metadata">

**Author:** ![dourillon](https://yyz1.discourse-cdn.com/flex027/user_avatar/forum.weaviate.io/dourillon/32/3643_2.png) [@dourillon](https://forum.weaviate.io/u/dourillon)\
**Post date:** [February 28, 2025, 3:53pm UTC](https://forum.weaviate.io/t/collection-generate-near-text-using-claude-3-5-sonnet-20241022-raises-anthropic-api-error-invalid-request-error-max-tokens-must-be-greater-than-or-equal-to-1/10499/12 "2025-02-28T15:53:19Z")

</div>

To close the loop: upgrading my weaviate cluster to v1.28.6 appears to have fixed the issue I was seeing!

---

<div class="post-metadata">

**Author:** ![DudaNogueira](https://yyz1.discourse-cdn.com/flex027/user_avatar/forum.weaviate.io/dudanogueira/32/7846_2.png) [@DudaNogueira](https://forum.weaviate.io/u/DudaNogueira)\
**Post date:** [February 28, 2025, 5:54pm UTC](https://forum.weaviate.io/t/collection-generate-near-text-using-claude-3-5-sonnet-20241022-raises-anthropic-api-error-invalid-request-error-max-tokens-must-be-greater-than-or-equal-to-1/10499/13 "2025-02-28T17:54:43Z")

</div>

Glad to hear that, and thanks for sharing @dourillon !!
