跳到正文
DEV Community· Pierre-Laurent Medori·· 31 分鐘前AI 評分59

Anthropic Python SDK 1.14.0 的串流工具輸入截斷後,jiter 仍可能解析出缺少 append 的有效 JSON

A tool call cut at "append":tru came back as valid JSON, without append

AI 導讀

Pierre-Laurent Medori 發現,jiter 0.18.0 會把部分截斷的串流工具輸入解析成有效 JSON,遺失 append 欄位。這可能令工具呼叫覆寫檔案,而非追加內容。修補方案保留每次工具呼叫的原始串流文本,並以嚴格 JSON 解析驗證,讓無效呼叫報錯。

正文
>>> from jiter import from_json
>>> from_json(b'{"page":"p1","path":"index.html","content":"<p>Hi</p>","append":tru', partial_mode=True)
{'page': 'p1', 'path': 'index.html', 'content': '<p>Hi</p>'}
>>> from_json(b'{"page":"p1","path":"index.html","content":"<p>H', partial_mode=True)
{'page': 'p1', 'path': 'index.html'}

That is jiter 0.18.0, which we pin, the parser that version 1.14.0 of Anthropic's Python SDK uses for streamed tool input, reading two of our write_file calls, cut short as when the model runs out of output tokens. The second has lost content, which the tool requires, so it is refused. The first, cut in the middle of true, has lost only append, and it is a valid call. If it runs, it rewrites the file with that one part instead of appending it, and the tool only refuses a whole rewrite when the existing file is large.

I run backend engineering at GoodBarber, an app platform. The agent of our AI Extension Builder writes the files of a section with tools like write_file, in the studio, the Django service behind the builder's back-office screen. One adapter in our harness is built on that SDK, and it doesn't serve our customers' apps. On October 26, we were making the studio show each file while the model writes it, which meant asking for eager_input_streaming on every tool. Without it, the API sends each parameter whole, so a cut falls between two fields and still loses append; with it, a cut can fall inside true. One of the two other models reviewing the branch flagged the first case.

Partial mode, at every piece

In SDK 1.14.0, the one we pin, a line of anthropic/lib/streaming/_messages.py is content.input = from_json(json_buf, partial_mode=True). It runs at every input_json_delta piece of a streamed message, on the call's text so far. The jiter docstring describes that mode as "allow incomplete JSON but discard the last string if it is incomplete", and a literal cut short, like tru, is dropped too. Nothing reads the input strictly when the block ends, so the final message keeps the last partial read. SDK 0.88.0, with jiter 0.13.0, does the same, and so does 2.6.0, the latest on November 6. A number cut short stays too, with the wrong value: {"path":"index.html","start_line":12 reads as start_line 12, whatever the model was writing. I haven't reported it upstream.

A guard for the other cut

On October 20, we had made the adapter raise an error when a stream ends without message_stop, since a cut call would pass for whole. That catches a connection that drops. A cut by the output budget is a normal end: the message carries stop_reason: "max_tokens", and the stream still closes with message_stop. The guard let it through, and our agent loop runs the calls of a cut turn before it tells the model its output was cut. Anthropic's stop reasons page warns about an incomplete tool use block on max_tokens, and our loop doesn't look for one.

Passing on what the model wrote

My Claude Code session checked the finding against the SDK's source, and a subagent committed the fix. It keeps the raw text of each streamed call as the model wrote it. When that text is not a JSON object under a strict json.loads, it becomes the call's arguments. The code that runs our calls already refused arguments that are not valid JSON, so the cut call gets an error, as Anthropic's docs advise, then the loop says the output was cut.

The test cuts a fake stream at true} on the adapter's three streamed paths, and all three fail without the fix.

The fix reached master that night with the rest of the branch, and I deployed it on the morning of October 27.

來源:DEV Community · dev.to