Skip to content

fix(ollama): handle incomplete JSON chunks in stream #995

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

Open
wants to merge 1 commit into
base: main
Choose a base branch
from

Conversation

berkcaputcu
Copy link

@berkcaputcu berkcaputcu commented May 17, 2025

Addresses one of the issues raised in #686

Problem

Ollama LLM returns the final chunk in parts when the chunk is too long. I only noticed this behaviour on the final chunk, I'm not sure if it happens on other chunks as well.

Solution

Improve the json_responses_chunk_handler to gracefully handle cases where a JSON chunk is split across buffer boundaries. If a chunk does not end with '}', it is considered incomplete and buffered until the next chunk arrives. This prevents JSON parsing errors and ensures all responses are processed correctly.

I took part of the solution from this diff: https://github.com/patterns-ai-core/langchainrb/pull/644/files#diff-746ba2cd57580e32b0f013cbe3c8eaf8f1621e112c89f3af07983321dd6846dbL143-L148

Improve the `json_responses_chunk_handler` to gracefully handle cases
where a JSON chunk is split across buffer boundaries. If a chunk does
not end with '}', it is considered incomplete and buffered until the
next chunk arrives. This prevents JSON parsing errors and ensures all
responses are processed correctly.
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

Successfully merging this pull request may close these issues.

1 participant