Streaming drops a JSON object that follows reasoning text on the same line
How it was found
While updating an article's streaming example from claude-sonnet-4-5 to claude-opus-4-8, one of three expected objects stopped arriving (embabel-agent 1.5.4, unchanged on main). Older models followed the converter's JSONL format instructions closely, putting each JSON object on its own line. Newer models treat them as guidance and sometimes put reasoning and an object on the same line.
Problem
StreamingJacksonOutputConverter classifies each line once (ThinkingDetector.detectThinkingState). When reasoning and a JSON object share a line, the whole line is reported as thinking and the object is lost silently: no error, and the stream completes with fewer objects. createObjectStream is affected too.
| Line | Classified as | Result |
|---|---|---|
I'll check all three options.{"name": "x"} |
CONTINUATION |
JSON buried in the thinking text |
<think>reasoning</think>{"name": "x"} |
BOTH |
JSON discarded |
last step</think>{"name": "x"} (end of a multi-line block) |
CONTINUATION |
JSON buried in the thinking text |
Row 1 (I'll check all three options.{"name": "x"}) is the form that was observed. The model wrote "I'll check all three options." just before calling tools, without a line break. Embabel's line buffer kept that text and appended the next turn's first output, the first JSON object, to it.
Object streaming must not lose an object, and model upgrades expose working applications to this without any code change.
Fix: temporary but practical
Split a line only when a JSON object directly follows a closing thinking tag on the same line (rows 2 and 3). The closing tag proves the JSON is outside any thinking block, so the per-line decision is safe. The JSON must also convert to the target type; otherwise the line is left unchanged.
The streaming converter format instructions also add: "DO NOT put thinking blocks and JSON objects on the same line."
Not fixed here: plain text then JSON (row 1)
The detector is stateless, so on its own text {json} is ambiguous: it may be text outside a block (a real object) or a line inside a multi-line block (part of the reasoning):
<think>
Consider {"name": "example"}
</think>
{"name": "result"}
Splitting it would emit example as an object. Telling the two apart needs state across lines, which the multi-line streaming aggregator in #1881 is meant to provide. That is the real solution.
Workaround until then: instruct the model to write reasoning only inside <think></think> and to put each JSON object on its own line.
Not related: #1716
Native thinking doesn't reach the line stream (Spring AI streams it as messages with no text), so it can't share a line with JSON.