|
5 | 5 | from datetime import datetime |
6 | 6 |
|
7 | 7 | from anthropic import AsyncMessageStreamManager, AsyncStream, Client, MessageStreamManager, Stream |
8 | | -from anthropic.types import ContentBlockDeltaEvent, Message, MessageDeltaEvent, MessageStartEvent, TextBlock |
| 8 | +from anthropic.types import ContentBlockDeltaEvent, Message, MessageDeltaEvent, MessageStartEvent, TextBlock, \ |
| 9 | + InputJSONDelta, ToolUseBlock |
9 | 10 |
|
10 | 11 | from parea.cache.cache import Cache |
11 | 12 | from parea.helpers import timezone_aware_now |
@@ -43,8 +44,6 @@ def init(self, log: Callable, cache: Cache, client: Client): |
43 | 44 | def resolver(trace_id: str, _args: Sequence[Any], kwargs: Dict[str, Any], response: Optional[Message]) -> Optional[Any]: |
44 | 45 | if response: |
45 | 46 | if len(response.content) > 1: |
46 | | - from anthropic.types.beta.tools import ToolUseBlock |
47 | | - |
48 | 47 | output_list = [] |
49 | 48 | for content in response.content: |
50 | 49 | if isinstance(content, TextBlock): |
@@ -185,7 +184,10 @@ def _update_accumulator_streaming(accumulator, info_from_response, chunk): |
185 | 184 | if isinstance(chunk, MessageStartEvent): |
186 | 185 | info_from_response["input_tokens"] = chunk.message.usage.input_tokens |
187 | 186 | elif isinstance(chunk, ContentBlockDeltaEvent): |
188 | | - accumulator["content"].append(chunk.delta.text) |
| 187 | + if isinstance(chunk.delta, InputJSONDelta): |
| 188 | + accumulator["content"].append(chunk.delta.partial_json) |
| 189 | + else: |
| 190 | + accumulator["content"].append(chunk.delta.text) |
189 | 191 | if not info_from_response.get("first_token_timestamp"): |
190 | 192 | info_from_response["first_token_timestamp"] = timezone_aware_now() |
191 | 193 | elif isinstance(chunk, MessageDeltaEvent): |
|
0 commit comments