5
5
from contextlib import asynccontextmanager
6
6
from dataclasses import dataclass , field
7
7
from datetime import datetime , timezone
8
- from json import JSONDecodeError , loads as json_loads
9
8
from typing import Any , Literal , Union , cast , overload
10
9
11
10
from typing_extensions import assert_never
@@ -440,7 +439,6 @@ class AnthropicStreamedResponse(StreamedResponse):
440
439
441
440
async def _get_event_iterator (self ) -> AsyncIterator [ModelResponseStreamEvent ]:
442
441
current_block : ContentBlock | None = None
443
- current_json : str = ''
444
442
445
443
async for event in self ._response :
446
444
self ._usage += _map_usage (event )
@@ -455,7 +453,7 @@ async def _get_event_iterator(self) -> AsyncIterator[ModelResponseStreamEvent]:
455
453
maybe_event = self ._parts_manager .handle_tool_call_delta (
456
454
vendor_part_id = current_block .id ,
457
455
tool_name = current_block .name ,
458
- args = cast (dict [str , Any ], current_block .input ),
456
+ args = cast (dict [str , Any ], current_block .input ) or None ,
459
457
tool_call_id = current_block .id ,
460
458
)
461
459
if maybe_event is not None : # pragma: no branch
@@ -469,20 +467,10 @@ async def _get_event_iterator(self) -> AsyncIterator[ModelResponseStreamEvent]:
469
467
elif ( # pragma: no branch
470
468
current_block and event .delta .type == 'input_json_delta' and isinstance (current_block , ToolUseBlock )
471
469
):
472
- # Try to parse the JSON immediately, otherwise cache the value for later. This handles
473
- # cases where the JSON is not currently valid but will be valid once we stream more tokens.
474
- try :
475
- parsed_args = json_loads (current_json + event .delta .partial_json )
476
- current_json = ''
477
- except JSONDecodeError :
478
- current_json += event .delta .partial_json
479
- continue
480
-
481
- # For tool calls, we need to handle partial JSON updates
482
470
maybe_event = self ._parts_manager .handle_tool_call_delta (
483
471
vendor_part_id = current_block .id ,
484
472
tool_name = '' ,
485
- args = parsed_args ,
473
+ args = event . delta . partial_json ,
486
474
tool_call_id = current_block .id ,
487
475
)
488
476
if maybe_event is not None : # pragma: no branch
0 commit comments