cohere-ai / cohere-typescript

The Cohere TypeScript SDK
https://docs.cohere.ai
MIT License
127 stars 18 forks source link

More descriptive error handling in NODE SDK #30

Closed choicallum closed 1 year ago

choicallum commented 2 years ago

Currently, if a user inputs the wrong type into a function call, we respond with a generic "wrong type, check https://docs.cohere.ai/{endpoint}-reference" which is provided by the backend. Giving more verbose errors would be nice.

https://github.com/cohere-ai/cohere-python/pull/56 Here's how it's being handled in python. cc: @knajjars a followup on what was being discussed yesterday.

The new behaviour is, for non-list parameters:

co.generate(model = "small", prompt = 123)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/Users/cc-cohere/cohere/cohere-python/cohere/client.py", line 102, in generate
    assert_parameter(str, "prompt", prompt, "generate")
  File "/Users/cc-cohere/cohere/cohere-python/cohere/assert_parameters.py", line 12, in assert_parameter
    raise error
cohere.error.CohereError: the prompt parameter is expected to be a str, but received a int. See https://docs.cohere.ai/generate-reference/ for more details.

and for list parameters:

>>> co.generate(model = "small", prompt = "hello", stop_sequences=["hello", "...", 123])
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/Users/cc-cohere/cohere/cohere-python/cohere/client.py", line 110, in generate
    stop_sequences = assert_list_parameter(str, "stop_sequences", stop_sequences, "generate")
  File "/Users/cc-cohere/cohere/cohere-python/cohere/assert_parameters.py", line 26, in assert_list_parameter
    raise CohereError(
cohere.error.CohereError: the stop_sequences parameter is expected to be a list of str, but found a int at index 2. See https://docs.cohere.ai/generate-reference/ for more details.

We also allow users to input a string instead of a list of string wherever applicable (just create a list of strings with that string contained inside of it and feed that into the endpoint)