[GH-ISSUE #7703] Clarify JSONL as the Returned Format for Streaming JSON Objects #4920

Open
opened 2026-04-12 15:58:38 -05:00 by GiteaMirror · 3 comments
Owner

Originally created by @gwpl on GitHub (Nov 16, 2024).
Original GitHub issue: https://github.com/ollama/ollama/issues/7703

Current Documentation:
API documentation states:

A stream of JSON objects is returned.

Proposal:
Specify the format explicitly as:

A stream of JSON objects in [JSON Lines (JSONL)] format is returned.

Reasoning:
By constraining the format to JSONL (i.e., each JSON object is serialized on a separate line as per jsonlines.org), parsing implementations become simpler. Instead of relying on complex JSON parsing to determine object boundaries—especially when JSON content may include brackets—developers can leverage line-buffered iterators to process incoming chunks more efficiently.

This clarification aligns the documentation with common parsing practices for streaming JSON data and reduces potential ambiguity.

Originally created by @gwpl on GitHub (Nov 16, 2024). Original GitHub issue: https://github.com/ollama/ollama/issues/7703 **Current Documentation**: [API documentation](https://github.com/ollama/ollama/blob/4759d879f2376ffb9b82f296e442ec8ef137f27b/docs/api.md?plain=1#L79) states: > A stream of JSON objects is returned. **Proposal**: Specify the format explicitly as: > A stream of JSON objects in [JSON Lines (JSONL)] format is returned. **Reasoning**: By constraining the format to JSONL (i.e., each JSON object is serialized on a separate line as per [jsonlines.org](https://jsonlines.org/)), parsing implementations become simpler. Instead of relying on complex JSON parsing to determine object boundaries—especially when JSON content may include brackets—developers can leverage line-buffered iterators to process incoming chunks more efficiently. This clarification aligns the documentation with common parsing practices for streaming JSON data and reduces potential ambiguity.
GiteaMirror added the documentation label 2026-04-12 15:58:38 -05:00
Author
Owner

@gwpl commented on GitHub (Nov 18, 2024):

Technically speaking it's both: feature request AND documentation,

as this ticket requests "feature", that it is guaranteed, that responses are returned as "one json object per line" to simplify buffer management on client side and correct robust json parsing...

<!-- gh-comment-id:2483911086 --> @gwpl commented on GitHub (Nov 18, 2024): Technically speaking it's both: feature request AND documentation, as this ticket requests "feature", that it is guaranteed, that responses are returned as "one json object per line" to simplify buffer management on client side and correct robust json parsing...
Author
Owner

@gwpl commented on GitHub (Nov 20, 2024):

While updateing to https://jsonlines.org/ from https://ndjson.org/ maybe worth to consider updating following sections:

bfd30f4286/api/client.go (L151C1-L152C54)

	request.Header.Set("Content-Type", "application/json")
	request.Header.Set("Accept", "application/x-ndjson")

bfd30f4286/server/routes.go (L1324C1-L1324C50)

	c.Header("Content-Type", "application/x-ndjson")
<!-- gh-comment-id:2488346735 --> @gwpl commented on GitHub (Nov 20, 2024): While updateing to https://jsonlines.org/ from https://ndjson.org/ maybe worth to consider updating following sections: https://github.com/ollama/ollama/blob/bfd30f428682cca87f8dcd953fdd2af754a19f89/api/client.go#L151C1-L152C54 ``` request.Header.Set("Content-Type", "application/json") request.Header.Set("Accept", "application/x-ndjson") ``` https://github.com/ollama/ollama/blob/bfd30f428682cca87f8dcd953fdd2af754a19f89/server/routes.go#L1324C1-L1324C50 ``` c.Header("Content-Type", "application/x-ndjson") ```
Author
Owner

@gwpl commented on GitHub (Nov 20, 2024):

Also it looks like in code you are using json.Marshal .

As I am not mainly golang programmer I checked specification and asked perplexity to double check:
https://www.perplexity.ai/search/a53b73a7-5e65-4765-b8db-17dda3adb52d

And looks like that in order to consistently guarantee ollama to return one json object per line,
that json.Compact maybe needed to be introduced :
https://pkg.go.dev/encoding/json#Compact

<!-- gh-comment-id:2488620071 --> @gwpl commented on GitHub (Nov 20, 2024): Also it looks like in code you are using `json.Marshal` . As I am not mainly golang programmer I checked specification and asked perplexity to double check: https://www.perplexity.ai/search/a53b73a7-5e65-4765-b8db-17dda3adb52d And looks like that in order to consistently guarantee ollama to return one json object per line, that `json.Compact` maybe needed to be introduced : https://pkg.go.dev/encoding/json#Compact
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: github-starred/ollama#4920