Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

"Error: [object Object]" during message streaming when error is via an SSE (cause/detail not accessible) #346

Open
paulcalcraft opened this issue Mar 20, 2024 · 7 comments
Assignees

Comments

@paulcalcraft
Copy link

paulcalcraft commented Mar 20, 2024

When hitting an error during the async iterator of a anthropic.messages.create(), the exception raised and associated error object doesn't have any detail, it just has an e.cause.message set to "[object Object]".

My example SSE that's occurring during streaming is:

{event: 'error', data: '{"type":"error","error":{"type":"overloaded_error","message":"Overloaded"}              }', raw: Array(2)}

The error SSE is then thrown using APIError.generate here:

throw APIError.generate(undefined, errJSON, errMessage, createResponseHeaders(response.headers));

The errJSON is correctly being passed to generate, but because status isn't set (it's an SSE, not an HTTP response), we use castToError() to raise the APIConnectionError, with no other info:

return new APIConnectionError({ cause: castToError(errorResponse) });

castToError just returns new Error(errJSON)

return new Error(err);

But errJSON doesn't have a good toString, so our cause Error object has message "[object Object]" and no other properties. This means you can't handle/inspect the error cause correctly when catching the error during the async iterator.

An example error:

anthropic stream error Error: Connection error.
    at APIError.generate (\node_modules\@anthropic-ai\sdk\error.mjs:32:20)
    at Stream.iterator (\node_modules\@anthropic-ai\sdk\streaming.mjs:73:40)
    at process.processTicksAndRejections (\lib\internal\process\task_queues.js:95:5)
    at async [Symbol.asyncIterator] (\src\routes\api\oracle\+server.js:205:38)
...
    at async initiateResponse (/src/routes/api/oracle/+server.js:1410:17) {status: undefined, headers: undefined, error: undefined, cause: Error: [object Object]
    at castToError (fil…/node_modules/@anthr…, stack: 'Error: Connection error.
    at APIError.gene…/src/routes/api/oracle/+server.js:1410:17)', …}

And where I'm catching it:

try {
    for await (const messageStreamEvent of response) {
        // ...
    }
} catch (e) {
    console.error("anthropic stream error", e) // e.cause.message == "[object Object]"
}

Would it be possible to correctly format the error so that it's possible to identify at least the error type by inspecting .cause on the APIConnectionError?

Thanks for any help. Also happy to submit a PR if there's agreement on the best way to surface the error detail in the APIConnectionError object.

@paulcalcraft paulcalcraft changed the title Streaming error reason/detail not accessible when occuring during streaming as an SSE Streaming "Error: [object Object]" during streaming when error is via an SSE (cause/detail not accessible) Mar 20, 2024
@paulcalcraft paulcalcraft changed the title Streaming "Error: [object Object]" during streaming when error is via an SSE (cause/detail not accessible) "Error: [object Object]" during message streaming when error is via an SSE (cause/detail not accessible) Mar 20, 2024
@rattrayalex
Copy link
Collaborator

Thanks for the report, we'll take a look!

@ryanblock
Copy link

Just a heads up, we have also been able to replicate this issue. This is running within a Lambda; the error occurs after a few hundred tokens. An example prompt which seems to replicate this for us is: Please send the first 10 paragraphs of Alice's Adventures in Wonderland by Lewis Carroll (which is in the public domain).

APIConnectionError: Connection error.
    at Function.generate (file:///var/task/node_modules/@anthropic-ai/sdk/error.mjs:32:20)
    at Stream.iterator (file:///var/task/node_modules/@anthropic-ai/sdk/streaming.mjs:52:40)
    ... 2 lines matching cause stack trace ...
    at async MessageStream._createMessage (file:///var/task/node_modules/@anthropic-ai/sdk/lib/MessageStream.mjs:113:26) {
  status: undefined,
  headers: undefined,
  error: undefined,
  cause: Error: [object Object]
      at castToError (file:///var/task/node_modules/@anthropic-ai/sdk/core.mjs:682:12)
      at Function.generate (file:///var/task/node_modules/@anthropic-ai/sdk/error.mjs:32:52)
      at Stream.iterator (file:///var/task/node_modules/@anthropic-ai/sdk/streaming.mjs:52:40)
      at runMicrotasks (<anonymous>)
      at processTicksAndRejections (node:internal/process/task_queues:96:5)
      at async MessageStream._createMessage (file:///var/task/node_modules/@anthropic-ai/sdk/lib/MessageStream.mjs:113:26)
}

@beeirl
Copy link

beeirl commented Apr 30, 2024

Running into the exact same issue here when running it on Vercel using Vercel AI SDK.

@rattrayalex
Copy link
Collaborator

FWIW, my guess is that this is due to Vercel timing out your handler, but I agree the error message being hard to read makes this worse. @RobertCraigie care to ticket?

@paulcalcraft
Copy link
Author

Thanks! I'm on a Pro plan with Vercel for 5 minute timeouts so I don't think that's actually the case for me

@ryanblock
Copy link

@rattrayalex fwiw, and I mentioned above, we have seen this error in plain old AWS Lambda, and have observed this not to be related to Lambda timeouts. (Just for my own edification, what's the relationship here with @stainless-api?)

@rattrayalex
Copy link
Collaborator

Gotcha, that's helpful. We'll try to look into this, but a repro script would be very helpful. Can anyone share one?

what's the relationship here with https://github.com/stainless-api?

I work at Stainless, which Anthropic uses to build their SDKs.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

5 participants