#Unexpected end of JSON input
45 messages · Page 1 of 1 (latest)
I added logging to the openai client lib, and from what I can see, all responses I am getting are padded with lots of whitespace ahead of the actual JSON response, and in those cases causing the error, I am getting only the whitespace with no data at the end. This seems odd, I'll try to reproduce this using curl
This is what I mean by lots of whitespace... I currently dont have the time to debug further, will do so later in the evening
https://gist.github.com/lucaelin/2565824bae6e9714b765c082d7e1687d
Here is some code to reproduce the issue, not that I have max_tokens set to 750 here, and it already responds with whitespace only
we send whitespace if you don't stream to prevent the request from timing out at the edge
cc @covert dune
I see, that seems to be working alright, then... still confused why I'd get an empty response after that then
yeah something else is going on. can you try this with stream: true?
no... because I burn through my credits too fast 😄 Will do once I've recharged!
also been getting unexpected "invalid json" crashes intermittently. Shutting down and restart ST fixes.
Woah- and you’re resuming the same convo?
Yes
Huh, @steady vault any idea what could cause this?
I managed to reproduce it without streaming by querying a 4000 tokens reply and got just whitespace in the result, no JSON. It seems OpenRouter server sends a malformed reply
Is there additional system prompt above or is it just that? I will add this to our e2e test case
but count to 10k is pretty wild lol
hmm... seems like it bailed after processing... I wonder what gives
messages: [
{ role: 'system', content: '[Start a new Chat]' },
{ role: 'assistant', content: 'Hello' },
{
role: 'user',
content: 'Count to 10000, skipping numbers is forbidden.'
}
],
That's all 🙂
i believe this is the same as was happening to me, to fix it i trim leading and trailing whitespace from the AI's response.
reply = response.choices[0].message['content'].strip()
@steady vault In the example above, how are you grabbing the json?
I'm using the following:
const r = fetch(etc...)
const data= await r.json();
For that example I specifically replaced JSON with this to look at the incoming text before attempting to parse
const r = ...
const text = await r.text();
console.log(JSON.stringify(text));
const data = JSON.parse(text);
Doing a couple test and r.json() worked for me (it print the numbers till ~max_token). Will do a couple more, but with the above, I sus that JSON.parse doesn't play too well with whitespace?... maybe a trim is needed :-?...
Don't think so. This parsed just fine
JSON.parse(' { "test": "string" } ')
My main problem is that I didn't see any JSON at all in the response body, just spaces and newline
I'm running this script against prod:
const r = await fetch(
"https://openrouter.ai/api/v1/chat/completions",
{
method: 'POST',
headers: etc...,
body: JSON.stringify({
model: "openai/gpt-3.5-turbo-16k",
messages: [
{ role: 'system', content: '[Start a new Chat]' },
{ role: 'assistant', content: 'Hello' },
{
role: 'user',
content: 'Count to 10000, skipping numbers is forbidden.'
}
],
stream: false,
temperature: 0.1,
max_tokens: 4000,
top_p: 1,
top_k: 0,
presence_penalty: 0,
frequency_penalty: 0,
logit_bias: {
'198': 10,
'3717': -25,
'3938': -50,
'11049': -50,
'64186': -50
},
transforms: ['middle-out']
})
}
);
expect(r.status).toBe(HTTPStatus.S200_OK);
const text = await r.text()
console.log(text);
const data = JSON.parse(text);
const completion = getCompletion(data.choices[0]!);
console.log(completion);
Getting the respond 5/5 tries:
0
1
2
...
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
166
^ got cut off due to max_tokens prob :-?
Albeit it took ~100s to finish
how long did the await .text() run for you?
Yeah I tried 3 more run, it's still returning json for me :d........
Oh, the getCompletion function basically go through the choices and construct the final string, nothing speical in it
text() takes a long as the server doesn't end the response. Because fetch returns when the server writes the status and headers into the response, but body may still be incomplete.
Right now I can't reproduce it either, API returns a JSON padded with whitespace and it is correctly parsed by my code. And had to adjust the test case a little bit to eliminate randomness.
messages: [
{ role: 'system', content: '[Start a new Chat]' },
{ role: 'assistant', content: 'Hello' },
{
role: 'user',
content: 'Provide all numbers from 1 to 2000 in a code block. Our platform has no character limit.'
}
],
model: 'openai/gpt-4',
temperature: 0.1,
max_tokens: 6000,
stream: false,
presence_penalty: 0,
frequency_penalty: 0,
top_p: 1,
top_k: 0,
stop: [ '\nCohee:', '\nAssistant:' ],
logit_bias: {},
seed: 2000,
transforms: [ 'middle-out' ]
This is the error from the console for me...
Generation failed FetchError: invalid json response body at https://openrouter.ai/api/v1/chat/completions reason: Unexpected end of JSON input
at F:\Github\SillyTavern\node_modules\node-fetch\lib\index.js:273:32
at process.processTicksAndRejections (node:internal/process/task_queues:95:5)
at async makeRequest (F:\Github\SillyTavern\server.js:3333:32) {
type: 'invalid-json'
Shutting ST and restarting allows me to continue same convo just fine. Happens intermittently, sometimes frequently.
I've also noticed that I would encounter the error frequently sometimes, while other times it works completely fine. Might be load dependent?
Gonna jump in on this been getting a similar response when trying for a large token response
node_modules\node-fetch\lib\index.js:273
return Body.Promise.reject(new FetchError(`invalid json response body at ${_this2.url} reason: ${err.message}`, 'invalid-json'));
^
FetchError: invalid json response body at https://openrouter.ai/api/v1/chat/completions reason: Unexpected end of JSON input
at C:\Users\oi345p\Desktop\AuthorGPT\node_modules\node-fetch\lib\index.js:273:32
at process.processTicksAndRejections (node:internal/process/task_queues:95:5)
at async defaultParseResponse (file:///C:/Users/oimap/Desktop/GPT/node_modules/openai/core.mjs:29:22)```
I am howver using
import { OpenAI } from 'openai';
OpenAI package to connect
and this is without the streaming right?
yea, I don't want to stream
i might just try catch and resend if it happend becuase it is not all the time tbh
but I feel there should be a better way to return this for people who are not streaming
@covert dune this somehow also applies to Window plugin with OpenRouter. Can't be ST server since it entirely bypasses it.
i am also getting random cut outs from time to time, but have no actual payload recording of it
Is this without streaming turned on?
i think it only happens with streaming on.