refactor: read tool['arguments'] directly in the JSON completion guard

Restores the pre-existing comment above the guard to its original wording and drops the `buffered` local, so the guard and the parse call both read `tool['arguments']`, the name the rest of the file already uses for that buffer. Behaviour is unchanged: same three conditions in the same order, same short-circuit result.
This commit is contained in:
Classic298 2026-08-30 22:18:35 +02:00
parent a5933798b2
commit 653e2ce3b4

View file

@ -874,15 +874,18 @@ async def openai_stream_to_anthropic_stream(openai_stream_generator, model: str
}
yield f'event: content_block_delta\ndata: {JSONCodec.dumps(block_delta)}\n\n'.encode()
# Close the block once arguments form complete JSON, without re-parsing on every chunk
buffered = tool['arguments']
# Close the block once arguments form complete JSON
if (
tool['started']
and not tool['stopped']
and (buffered[-1] == '}' or buffered[-1].isspace() or buffered[0] != '{')
and (
tool['arguments'][-1] == '}'
or tool['arguments'][-1].isspace()
or tool['arguments'][0] != '{'
)
):
try:
JSONCodec.loads(buffered)
JSONCodec.loads(tool['arguments'])
tool['stopped'] = True
block_stop = {
'type': 'content_block_stop',