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

Streaming example throws JSON SyntaxError #18

Closed
bracesproul opened this issue Dec 15, 2023 · 2 comments
Closed

Streaming example throws JSON SyntaxError #18

bracesproul opened this issue Dec 15, 2023 · 2 comments

Comments

@bracesproul
Copy link

node chat_with_streaming.js
Chat Stream:
It's subjective to determine theundefined:1
{"id": "cmpl-794d708af6ed43aeab6a2a81390c5d90", "object": "chat.completion.chunk", "created": 1702672109, "model": "mistral-tiny", "choices": [{"index": 0, "delta": {"role": null, "content": " \"best\" French cheese as people have different preferences based

SyntaxError: Unterminated string in JSON at position 258
    at JSON.parse (<anonymous>)
    at MistralClient.chatStream (file:///Users/bracesproul/code/mistral-client-js/src/client.js:184:24)
    at chatStream.next (<anonymous>)
    at file:///Users/bracesproul/code/mistral-client-js/examples/chat_with_streaming.js:13:18
    at process.processTicksAndRejections (node:internal/process/task_queues:95:5)

Node.js v20.10.0

If I modify the client streaming code to this:

if (chunkLine.startsWith('data:')) {
  const chunkData = chunkLine.substring(6).trim();
  if (chunkData !== '[DONE]') {
    let parsedChunkData;
    // try/catch added
    try {
      parsedChunkData = JSON.parse(chunkData);
    } catch (e) {
      console.error('\nError parsing chunk data.\n');
      continue;
    }
    yield parsedChunkData;
  }
}

Streaming works because continue allows for the error to not be thrown.
And Error parsing chunk data. is logged anywhere between 5-8 times (ran the example ~5 times)

@bracesproul bracesproul changed the title Streaming example throws SyntaxError: Unterminated string in JSON at position ... error. Streaming example throws JSON SyntaxError Dec 15, 2023
@ssmi153
Copy link

ssmi153 commented Dec 16, 2023

Yeah, I'm getting this issue too. The issue is the the SSE data comes in chunks that don't always form complete JSON objects. The library will need to buffer the incomplete chunks until it get's a complete line/json object. #12 is the same problem, with a suggested reference solution.

@wordware-ai
Copy link

Yeah, we've fixed this in our own package that is also compatible with Vercel Edge https://www.npmjs.com/package/mistral-edge

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

4 participants