[GH-ISSUE #1718] incomplete json in api responses #973

Closed
opened 2026-04-12 10:40:08 -05:00 by GiteaMirror · 2 comments
Owner

Originally created by @ralyodio on GitHub (Dec 26, 2023).
Original GitHub issue: https://github.com/ollama/ollama/issues/1718

I tried both /api/chat and /api/generate endpoints which seem to produce the same results. however I'm getting invalid json on every response.

Originally created by @ralyodio on GitHub (Dec 26, 2023). Original GitHub issue: https://github.com/ollama/ollama/issues/1718 I tried both /api/chat and /api/generate endpoints which seem to produce the same results. however I'm getting invalid json on every response.
Author
Owner

@ralyodio commented on GitHub (Dec 26, 2023):

		const response = await fetch('https://ai.profullstack.com/api/generate', {
			method: 'POST',
			body: JSON.stringify({
				model: 'llama2-uncensored:latest',
				prompt
			})
		});

		const reader = response.body?.getReader();
		if (!reader) {
			throw new Error('Failed to read response body');
		}

		let content = '';

		while (true) {
			const { done, response: value } = await reader.read();
			if (done) {
				break;
			}

			if (!value) {
				continue;
			}
			const rawjson = new TextDecoder().decode(value);
			console.log('value: ', rawjson);

			let json;
			try {
			} catch (err) {}
				json = JSON.parse(rawjson);

			console.log('value: ', json);
			if (json.done === false) {
				process.stdout.write(json.message.content);
				content += json.message.content;
			}
		}

		console.log('content: ', content);
		const res = JSON.parse(content);
		return res;

<!-- gh-comment-id:1869467185 --> @ralyodio commented on GitHub (Dec 26, 2023): ``` const response = await fetch('https://ai.profullstack.com/api/generate', { method: 'POST', body: JSON.stringify({ model: 'llama2-uncensored:latest', prompt }) }); const reader = response.body?.getReader(); if (!reader) { throw new Error('Failed to read response body'); } let content = ''; while (true) { const { done, response: value } = await reader.read(); if (done) { break; } if (!value) { continue; } const rawjson = new TextDecoder().decode(value); console.log('value: ', rawjson); let json; try { } catch (err) {} json = JSON.parse(rawjson); console.log('value: ', json); if (json.done === false) { process.stdout.write(json.message.content); content += json.message.content; } } console.log('content: ', content); const res = JSON.parse(content); return res; ```
Author
Owner

@ralyodio commented on GitHub (Dec 26, 2023):

if I add format: 'json', stream: false to the body it works as expected.

<!-- gh-comment-id:1869676310 --> @ralyodio commented on GitHub (Dec 26, 2023): if I add `format: 'json', stream: false` to the body it works as expected.
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: github-starred/ollama#973